Merge "Rearrange build files to support USE_SOONG=true"
diff --git a/libc/Android.bp b/libc/Android.bp
index b1ebe53..8df77ce 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1236,7 +1236,9 @@
                     "arch-x86/atom/string/sse2-strlen-atom.S",
                     "arch-x86/atom/string/ssse3-bcopy-atom.S",
                     "arch-x86/atom/string/ssse3-memcmp-atom.S",
+                    "arch-x86/atom/string/ssse3-memcpy-atom.S",
                     "arch-x86/atom/string/ssse3-memmove-atom.S",
+                    "arch-x86/atom/string/ssse3-strcpy-atom.S",
                     "arch-x86/atom/string/ssse3-strncpy-atom.S",
                     "arch-x86/atom/string/ssse3-wmemcmp-atom.S",
                 ],
diff --git a/libc/arch-x86/atom/string/sse2-strrchr-atom.S b/libc/arch-x86/atom/string/sse2-strrchr-atom.S
index da3dc3b..e916bc1 100644
--- a/libc/arch-x86/atom/string/sse2-strrchr-atom.S
+++ b/libc/arch-x86/atom/string/sse2-strrchr-atom.S
@@ -278,7 +278,7 @@
 	jnz	L(FindZeroExit2)
 	test	$0x04, %cl
 	jnz	L(FindZeroExit3)
-	and	$1 << 4 - 1, %eax
+	and	$(1 << 4) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -296,7 +296,7 @@
 	jnz	L(FindZeroExit6)
 	test	$0x40, %cl
 	jnz	L(FindZeroExit7)
-	and	$1 << 8 - 1, %eax
+	and	$(1 << 8) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -317,7 +317,7 @@
 	jnz	L(FindZeroExit10)
 	test	$0x04, %ch
 	jnz	L(FindZeroExit11)
-	and	$1 << 12 - 1, %eax
+	and	$(1 << 12) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -335,7 +335,7 @@
 	jnz	L(FindZeroExit14)
 	test	$0x40, %ch
 	jnz	L(FindZeroExit15)
-	and	$1 << 16 - 1, %eax
+	and	$(1 << 16) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -359,7 +359,7 @@
 
 	.p2align 4
 L(FindZeroExit2):
-	and	$1 << 2 - 1, %eax
+	and	$(1 << 2) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -371,7 +371,7 @@
 
 	.p2align 4
 L(FindZeroExit3):
-	and	$1 << 3 - 1, %eax
+	and	$(1 << 3) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -383,7 +383,7 @@
 
 	.p2align 4
 L(FindZeroExit5):
-	and	$1 << 5 - 1, %eax
+	and	$(1 << 5) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -395,7 +395,7 @@
 
 	.p2align 4
 L(FindZeroExit6):
-	and	$1 << 6 - 1, %eax
+	and	$(1 << 6) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -407,7 +407,7 @@
 
 	.p2align 4
 L(FindZeroExit7):
-	and	$1 << 7 - 1, %eax
+	and	$(1 << 7) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -419,7 +419,7 @@
 
 	.p2align 4
 L(FindZeroExit9):
-	and	$1 << 9 - 1, %eax
+	and	$(1 << 9) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -431,7 +431,7 @@
 
 	.p2align 4
 L(FindZeroExit10):
-	and	$1 << 10 - 1, %eax
+	and	$(1 << 10) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -443,7 +443,7 @@
 
 	.p2align 4
 L(FindZeroExit11):
-	and	$1 << 11 - 1, %eax
+	and	$(1 << 11) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -455,7 +455,7 @@
 
 	.p2align 4
 L(FindZeroExit13):
-	and	$1 << 13 - 1, %eax
+	and	$(1 << 13) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -467,7 +467,7 @@
 
 	.p2align 4
 L(FindZeroExit14):
-	and	$1 << 14 - 1, %eax
+	and	$(1 << 14) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -479,7 +479,7 @@
 
 	.p2align 4
 L(FindZeroExit15):
-	and	$1 << 15 - 1, %eax
+	and	$(1 << 15) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%ebx)
@@ -619,7 +619,7 @@
 	jnz	L(PrologFindZeroExit2)
 	test	$0x04, %cl
 	jnz	L(PrologFindZeroExit3)
