Remove compiler warnings when building Bionic.

Also add missing declarations to misc. functions.
Fix clearerr() implementation (previous was broken).
Handle feature test macros like _POSIX_C_SOURCE properly.

Change-Id: Icdc973a6b9d550a166fc2545f727ea837fe800c4
diff --git a/libc/Android.mk b/libc/Android.mk
index 6b2334e..80c8cc7 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -441,7 +441,6 @@
 		-D_LIBC=1 			\
 		-DSOFTFLOAT                     \
 		-DFLOATING_POINT		\
-		-DNEED_PSELECT=1		\
 		-DINET6 \
 		-I$(LOCAL_PATH)/private \
 		-DUSE_DL_PREFIX \
diff --git a/libc/bionic/clearenv.c b/libc/bionic/clearenv.c
index ffc58d9..0f6f066 100644
--- a/libc/bionic/clearenv.c
+++ b/libc/bionic/clearenv.c
@@ -26,14 +26,17 @@
  * SUCH DAMAGE.
  */
 
+#include <stddef.h>
+
 extern char** environ;
 
 int clearenv(void)
 {
-	char **P = environ;
-	int offset;
+    char **P = environ;
 
-	for (P = &environ[offset]; *P; ++P)
-		*P = 0;
-        return 0;
+    if (P != NULL) {
+        for (; *P; ++P)
+            *P = NULL;
+    }
+    return 0;
 }
