Fix all resolv warnings and turn on -Werror.

The res_init.c changes bring us a bit closer to upstream too, though
there's still work to be done there. Some of the remaining differences
look like bugs we'd want to fix, so we should definitely try to come
back to that.

Change-Id: I50baa148e967c90d55d711e9904ad54c7d724d4d
diff --git a/libc/Android.mk b/libc/Android.mk
index 0f9cf6a..078be59 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -637,7 +637,6 @@
     -I$(LOCAL_PATH)/upstream-netbsd/lib/libc/include \
     -I$(LOCAL_PATH)/upstream-netbsd/android/include \
     -include netbsd-compat.h \
-    -Wno-error \
 
 LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
 LOCAL_CPPFLAGS := $(libc_common_cppflags)
diff --git a/libc/dns/net/getservbyname.c b/libc/dns/net/getservbyname.c
index 5ea528e..c95c9b0 100644
--- a/libc/dns/net/getservbyname.c
+++ b/libc/dns/net/getservbyname.c
@@ -34,7 +34,6 @@
 getservbyname(const char *name, const char *proto)
 {
     res_static       rs = __res_get_static();
-    struct servent*  s;
 
     if (rs == NULL || proto == NULL || name == NULL) {
         errno = EINVAL;
diff --git a/libc/dns/net/getservbyport.c b/libc/dns/net/getservbyport.c
index fad7e23..7dcaafb 100644
--- a/libc/dns/net/getservbyport.c
+++ b/libc/dns/net/getservbyport.c
@@ -33,7 +33,6 @@
 getservbyport(int port, const char *proto)
 {
     res_static       rs = __res_get_static();
-    struct servent*  s;
 
     if (rs == NULL || proto == NULL) {
         errno = EINVAL;
diff --git a/libc/dns/resolv/res_cache.c b/libc/dns/resolv/res_cache.c
index 9df97cd..77a1b4d 100644
--- a/libc/dns/resolv/res_cache.c
+++ b/libc/dns/resolv/res_cache.c
@@ -2043,7 +2043,7 @@
         register char **pp = statp->dnsrch;
         register int *p = info->dnsrch_offset;
         while (pp < statp->dnsrch + MAXDNSRCH && *p != -1) {
-            *pp++ = &statp->defdname + *p++;
+            *pp++ = &statp->defdname[0] + *p++;
         }
     }
     pthread_mutex_unlock(&_res_cache_list_lock);
diff --git a/libc/dns/resolv/res_init.c b/libc/dns/resolv/res_init.c
index 158466e..f1cbed8 100644
--- a/libc/dns/resolv/res_init.c
+++ b/libc/dns/resolv/res_init.c
@@ -104,10 +104,6 @@
 #include <sys/system_properties.h>
 #endif /* ANDROID_CHANGES */
 
-#ifndef MIN
-#define	MIN(x,y)	((x)<(y)?(x):(y))
-#endif
-
 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
 #ifdef ANDROID_CHANGES
 #include "resolv_netid.h"
@@ -125,9 +121,11 @@
 
 static void res_setoptions __P((res_state, const char *, const char *));
 
+#ifdef RESOLVSORT
 static const char sort_mask[] = "/&";
 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
-static u_int32_t net_mask __P((struct in_addr));
+static uint32_t net_mask(struct in_addr);
+#endif
 
 #if !defined(isascii)	/* XXX - could be a function */
 # define isascii(c) (!(c & 0200))
@@ -168,15 +166,25 @@
 /* This function has to be reachable by res_data.c but not publicly. */
 int
 __res_vinit(res_state statp, int preinit) {
+#if !defined(__ANDROID__)
 	register FILE *fp;
+#endif
 	register char *cp, **pp;
+#if !defined(__ANDROID__)
 	register int n;
+#endif
 	char buf[BUFSIZ];
 	int nserv = 0;    /* number of nameserver records read from file */
+#if !defined(__ANDROID__)
 	int haveenv = 0;
+#endif
 	int havesearch = 0;
+#ifdef RESOLVSORT
 	int nsort = 0;
+#endif
+#if !defined(__ANDROID__)
 	char *net;
+#endif
 	int dots;
 	union res_sockaddr_union u[2];
 
@@ -235,7 +243,7 @@
 	statp->nsort = 0;
 	res_setservers(statp, u, nserv);
 
-#if 0 /* IGNORE THE ENVIRONMENT */
+#if !defined(__ANDROID__) /* IGNORE THE ENVIRONMENT */
 	/* Allow user to override the local domain definition */
 	if ((cp = getenv("LOCALDOMAIN")) != NULL) {
 		(void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
@@ -593,12 +601,12 @@
 	}
 }
 
+#ifdef RESOLVSORT
 /* XXX - should really support CIDR which means explicit masks always. */
-static u_int32_t
-net_mask(in)		/* XXX - should really use system's version of this */
-	struct in_addr in;
+static uint32_t
+net_mask(struct in_addr in)	/*!< XXX - should really use system's version of this */
 {
-	register u_int32_t i = ntohl(in.s_addr);
+	register uint32_t i = ntohl(in.s_addr);
 
 	if (IN_CLASSA(i))
 		return (htonl(IN_CLASSA_NET));
@@ -606,6 +614,7 @@
 		return (htonl(IN_CLASSB_NET));
 	return (htonl(IN_CLASSC_NET));
 }
+#endif
 
 #ifdef ANDROID_CHANGES
 static int
@@ -617,8 +626,7 @@
 	}
 
 	/* read from the random device, returning -1 on failure (or too many retries)*/
-	u_int retry = 5;
-	for (retry; retry > 0; retry--) {
+	for (u_int retry = 5; retry > 0; retry--) {
 		int retval = read(random_device, random_value, sizeof(u_int));
 		if (retval == sizeof(u_int)) {
 			*random_value &= 0xffff;
@@ -649,7 +657,7 @@
 	return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
 }
 
-/*
+/*%
  * This routine is for closing the socket if a virtual circuit is used and
  * the program wants to close it.  This provides support for endhostent()
  * which expects to close the socket.
@@ -657,7 +665,8 @@
  * This routine is not expected to be user visible.
  */
 void
-res_nclose(res_state statp) {
+res_nclose(res_state statp)
+{
 	int ns;
 
 	if (statp->_vcsock >= 0) {
@@ -674,7 +683,8 @@
 }
 
 void
-res_ndestroy(res_state statp) {
+res_ndestroy(res_state statp)
+{
 	res_nclose(statp);
 	if (statp->_u._ext.ext != NULL)
 		free(statp->_u._ext.ext);
@@ -683,21 +693,24 @@
 }
 
 const char *
-res_get_nibblesuffix(res_state statp) {
+res_get_nibblesuffix(res_state statp)
+{
 	if (statp->_u._ext.ext)
 		return (statp->_u._ext.ext->nsuffix);
 	return ("ip6.arpa");
 }
 
 const char *
-res_get_nibblesuffix2(res_state statp) {
+res_get_nibblesuffix2(res_state statp)
+{
 	if (statp->_u._ext.ext)
 		return (statp->_u._ext.ext->nsuffix2);
 	return ("ip6.int");
 }
 
 void
-res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
+res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt)
+{
 	int i, nserv;
 	size_t size;
 
@@ -718,10 +731,8 @@
 			if (size <= sizeof(statp->nsaddr_list[nserv]))
 				memcpy(&statp->nsaddr_list[nserv],
 					&set->sin, size);
-#ifdef notdef
 			else
 				statp->nsaddr_list[nserv].sin_family = 0;
-#endif
 			nserv++;
 			break;
 
@@ -750,10 +761,11 @@
 }
 
 int
-res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
+res_getservers(res_state statp, union res_sockaddr_union *set, int cnt)
+{
 	int i;
 	size_t size;
-	u_int16_t family;
+	uint16_t family;
 
 	for (i = 0; i < statp->nscount && i < cnt; i++) {
 		if (statp->_u._ext.ext)
diff --git a/libc/dns/resolv/res_send.c b/libc/dns/resolv/res_send.c
index b6a990a..4a010d5 100644
--- a/libc/dns/resolv/res_send.c
+++ b/libc/dns/resolv/res_send.c
@@ -764,7 +764,7 @@
 		struct sockaddr_storage peer;
 		socklen_t size = sizeof peer;
 		unsigned old_mark;
-		int mark_size = sizeof(old_mark);
+		socklen_t mark_size = sizeof(old_mark);
 		if (getpeername(statp->_vcsock,
 				(struct sockaddr *)(void *)&peer, &size) < 0 ||
 		    !sock_eq((struct sockaddr *)(void *)&peer, nsap) ||