-	and	$1 << 4 - 1, %eax
+	and	$(1 << 4) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
@@ -632,7 +632,7 @@
 	jnz	L(PrologFindZeroExit6)
 	test	$0x40, %cl
 	jnz	L(PrologFindZeroExit7)
-	and	$1 << 8 - 1, %eax
+	and	$(1 << 8) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
@@ -648,7 +648,7 @@
 	jnz	L(PrologFindZeroExit10)
 	test	$0x04, %ch
 	jnz	L(PrologFindZeroExit11)
-	and	$1 << 12 - 1, %eax
+	and	$(1 << 12) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
@@ -661,7 +661,7 @@
 	jnz	L(PrologFindZeroExit14)
 	test	$0x40, %ch
 	jnz	L(PrologFindZeroExit15)
-	and	$1 << 16 - 1, %eax
+	and	$(1 << 16) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
@@ -675,77 +675,77 @@
 
 	.p2align 4
 L(PrologFindZeroExit2):
-	and	$1 << 2 - 1, %eax
+	and	$(1 << 2) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
 
 	.p2align 4
 L(PrologFindZeroExit3):
-	and	$1 << 3 - 1, %eax
+	and	$(1 << 3) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
 
 	.p2align 4
 L(PrologFindZeroExit5):
-	and	$1 << 5 - 1, %eax
+	and	$(1 << 5) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
 
 	.p2align 4
 L(PrologFindZeroExit6):
-	and	$1 << 6 - 1, %eax
+	and	$(1 << 6) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
 
 	.p2align 4
 L(PrologFindZeroExit7):
-	and	$1 << 7 - 1, %eax
+	and	$(1 << 7) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
 
 	.p2align 4
 L(PrologFindZeroExit9):
-	and	$1 << 9 - 1, %eax
+	and	$(1 << 9) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
 
 	.p2align 4
 L(PrologFindZeroExit10):
-	and	$1 << 10 - 1, %eax
+	and	$(1 << 10) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
 
 	.p2align 4
 L(PrologFindZeroExit11):
-	and	$1 << 11 - 1, %eax
+	and	$(1 << 11) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
 
 	.p2align 4
 L(PrologFindZeroExit13):
-	and	$1 << 13 - 1, %eax
+	and	$(1 << 13) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
 
 	.p2align 4
 L(PrologFindZeroExit14):
-	and	$1 << 14 - 1, %eax
+	and	$(1 << 14) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
 
 	.p2align 4
 L(PrologFindZeroExit15):
-	and	$1 << 15 - 1, %eax
+	and	$(1 << 15) - 1, %eax
 	jnz	L(match_case1)
 	xor	%eax, %eax
 	RETURN
diff --git a/libc/arch-x86/atom/string/sse2-wcsrchr-atom.S b/libc/arch-x86/atom/string/sse2-wcsrchr-atom.S
index e30779d..1a55df2 100644
--- a/libc/arch-x86/atom/string/sse2-wcsrchr-atom.S
+++ b/libc/arch-x86/atom/string/sse2-wcsrchr-atom.S
@@ -280,7 +280,7 @@
 
 	.p2align 4
 L(find_zero_in_second_wchar):
-	and	$1 << 5 - 1, %eax
+	and	$(1 << 5) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%esi)
@@ -296,7 +296,7 @@
 L(find_zero_in_third_or_fourth_wchar):
 	test	$15, %ch
 	jz	L(find_zero_in_fourth_wchar)
-	and	$1 << 9 - 1, %eax
+	and	$(1 << 9) - 1, %eax
 	jz	L(return_value)
 
 	POP	(%esi)
@@ -368,7 +368,7 @@
 
 	.p2align 4
 L(prolog_find_zero_in_second_wchar):
-	and	$1 << 5 - 1, %eax
+	and	$(1 << 5) - 1, %eax
 	jz	L(return_null)
 
 	test	$15 << 4, %al
@@ -380,7 +380,7 @@
 L(prolog_find_zero_in_third_or_fourth_wchar):
 	test	$15, %ch
 	jz	L(prolog_find_zero_in_fourth_wchar)
-	and	$1 << 9 - 1, %eax
+	and	$(1 << 9) - 1, %eax
 	jz	L(return_null)
 
 	test	%ah, %ah