diff --git a/libc/bionic/cpuacct.c b/libc/bionic/cpuacct.c
index abdbc51..7317073 100644
--- a/libc/bionic/cpuacct.c
+++ b/libc/bionic/cpuacct.c
@@ -29,7 +29,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <sys/stat.h>
-//#include <sys/types.h>
+#include "cpuacct.h"
 
 int cpuacct_add(uid_t uid)
 {
diff --git a/libc/bionic/fork.c b/libc/bionic/fork.c
index e20f548..79b8fff 100644
--- a/libc/bionic/fork.c
+++ b/libc/bionic/fork.c
@@ -27,6 +27,7 @@
  */
 #include <unistd.h>
 #include "pthread_internal.h"
+#include "cpuacct.h"
 
 extern int  __fork(void);
 
diff --git a/libc/bionic/fts.c b/libc/bionic/fts.c
index 3dcfb28..c7770b6 100644
--- a/libc/bionic/fts.c
+++ b/libc/bionic/fts.c
@@ -548,9 +548,9 @@
 	DIR *dirp;
 	void *oldaddr;
 	size_t len, maxlen;
-	int nitems, cderrno, descend, level, nlinks, nostat, doadjust;
+	int nitems, cderrno, descend, level, nlinks, nostat = 0, doadjust;
 	int saved_errno;
-	char *cp;
+	char *cp = NULL;
 
 	/* Set current node pointer. */
 	cur = sp->fts_cur;
diff --git a/libc/bionic/pthread.c b/libc/bionic/pthread.c
index 8eee136..061cce1 100644
--- a/libc/bionic/pthread.c
+++ b/libc/bionic/pthread.c
@@ -48,6 +48,7 @@
 #include <sys/prctl.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <stdio.h>
 
 extern int  __pthread_clone(int (*fn)(void*), void *child_stack, int flags, void *arg);
 extern void _exit_with_stack_teardown(void * stackBase, int stackSize, int retCode);
diff --git a/libc/bionic/system_properties.c b/libc/bionic/system_properties.c
index 5e3b9e7..767baa3 100644
--- a/libc/bionic/system_properties.c
+++ b/libc/bionic/system_properties.c
@@ -126,7 +126,7 @@
     for(;;) {
         serial = pi->serial;
         while(SERIAL_DIRTY(serial)) {
-            __futex_wait(&pi->serial, serial, 0);
+            __futex_wait((volatile void *)&pi->serial, serial, 0);
             serial = pi->serial;
         }
         len = SERIAL_VALUE_LEN(serial);
@@ -164,7 +164,7 @@
     } else {
         n = pi->serial;
         do {
-            __futex_wait(&pi->serial, n, 0);
+            __futex_wait((volatile void *)&pi->serial, n, 0);
         } while(n == pi->serial);
     }
     return 0;
diff --git a/libc/docs/CHANGES.TXT b/libc/docs/CHANGES.TXT
index 0eae752..83703d8 100644
--- a/libc/docs/CHANGES.TXT
+++ b/libc/docs/CHANGES.TXT
@@ -46,6 +46,19 @@
         point. We need better multi-byte support code, and wprintf/wscanf
         stuff too.
 
+- <inttypes.h>: add missing declarations for strntoimax abd strntoumax.
+
+- <stdlib.h>: add missing declarations for drand48() and erand48().
+
+- clearerr(): fix broken implementation.
+
+- Feature test macros like _POSIX_C_SOURCE / _XOPEN_SOURCE / _C99_SOURCE
+  are now handled correctly by our C library headers (see <sys/cdefs.h>)
+
+- <sys/select.h>: add missing declaration for pselect()
+
+- 
+
 -------------------------------------------------------------------------------
 Differences between Android 2.2. and Android 2.1:
 
diff --git a/libc/include/inttypes.h b/libc/include/inttypes.h
index ca303cb..81d2315 100644
--- a/libc/include/inttypes.h
+++ b/libc/include/inttypes.h
@@ -253,6 +253,9 @@
 imaxdiv_t	imaxdiv(intmax_t, intmax_t);
 intmax_t	strtoimax(const char *, char **, int);
 uintmax_t	strtoumax(const char *, char **, int);
+
+intmax_t	strntoimax(const char *nptr, char **endptr, int base, size_t n);
+uintmax_t	strntoumax(const char *nptr, char **endptr, int base, size_t n);
 __END_DECLS
 
 #endif /* _INTTYPES_H_ */
diff --git a/libc/include/limits.h b/libc/include/limits.h
index c204e4d..1de8ea6 100644
--- a/libc/include/limits.h
+++ b/libc/include/limits.h
@@ -86,7 +86,7 @@
 #include <sys/limits.h>
 
 #if __POSIX_VISIBLE
-#include <arch/syslimits.h>
+#include <sys/syslimits.h>
 #endif
 
 #ifndef PAGESIZE
diff --git a/libc/include/stdio.h b/libc/include/stdio.h
index f0e103e..d164e95 100644
--- a/libc/include/stdio.h
+++ b/libc/include/stdio.h
@@ -305,7 +305,7 @@
 FILE	*fdopen(int, const char *);
 int	 fileno(FILE *);
 
-#if (__POSIX_VISIBLE >= 199209) || 1 /* ANDROID: Bionic does include this */
+#if (__POSIX_VISIBLE >= 199209)
 int	 pclose(FILE *);
 FILE	*popen(const char *, const char *);
 #endif
diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h
index 5a991ac..f889159 100644
--- a/libc/include/stdlib.h
+++ b/libc/include/stdlib.h
@@ -107,6 +107,8 @@
 extern long nrand48(unsigned short *);
 extern long lrand48(void);
 extern unsigned short *seed48(unsigned short*);
+extern double erand48(unsigned short xsubi[3]);
+extern double drand48(void);
 extern void srand48(long);
 extern unsigned int arc4random(void);
 extern void arc4random_stir(void);
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index de1dc09..849e2b8 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -37,12 +37,6 @@
 #ifndef	_SYS_CDEFS_H_
 #define	_SYS_CDEFS_H_
 
-
-/* our implementation of wchar_t is only 8-bit - die die non-portable code */
-#undef  __WCHAR_TYPE__
-#define __WCHAR_TYPE__  unsigned char
-
-
 /*
  * Macro to test if we're using a GNU C compiler of a specific vintage
  * or later, for e.g. features that appeared in a particular version
@@ -62,11 +56,6 @@
 #define	__GNUC_PREREQ__(x, y)	0
 #endif
 
-//XXX #include <machine/cdefs.h>
-
-/* BIONIC: simpler definition */
-#define __BSD_VISIBLE   1
-
 #include <sys/cdefs_elf.h>
 
 #if defined(__cplusplus)
@@ -378,6 +367,134 @@
  */
 #define	__FBSDID(s)	struct __hack
 
+/*-
+ * The following definitions are an extension of the behavior originally
+ * implemented in <sys/_posix.h>, but with a different level of granularity.
+ * POSIX.1 requires that the macros we test be defined before any standard
+ * header file is included.
+ *
+ * Here's a quick run-down of the versions:
+ *  defined(_POSIX_SOURCE)		1003.1-1988
+ *  _POSIX_C_SOURCE == 1		1003.1-1990
+ *  _POSIX_C_SOURCE == 2		1003.2-1992 C Language Binding Option
+ *  _POSIX_C_SOURCE == 199309		1003.1b-1993
+ *  _POSIX_C_SOURCE == 199506		1003.1c-1995, 1003.1i-1995,
+ *					and the omnibus ISO/IEC 9945-1: 1996
+ *  _POSIX_C_SOURCE == 200112		1003.1-2001
+ *  _POSIX_C_SOURCE == 200809		1003.1-2008
+ *
+ * In addition, the X/Open Portability Guide, which is now the Single UNIX
+ * Specification, defines a feature-test macro which indicates the version of
+ * that specification, and which subsumes _POSIX_C_SOURCE.
+ *
+ * Our macros begin with two underscores to avoid namespace screwage.
+ */
+
+/* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1. */
+#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 1
+#undef _POSIX_C_SOURCE		/* Probably illegal, but beyond caring now. */
+#define	_POSIX_C_SOURCE		199009
+#endif
+
+/* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2. */
+#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 2
+#undef _POSIX_C_SOURCE
+#define	_POSIX_C_SOURCE		199209
+#endif
+
+/* Deal with various X/Open Portability Guides and Single UNIX Spec. */
+#ifdef _XOPEN_SOURCE
+#if _XOPEN_SOURCE - 0 >= 700
+#define	__XSI_VISIBLE		700
+#undef _POSIX_C_SOURCE
+#define	_POSIX_C_SOURCE		200809
+#elif _XOPEN_SOURCE - 0 >= 600
+#define	__XSI_VISIBLE		600
+#undef _POSIX_C_SOURCE
+#define	_POSIX_C_SOURCE		200112
+#elif _XOPEN_SOURCE - 0 >= 500
+#define	__XSI_VISIBLE		500
+#undef _POSIX_C_SOURCE
+#define	_POSIX_C_SOURCE		199506
+#endif
+#endif
+
+/*
+ * Deal with all versions of POSIX.  The ordering relative to the tests above is
+ * important.
+ */
+#if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
+#define	_POSIX_C_SOURCE		198808
+#endif
+#ifdef _POSIX_C_SOURCE
+#if _POSIX_C_SOURCE >= 200809
+#define	__POSIX_VISIBLE		200809
+#define	__ISO_C_VISIBLE		1999
+#elif _POSIX_C_SOURCE >= 200112
+#define	__POSIX_VISIBLE		200112
+#define	__ISO_C_VISIBLE		1999
+#elif _POSIX_C_SOURCE >= 199506
+#define	__POSIX_VISIBLE		199506
+#define	__ISO_C_VISIBLE		1990
+#elif _POSIX_C_SOURCE >= 199309
+#define	__POSIX_VISIBLE		199309
+#define	__ISO_C_VISIBLE		1990
+#elif _POSIX_C_SOURCE >= 199209
+#define	__POSIX_VISIBLE		199209
+#define	__ISO_C_VISIBLE		1990
+#elif _POSIX_C_SOURCE >= 199009
+#define	__POSIX_VISIBLE		199009
+#define	__ISO_C_VISIBLE		1990
+#else
+#define	__POSIX_VISIBLE		198808
+#define	__ISO_C_VISIBLE		0
+#endif /* _POSIX_C_SOURCE */
+#else
+/*-
+ * Deal with _ANSI_SOURCE:
+ * If it is defined, and no other compilation environment is explicitly
+ * requested, then define our internal feature-test macros to zero.  This
+ * makes no difference to the preprocessor (undefined symbols in preprocessing
+ * expressions are defined to have value zero), but makes it more convenient for
+ * a test program to print out the values.
+ *
+ * If a program mistakenly defines _ANSI_SOURCE and some other macro such as
+ * _POSIX_C_SOURCE, we will assume that it wants the broader compilation
+ * environment (and in fact we will never get here).
+ */
+#if defined(_ANSI_SOURCE)	/* Hide almost everything. */
+#define	__POSIX_VISIBLE		0
+#define	__XSI_VISIBLE		0
+#define	__BSD_VISIBLE		0
+#define	__ISO_C_VISIBLE		1990
+#elif defined(_C99_SOURCE)	/* Localism to specify strict C99 env. */
+#define	__POSIX_VISIBLE		0
+#define	__XSI_VISIBLE		0
+#define	__BSD_VISIBLE		0
+#define	__ISO_C_VISIBLE		1999
+#else				/* Default environment: show everything. */
+#define	__POSIX_VISIBLE		200809
+#define	__XSI_VISIBLE		700
+#define	__BSD_VISIBLE		1
+#define	__ISO_C_VISIBLE		1999
+#endif
+#endif
+
+/*
+ * Default values.
+ */
+#ifndef __XPG_VISIBLE
+# define __XPG_VISIBLE          700
+#endif
+#ifndef __POSIX_VISIBLE
+# define __POSIX_VISIBLE        200809
+#endif
+#ifndef __ISO_C_VISIBLE
+# define __ISO_C_VISIBLE        1999
+#endif
+#ifndef __BSD_VISIBLE
+# define __BSD_VISIBLE          1
+#endif
 
 #define  __BIONIC__   1
 
diff --git a/libc/include/sys/select.h b/libc/include/sys/select.h
index 52315b9..9d11ee8 100644
--- a/libc/include/sys/select.h
+++ b/libc/include/sys/select.h
@@ -31,12 +31,15 @@
 #include <sys/cdefs.h>
 #include <sys/time.h>
 #include <sys/types.h>
+#include <signal.h>
 
 __BEGIN_DECLS
 
 typedef __kernel_fd_set   fd_set;
 
 extern int select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
+extern int pselect(int n, fd_set *readfds, fd_set *writefds, fd_set *errfds,
+                   const struct timespec *timeout, const sigset_t *sigmask);
 
 __END_DECLS
 
diff --git a/libc/include/sys/types.h b/libc/include/sys/types.h
index b071ee9..33fe30e 100644
--- a/libc/include/sys/types.h
+++ b/libc/include/sys/types.h
@@ -62,8 +62,10 @@
 typedef __kernel_key_t       key_t;
 typedef __kernel_mode_t      mode_t;
 typedef __kernel_nlink_t	 nlink_t;
+#ifndef _OFF_T_DEFINED_
 #define _OFF_T_DEFINED_
 typedef __kernel_off_t       off_t;
+#endif
 typedef __kernel_loff_t      loff_t;
 typedef loff_t               off64_t;  /* GLibc-specific */
 
diff --git a/libc/include/wchar.h b/libc/include/wchar.h
index df456ef..9b744a5 100644
--- a/libc/include/wchar.h
+++ b/libc/include/wchar.h
@@ -72,7 +72,7 @@
 
 #define  WCHAR_MAX   INT_MAX
 #define  WCHAR_MIN   INT_MIN
-#define  WEOF        ((wchar_t)(-1))
+#define  WEOF        ((wint_t)(-1))
 
 extern wint_t            btowc(int);
 extern int               fwprintf(FILE *, const wchar_t *, ...);
diff --git a/libc/netbsd/isc/ev_timers.c b/libc/netbsd/isc/ev_timers.c
index 9674687..a584f99 100644
--- a/libc/netbsd/isc/ev_timers.c
+++ b/libc/netbsd/isc/ev_timers.c
@@ -33,6 +33,7 @@
 /* Import. */
 
 #include <errno.h>
+#include <time.h>
 
 #include <isc/assertions.h>
 #include <isc/eventlib.h>
diff --git a/libc/netbsd/nameser/ns_ttl.c b/libc/netbsd/nameser/ns_ttl.c
index 0878194..cc98331 100644
--- a/libc/netbsd/nameser/ns_ttl.c
+++ b/libc/netbsd/nameser/ns_ttl.c
@@ -47,7 +47,7 @@
 
 /* Macros. */
 
-#define T(x) if ((x) < 0) return (-1); else
+#define T(x) do { if ((x) < 0) return (-1); } while(0)
 
 /* Public. */
 
diff --git a/libc/netbsd/net/base64.c b/libc/netbsd/net/base64.c
index 70caaf7..7270703 100644
--- a/libc/netbsd/net/base64.c
+++ b/libc/netbsd/net/base64.c
@@ -141,7 +141,7 @@
 	size_t targsize;
 {
 	size_t datalength = 0;
-	u_char input[3];
+	u_char input[3] = { 0, 0, 0 };  /* make compiler happy */
 	u_char output[4];
 	size_t i;
 
diff --git a/libc/netbsd/net/getaddrinfo.c b/libc/netbsd/net/getaddrinfo.c
index 51079ae..e7564c4 100644
--- a/libc/netbsd/net/getaddrinfo.c
+++ b/libc/netbsd/net/getaddrinfo.c
@@ -100,6 +100,12 @@
 #include <stdarg.h>
 #include "nsswitch.h"
 
+typedef union sockaddr_union {
+    struct sockaddr     generic;
+    struct sockaddr_in  in;
+    struct sockaddr_in6 in6;
+} sockaddr_union;
+
 #define SUCCESS 0
 #define ANY 0
 #define YES 1
@@ -349,13 +355,14 @@
 		{{{ 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}},
 		/* scope ID */
 		0};
-	static const struct sockaddr *sa_test = (struct sockaddr *) &sin6_test;
+        sockaddr_union addr_test;
+        addr_test.in6 = sin6_test;
 	int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
 	if (s < 0)
 		return 0;
 	int ret;
 	do {
-		ret = connect(s, sa_test, sizeof(sin6_test));
+		ret = connect(s, &addr_test.generic, sizeof(addr_test.in6));
 	} while (ret < 0 && errno == EINTR);
 	int have_ipv6 = (ret == 0);
 	do {
@@ -1261,7 +1268,7 @@
 struct addrinfo_sort_elem {
 	struct addrinfo *ai;
 	int has_src_addr;
-	struct sockaddr_in6 src_addr;  /* Large enough to hold IPv4 or IPv6. */
+	sockaddr_union src_addr;
 	int original_order;
 };
 
@@ -1433,11 +1440,11 @@
 	}
 
 	/* Rule 2: Prefer matching scope. */
-	scope_src1 = _get_scope((const struct sockaddr *)&a1->src_addr);
+	scope_src1 = _get_scope(&a1->src_addr.generic);
 	scope_dst1 = _get_scope(a1->ai->ai_addr);
 	scope_match1 = (scope_src1 == scope_dst1);
 
-	scope_src2 = _get_scope((const struct sockaddr *)&a2->src_addr);
+	scope_src2 = _get_scope(&a2->src_addr.generic);
 	scope_dst2 = _get_scope(a2->ai->ai_addr);
 	scope_match2 = (scope_src2 == scope_dst2);
 
@@ -1456,11 +1463,11 @@
 	 */
 
 	/* Rule 5: Prefer matching label. */
-	label_src1 = _get_label((const struct sockaddr *)&a1->src_addr);
+	label_src1 = _get_label(&a1->src_addr.generic);
 	label_dst1 = _get_label(a1->ai->ai_addr);
 	label_match1 = (label_src1 == label_dst1);
 
-	label_src2 = _get_label((const struct sockaddr *)&a2->src_addr);
+	label_src2 = _get_label(&a2->src_addr.generic);
 	label_dst2 = _get_label(a2->ai->ai_addr);
 	label_match2 = (label_src2 == label_dst2);
 
@@ -1493,9 +1500,9 @@
 	 */
 	if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 &&
 	    a2->has_src_addr && a2->ai->ai_addr->sa_family == AF_INET6) {
-		const struct sockaddr_in6 *a1_src = (const struct sockaddr_in6 *)&a1->src_addr;
+		const struct sockaddr_in6 *a1_src = &a1->src_addr.in6;
 		const struct sockaddr_in6 *a1_dst = (const struct sockaddr_in6 *)a1->ai->ai_addr;
-		const struct sockaddr_in6 *a2_src = (const struct sockaddr_in6 *)&a2->src_addr;
+		const struct sockaddr_in6 *a2_src = &a2->src_addr.in6;
 		const struct sockaddr_in6 *a2_dst = (const struct sockaddr_in6 *)a2->ai->ai_addr;
 		prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
 		prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
@@ -1600,7 +1607,7 @@
 		elems[i].ai = cur;
 		elems[i].original_order = i;
 
-		has_src_addr = _find_src_addr(cur->ai_addr, (struct sockaddr *)&elems[i].src_addr);
+		has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic);
 		if (has_src_addr == -1) {
 			goto error;
 		}
diff --git a/libc/netbsd/net/getnameinfo.c b/libc/netbsd/net/getnameinfo.c
index db04fbf..3666443 100644
--- a/libc/netbsd/net/getnameinfo.c
+++ b/libc/netbsd/net/getnameinfo.c
@@ -339,11 +339,14 @@
 	assert(addr != NULL);
 	assert(host != NULL);
 
+	if (hostlen < 0)
+		return EAI_OVERFLOW;
+
 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
 		return EAI_SYSTEM;
 
 	numaddrlen = strlen(numaddr);
-	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
+	if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
 		return EAI_OVERFLOW;
 	strlcpy(host, numaddr, hostlen);
 
@@ -356,7 +359,7 @@
 		    zonebuf, sizeof(zonebuf), flags);
 		if (zonelen < 0)
 			return EAI_OVERFLOW;
