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/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 *, ...);