diff --git a/libc/include/sys/sysmacros.h b/libc/include/sys/sysmacros.h
index 6f053a8..54e43dd 100644
--- a/libc/include/sys/sysmacros.h
+++ b/libc/include/sys/sysmacros.h
@@ -25,28 +25,20 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _SYS_SYSMACROS_H_
 #define _SYS_SYSMACROS_H_
 
-/* some rogue code includes this file directly :-( */
-#ifndef _SYS_TYPES_H_
-# include <sys/types.h>
-#endif
+#define makedev(__major, __minor) \
+  ( \
+    (((__major) & 0xfffff000ULL) << 32) | (((__major) & 0xfffULL) << 8) | \
+    (((__minor) & 0xffffff00ULL) << 12) | (((__minor) & 0xffULL)) \
+  )
 
-static __inline__ int major(dev_t _dev)
-{
-  return (_dev >> 8) & 0xfff;
-}
+#define major(__dev) \
+  ((unsigned) ((((unsigned long long) (__dev) >> 32) & 0xfffff000) | (((__dev) >> 8) & 0xfff)))
 
-static __inline__ int minor(dev_t _dev)
-{
-  return (_dev & 0xff) | ((_dev >> 12) & 0xfff00);
-}
-
-static __inline__ dev_t makedev(int __ma, int __mi)
-{
-  return ((__ma & 0xfff) << 8) | (__mi & 0xff) | ((__mi & 0xfff00) << 12);
-}
+#define minor(__dev) \
+  ((unsigned) ((((__dev) >> 12) & 0xffffff00) | ((__dev) & 0xff)))
 
 #endif /* _SYS_SYSMACROS_H_ */
-
diff --git a/libc/include/sys/types.h b/libc/include/sys/types.h
index a6b0fd8..217fd60 100644
--- a/libc/include/sys/types.h
+++ b/libc/include/sys/types.h
@@ -139,19 +139,18 @@
 typedef unsigned int        uint_t;
 typedef unsigned int        uint;
 
-/* for some applications */
+#ifdef __BSD_VISIBLE
 #include <sys/sysmacros.h>
 
-#ifdef __BSD_VISIBLE
-typedef	unsigned char	u_char;
-typedef	unsigned short	u_short;
-typedef	unsigned int	u_int;
-typedef	unsigned long	u_long;
+typedef unsigned char  u_char;
+typedef unsigned short u_short;
+typedef unsigned int   u_int;
+typedef unsigned long  u_long;
 
-typedef uint32_t       u_int32_t;
-typedef uint16_t       u_int16_t;
-typedef uint8_t        u_int8_t;
-typedef uint64_t       u_int64_t;
+typedef uint32_t u_int32_t;
+typedef uint16_t u_int16_t;
+typedef uint8_t  u_int8_t;
+typedef uint64_t u_int64_t;
 #endif
 
 #endif
diff --git a/tests/Android.mk b/tests/Android.mk
index 0e84331..3a0d6ee 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -71,6 +71,7 @@
     malloc_test.cpp \
     math_test.cpp \
     mntent_test.cpp \
+    net_if_test.cpp \
     netdb_test.cpp \
     netinet_udp_test.cpp \
     pthread_test.cpp \
@@ -106,6 +107,7 @@
     sys_statvfs_test.cpp \
     sys_syscall_test.cpp \
     sys_sysinfo_test.cpp \
+    sys_sysmacros_test.cpp \
     sys_time_test.cpp \
     sys_types_test.cpp \
     sys_uio_test.cpp \
diff --git a/tests/ifaddrs_test.cpp b/tests/ifaddrs_test.cpp
index bbaaec3..8159710 100644
--- a/tests/ifaddrs_test.cpp
+++ b/tests/ifaddrs_test.cpp
@@ -19,6 +19,9 @@
 #include <ifaddrs.h>
 
 #include <linux/if_packet.h>
+#include <net/ethernet.h>
+#include <net/if.h>
+#include <netdb.h>
 #include <netinet/in.h>
 
 TEST(ifaddrs, freeifaddrs_null) {
@@ -61,3 +64,63 @@
 
   freeifaddrs(addrs);
 }