-		if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen)
+		if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
 			return EAI_OVERFLOW;
 		/* construct <numeric-addr><delim><zoneid> */
 		memcpy(host + numaddrlen + 1, zonebuf,
diff --git a/libc/private/cpuacct.h b/libc/private/cpuacct.h
new file mode 100644
index 0000000..8e24c8c
--- /dev/null
+++ b/libc/private/cpuacct.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+#ifndef _BIONIC_CPUACCT_H
+#define _BIONIC_CPUACCT_H
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+__BEGIN_DECLS
+
+extern int cpuacct_add(uid_t uid);
+
+__END_DECLS
+
+#endif /* _BIONIC_CPUACCT_H */
diff --git a/libc/regex/engine.c b/libc/regex/engine.c
index 66be3c7..eae6ff2 100644
--- a/libc/regex/engine.c
+++ b/libc/regex/engine.c
@@ -209,7 +209,7 @@
 			STATETEARDOWN(m);
 			return(REG_ESPACE);
 		}
-		for (i = 1; i <= m->g->nsub; i++)
+		for (i = 1; i <= (int)m->g->nsub; i++)
 			m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
 		if (!g->backrefs && !(m->eflags&REG_BACKR)) {
 			NOTE("dissecting");
@@ -267,8 +267,8 @@
 	}
 	if (nmatch > 1) {
 		assert(m->pmatch != NULL);
-		for (i = 1; i < nmatch; i++)
-			if (i <= m->g->nsub)
+		for (i = 1; i < (ssize_t)nmatch; i++)
+			if (i <= (int)m->g->nsub)
 				pmatch[i] = m->pmatch[i];
 			else {
 				pmatch[i].rm_so = -1;
diff --git a/libc/regex/regcomp.c b/libc/regex/regcomp.c
index 5b632c8..19f4790 100644
--- a/libc/regex/regcomp.c
+++ b/libc/regex/regcomp.c
@@ -249,8 +249,8 @@
 p_ere(struct parse *p, int stop)	/* character this ERE should end at */
 {
 	char c;
-	sopno prevback;
-	sopno prevfwd;
+	sopno prevback = 0;
+	sopno prevfwd = 0;
 	sopno conc;
 	int first = 1;		/* is this the first alternative? */
 
@@ -767,7 +767,7 @@
 p_b_cclass(struct parse *p, cset *cs)
 {
 	char *sp = p->next;
-	struct cclass *cp;
+	const struct cclass *cp;
 	size_t len;
 	char *u;
 	char c;
@@ -831,7 +831,7 @@
     int endc)			/* name ended by endc,']' */
 {
 	char *sp = p->next;
-	struct cname *cp;
+	const struct cname *cp;
 	int len;
 
 	while (MORE() && !SEETWO(endc, ']'))
@@ -1084,7 +1084,7 @@
 	cset *top = &p->g->sets[p->g->ncsets];
 	size_t css = (size_t)p->g->csetsize;
 
-	for (i = 0; i < css; i++)
+	for (i = 0; i < (ssize_t)css; i++)
 		CHsub(cs, i);
 	if (cs == top-1)	/* recover only the easy case */
 		p->g->ncsets--;
@@ -1112,10 +1112,10 @@
 	for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
 		if (cs2->hash == h && cs2 != cs) {
 			/* maybe */
-			for (i = 0; i < css; i++)
+			for (i = 0; i < (ssize_t)css; i++)
 				if (!!CHIN(cs2, i) != !!CHIN(cs, i))
 					break;		/* no */
-			if (i == css)
+			if (i == (ssize_t)css)
 				break;			/* yes */
 		}
 
@@ -1136,7 +1136,7 @@
 	int i;
 	size_t css = (size_t)p->g->csetsize;
 
-	for (i = 0; i < css; i++)
+	for (i = 0; i < (ssize_t)css; i++)
 		if (CHIN(cs, i))
 			return((char)i);
 	assert(never);
@@ -1153,7 +1153,7 @@
 	size_t css = (size_t)p->g->csetsize;
 	int n = 0;
 
-	for (i = 0; i < css; i++)
+	for (i = 0; i < (ssize_t)css; i++)
 		if (CHIN(cs, i))
 			n++;
 	return(n);
@@ -1412,7 +1412,7 @@
 findmust(struct parse *p, struct re_guts *g)
 {
 	sop *scan;
-	sop *start;    /* start initialized in the default case, after that */
+	sop *start = NULL;    /* start initialized in the default case, after that */
 	sop *newstart; /* newstart was initialized in the OCHAR case */
 	sopno newlen;
 	sop s;
diff --git a/libc/regex/regerror.c b/libc/regex/regerror.c
index 894a939..838ec8f 100644
--- a/libc/regex/regerror.c
+++ b/libc/regex/regerror.c
@@ -78,7 +78,7 @@
 size_t
 regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
 {
-	struct rerr *r;
+	const struct rerr *r;
 	size_t len;
 	int target = errcode &~ REG_ITOA;
 	char *s;
@@ -117,7 +117,7 @@
 static char *
 regatoi(const regex_t *preg, char *localbuf, int localbufsize)
 {
-	struct rerr *r;
+	const struct rerr *r;
 
 	for (r = rerrs; r->code != 0; r++)
 		if (strcmp(r->name, preg->re_endp) == 0)
diff --git a/libc/regex/regexec.c b/libc/regex/regexec.c
index 7b3bfc7..6feed3b 100644
--- a/libc/regex/regexec.c
+++ b/libc/regex/regexec.c
@@ -153,7 +153,7 @@
 		return(REG_BADPAT);
 	eflags = GOODFLAGS(eflags);
 
-	if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
+	if (g->nstates <= (int)(CHAR_BIT*sizeof(states1)) && !(eflags&REG_LARGE))
 		return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
 	else
 		return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
diff --git a/libc/stdlib/strntoumax.c b/libc/stdlib/strntoumax.c
index a151ef5..050d718 100644
--- a/libc/stdlib/strntoumax.c
+++ b/libc/stdlib/strntoumax.c
@@ -48,7 +48,7 @@
 uintmax_t
 strntoumax(const char *nptr, char **endptr, int base, size_t n)
 {
-    const unsigned char*  p   = nptr;
+    const unsigned char*  p   = (const unsigned char *)nptr;
     const unsigned char*  end = p + n;
     int                   minus = 0;
     uintmax_t             v = 0;
diff --git a/libc/tzcode/localtime.c b/libc/tzcode/localtime.c
index 83c1011..85a913e 100644
--- a/libc/tzcode/localtime.c
+++ b/libc/tzcode/localtime.c
@@ -389,7 +389,11 @@
     if (TYPE_INTEGRAL(time_t) &&
         TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
             return 0;
+#if SECSPERREPEAT_BITS <= 32  /* to avoid compiler warning (condition is always false) */
         return (t1 - t0) == SECSPERREPEAT;
+#else
+        return 0;
+#endif
 }
 
 static int toint(unsigned char *s) {
diff --git a/libc/unistd/seteuid.c b/libc/unistd/seteuid.c
index dd94932..b3ea372 100644
--- a/libc/unistd/seteuid.c
+++ b/libc/unistd/seteuid.c
@@ -26,6 +26,9 @@
  * SUCH DAMAGE.
  */
 #include <unistd.h>
+#include "cpuacct.h"
+
+extern int __setresuid(uid_t, uid_t, uid_t);
 
 int seteuid(uid_t euid)
 {
diff --git a/libc/unistd/setresuid.c b/libc/unistd/setresuid.c
index 1964881..e62b3e9 100644
--- a/libc/unistd/setresuid.c
+++ b/libc/unistd/setresuid.c
@@ -26,6 +26,9 @@
  * SUCH DAMAGE.
  */
 #include <unistd.h>
+#include "cpuacct.h"
+
+extern int __setresuid(uid_t ruid, uid_t euid, uid_t suid);
 
 int setresuid(uid_t ruid, uid_t euid, uid_t suid)
 {
diff --git a/libc/unistd/setreuid.c b/libc/unistd/setreuid.c
index 04c2826..32e70c8 100644
--- a/libc/unistd/setreuid.c
+++ b/libc/unistd/setreuid.c
@@ -26,6 +26,9 @@
  * SUCH DAMAGE.
  */
 #include <unistd.h>
+#include "cpuacct.h"
+
+extern int __setreuid(uid_t ruid, uid_t euid);
 
 int setreuid(uid_t ruid, uid_t euid)
 {
diff --git a/libc/unistd/setuid.c b/libc/unistd/setuid.c
index 8ab637d..30785d6 100644
--- a/libc/unistd/setuid.c
+++ b/libc/unistd/setuid.c
@@ -26,6 +26,9 @@
  * SUCH DAMAGE.
  */
 #include <unistd.h>
+#include "cpuacct.h"
+
+extern int __setuid(uid_t);
 
 int setuid(uid_t uid)
 {
diff --git a/libc/unistd/sysconf.c b/libc/unistd/sysconf.c
index dedc5bc..27b113c 100644
--- a/libc/unistd/sysconf.c
+++ b/libc/unistd/sysconf.c
@@ -96,7 +96,7 @@
     case _SC_COLL_WEIGHTS_MAX:  return _POSIX2_COLL_WEIGHTS_MASK;
 #endif
 #ifdef _POSIX2_EXPR_NEST_MAX
-    case _SC_EXPR_NEXT_MASK:    return _POSIX2_EXPR_NEST_MAX;
+    case _SC_EXPR_NEST_MAX:    return _POSIX2_EXPR_NEST_MAX;
 #endif
 #ifdef _POSIX2_LINE_MAX
     case _SC_LINE_MAX:          return _POSIX2_LINE_MAX;