Merge "Update docs/status.md now fexecve is implemented."
diff --git a/docs/status.md b/docs/status.md
index 2c8331b..d9bd0af 100644
--- a/docs/status.md
+++ b/docs/status.md
@@ -17,6 +17,7 @@
* `iconv`/`iconv_close`/`iconv_open` (adding <iconv.h>)
* `pthread_setschedprio`
* <spawn.h>
+ * `swab`
* `syncfs`
New libc functions in O:
@@ -89,7 +90,6 @@
pthread_setcanceltype
pthread_testcancel
sockatmark
-swab
wordexp
wordfree
```
diff --git a/libc/Android.bp b/libc/Android.bp
index 81135ea..2e5ec00 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1534,6 +1534,7 @@
"bionic/strings_l.cpp",
"bionic/strsignal.cpp",
"bionic/strtold.cpp",
+ "bionic/swab.cpp",
"bionic/symlink.cpp",
"bionic/sync_file_range.cpp",
"bionic/sys_msg.cpp",
diff --git a/libc/bionic/swab.cpp b/libc/bionic/swab.cpp
new file mode 100644
index 0000000..bc53ba4
--- /dev/null
+++ b/libc/bionic/swab.cpp
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <unistd.h>
+
+void swab(const void* void_src, void* void_dst, ssize_t byte_count) {
+ const uint8_t* src = static_cast<const uint8_t*>(void_src);
+ uint8_t* dst = static_cast<uint8_t*>(void_dst);
+ while (byte_count > 1) {
+ uint8_t x = *src++;
+ uint8_t y = *src++;
+ *dst++ = y;
+ *dst++ = x;
+ byte_count -= 2;
+ }
+}
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index fe98c10..c60cf80 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -247,6 +247,8 @@
int getdomainname(char* __buf, size_t __buf_size) __INTRODUCED_IN(26);
int setdomainname(const char* __name, size_t __n) __INTRODUCED_IN(26);
+void swab(const void* __src, void* __dst, ssize_t __byte_count) __INTRODUCED_IN_FUTURE;
+
#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
#include <bits/fortify/unistd.h>
#endif
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index af4efb9..6e1015a 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1367,6 +1367,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index 5c7f726..b88ecd0 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1287,6 +1287,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index 33ecbed..4fc6535 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1392,6 +1392,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index 579491a..360af09 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1351,6 +1351,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index 5c7f726..b88ecd0 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1287,6 +1287,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index 7d1d3ef..04ff514 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1349,6 +1349,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index 5c7f726..b88ecd0 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1287,6 +1287,7 @@
sethostent;
setnetent;
setprotoent;
+ swab;
syncfs;
} LIBC_O;
diff --git a/tests/linux_swab_test.cpp b/tests/linux_swab_test.cpp
index 6b964dc..ffd4072 100644
--- a/tests/linux_swab_test.cpp
+++ b/tests/linux_swab_test.cpp
@@ -21,7 +21,7 @@
// This test makes sure that references to all of the kernel swab
// macros/inline functions that are exported work properly.
// Verifies that any kernel header updates do not break these macros.
-TEST(swab, fswa) {
+TEST(linux_swab, smoke) {
EXPECT_EQ(0x3412U, __swab16(0x1234));
EXPECT_EQ(0x78563412U, __swab32(0x12345678U));
EXPECT_EQ(0xbaefcdab78563412ULL, __swab64(0x12345678abcdefbaULL));
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index c79ed59..3143c50 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -1443,3 +1443,46 @@
EXPECT_EQ(0, getlogin_r(buf, sizeof(buf)));
EXPECT_STREQ(getlogin(), buf);
}
+
+TEST(UNISTD_TEST, swab) {
+ // POSIX: "The swab() function shall copy nbytes bytes, which are pointed to by src,
+ // to the object pointed to by dest, exchanging adjacent bytes."
+ char buf[BUFSIZ];
+ memset(buf, 'x', sizeof(buf));
+ swab("ehll oowlr\0d", buf, 12);
+ ASSERT_STREQ("hello world", buf);
+}
+
+TEST(UNISTD_TEST, swab_odd_byte_count) {
+ // POSIX: "If nbytes is odd, swab() copies and exchanges nbytes-1 bytes and the disposition
+ // of the last byte is unspecified."
+ // ...but it seems unreasonable to not just leave the last byte alone.
+ char buf[BUFSIZ];
+ memset(buf, 'x', sizeof(buf));
+ swab("012345", buf, 3);
+ ASSERT_EQ('1', buf[0]);
+ ASSERT_EQ('0', buf[1]);
+ ASSERT_EQ('x', buf[2]);
+}
+
+TEST(UNISTD_TEST, swab_overlap) {
+ // POSIX: "If copying takes place between objects that overlap, the behavior is undefined."
+ // ...but it seems unreasonable to not just do the right thing.
+ char buf[] = "012345";
+ swab(buf, buf, 4);
+ ASSERT_EQ('1', buf[0]);
+ ASSERT_EQ('0', buf[1]);
+ ASSERT_EQ('3', buf[2]);
+ ASSERT_EQ('2', buf[3]);
+ ASSERT_EQ('4', buf[4]);
+ ASSERT_EQ('5', buf[5]);
+ ASSERT_EQ(0, buf[6]);
+}
+
+TEST(UNISTD_TEST, swab_negative_byte_count) {
+ // POSIX: "If nbytes is negative, swab() does nothing."
+ char buf[BUFSIZ];
+ memset(buf, 'x', sizeof(buf));
+ swab("hello", buf, -1);
+ ASSERT_EQ('x', buf[0]);
+}