+
+static void print_sockaddr_ll(const char* what, const sockaddr* p) {
+  const sockaddr_ll* s = reinterpret_cast<const sockaddr_ll*>(p);
+  printf("\t%s\t", what);
+  for (int i = 0; i < s->sll_halen; ++i) {
+    if (i > 0) printf(":");
+    printf("%02X", s->sll_addr[i]);
+  }
+  printf(" (%d bytes)\n", s->sll_halen);
+}
+
+static void print_sockaddr_inet(const char* what, const sockaddr* addr) {
+  char host[NI_MAXHOST];
+  int family = addr->sa_family;
+  int error = getnameinfo(addr,
+                          (family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6),
+                          host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST);
+  if (error != 0) {
+    printf("%d getnameinfo() failed: %s\n", family, gai_strerror(error));
+    strcpy(host, "???");
+  }
+  printf("\t%s: <%s>\n", what, host);
+}
+
+static const char* family_to_name(int family) {
+  if (family == AF_INET) return "AF_INET";
+  if (family == AF_INET6) return "AF_INET6";
+  if (family == AF_PACKET) return "AF_PACKET";
+  if (family == AF_UNSPEC) return "AF_UNSPEC";
+  return "?";
+}
+
+// Not really a test, but a useful debugging tool.
+TEST(ifaddrs, dump) {
+  ifaddrs* addrs;
+  ASSERT_EQ(0, getifaddrs(&addrs));
+
+  for (ifaddrs* ifa = addrs; ifa != nullptr; ifa = ifa->ifa_next) {
+    int family = ifa->ifa_addr ? ifa->ifa_addr->sa_family :
+                                 ifa->ifa_broadaddr ? ifa->ifa_broadaddr->sa_family : AF_UNSPEC;
+
+    printf("%s\n\t%s (%d) flags=%#x\n",
+           ifa->ifa_name, family_to_name(family), family, ifa->ifa_flags);
+
+    if (family == AF_PACKET) {
+      if (ifa->ifa_addr) print_sockaddr_ll("hwaddr", ifa->ifa_addr);
+      if (ifa->ifa_broadaddr) print_sockaddr_ll("hwbroad", ifa->ifa_addr);
+    } else if (family == AF_INET || family == AF_INET6) {
+      if (ifa->ifa_addr) print_sockaddr_inet("address", ifa->ifa_addr);
+      if (ifa->ifa_broadaddr && (ifa->ifa_flags & (IFF_BROADCAST | IFF_POINTOPOINT)) != 0) {
+        print_sockaddr_inet((ifa->ifa_flags & IFF_BROADCAST) ? "broadcast" : "destination",
+                            ifa->ifa_broadaddr);
+      }
+    }
+
+    fflush(stdout);
+  }
+
+  freeifaddrs(addrs);
+}
diff --git a/tests/net_if_test.cpp b/tests/net_if_test.cpp
new file mode 100644
index 0000000..9f38411
--- /dev/null
+++ b/tests/net_if_test.cpp
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <net/if.h>
+
+#include <errno.h>
+
+#include <gtest/gtest.h>
+
+TEST(net_if, if_nametoindex_if_indextoname) {
+  unsigned index;
+  index = if_nametoindex("lo");
+  ASSERT_NE(index, 0U);
+
+  char buf[IF_NAMESIZE] = {};
+  char* name = if_indextoname(index, buf);
+  ASSERT_STREQ("lo", name);
+}
+
+TEST(net_if, if_nametoindex_fail) {
+  unsigned index = if_nametoindex("this-interface-does-not-exist");
+  ASSERT_EQ(0U, index);
+}
diff --git a/tests/sys_sysmacros_test.cpp b/tests/sys_sysmacros_test.cpp
new file mode 100644
index 0000000..f17fac5
--- /dev/null
+++ b/tests/sys_sysmacros_test.cpp
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/sysmacros.h>
+
+#include <gtest/gtest.h>
+
+TEST(sys_sysmacros, makedev) {
+  ASSERT_EQ(0x12345aabbcc678ddULL, makedev(0x12345678, 0xaabbccdd));
+}
+
+TEST(sys_sysmacros, major) {
+  ASSERT_EQ(0x12345678UL, major(0x12345aabbcc678dd));
+}
+
+TEST(sys_sysmacros, minor) {
+  ASSERT_EQ(0xaabbccddUL, minor(0x12345aabbcc678dd));
+}