Switch to OpenBSD fopen/fclose.

This means all our stdio implementation is now the OpenBSD implementation.
The only thing we lose is the STDIO_THREAD_LOCK calls but they were no-ops
anyway.

We should probably talk to upstream about this. Either fix the locking or,
preferably, encourage them to move away from this pooling (especially since
there's no eviction policy).

Bug: 17154680
Change-Id: Ie2523e444a7d0965b8d141d57e3e11f6432d5b9a
diff --git a/libc/Android.mk b/libc/Android.mk
index ce06096..fee54a2 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -242,8 +242,6 @@
     upstream-freebsd/lib/libc/gen/ldexp.c \
     upstream-freebsd/lib/libc/gen/sleep.c \
     upstream-freebsd/lib/libc/gen/usleep.c \
-    upstream-freebsd/lib/libc/stdio/fclose.c \
-    upstream-freebsd/lib/libc/stdio/fopen.c \
     upstream-freebsd/lib/libc/stdlib/abs.c \
     upstream-freebsd/lib/libc/stdlib/getopt_long.c \
     upstream-freebsd/lib/libc/stdlib/imaxabs.c \
@@ -388,6 +386,7 @@
     upstream-openbsd/lib/libc/stdio/asprintf.c \
     upstream-openbsd/lib/libc/stdio/clrerr.c \
     upstream-openbsd/lib/libc/stdio/dprintf.c \
+    upstream-openbsd/lib/libc/stdio/fclose.c \
     upstream-openbsd/lib/libc/stdio/fdopen.c \
     upstream-openbsd/lib/libc/stdio/feof.c \
     upstream-openbsd/lib/libc/stdio/ferror.c \
@@ -402,6 +401,7 @@
     upstream-openbsd/lib/libc/stdio/findfp.c \
     upstream-openbsd/lib/libc/stdio/flags.c \
     upstream-openbsd/lib/libc/stdio/fmemopen.c \
+    upstream-openbsd/lib/libc/stdio/fopen.c \
     upstream-openbsd/lib/libc/stdio/fprintf.c \
     upstream-openbsd/lib/libc/stdio/fpurge.c \
     upstream-openbsd/lib/libc/stdio/fputc.c \
diff --git a/libc/upstream-freebsd/android/include/freebsd-compat.h b/libc/upstream-freebsd/android/include/freebsd-compat.h
index b44b94a..7acdf7c 100644
--- a/libc/upstream-freebsd/android/include/freebsd-compat.h
+++ b/libc/upstream-freebsd/android/include/freebsd-compat.h
@@ -38,17 +38,6 @@
 #define __usleep usleep
 
 /* Redirect internal C library calls to the public function. */
-#define _close close
-#define _fcntl fcntl
-#define _fstat fstat
 #define _nanosleep nanosleep
-#define _open open
-
-/* This one is only needed as long as we have a mix of OpenBSD and FreeBSD stdio. */
-#define _sseek __sseek
-
-/* This is in BSD's <stdlib.h>. */
-#include <stdint.h>
-extern uint32_t arc4random_uniform(uint32_t upper_bound);
 
 #endif
diff --git a/libc/upstream-freebsd/lib/libc/stdio/fclose.c b/libc/upstream-openbsd/lib/libc/stdio/fclose.c
similarity index 75%
rename from libc/upstream-freebsd/lib/libc/stdio/fclose.c
rename to libc/upstream-openbsd/lib/libc/stdio/fclose.c
index 5ed8b2c..c72af54 100644
--- a/libc/upstream-freebsd/lib/libc/stdio/fclose.c
+++ b/libc/upstream-openbsd/lib/libc/stdio/fclose.c
@@ -1,3 +1,4 @@
+/*	$OpenBSD: fclose.c,v 1.9 2009/11/09 00:18:27 kurt Exp $ */
 /*-
  * Copyright (c) 1990, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -30,19 +31,9 @@
  * SUCH DAMAGE.
  */
 
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)fclose.c	8.1 (Berkeley) 6/4/93";
-#endif /* LIBC_SCCS and not lint */
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
-
-#include "namespace.h"
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include "un-namespace.h"
-#include <spinlock.h>
-#include "libc_private.h"
 #include "local.h"
 
 int
@@ -55,6 +46,7 @@
 		return (EOF);
 	}
 	FLOCKFILE(fp);
+	WCIO_FREE(fp);
 	r = fp->_flags & __SWR ? __sflush(fp) : 0;
 	if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0)
 		r = EOF;
@@ -64,22 +56,8 @@
 		FREEUB(fp);
 	if (HASLB(fp))
 		FREELB(fp);
-	fp->_file = -1;
 	fp->_r = fp->_w = 0;	/* Mess up if reaccessed. */
-
-	/*
-	 * Lock the spinlock used to protect __sglue list walk in
-	 * __sfp().  The __sfp() uses fp->_flags == 0 test as an
-	 * indication of the unused FILE.
-	 *
-	 * Taking the lock prevents possible compiler or processor
-	 * reordering of the writes performed before the final _flags
-	 * cleanup, making sure that we are done with the FILE before
-	 * it is considered available.
-	 */
-	STDIO_THREAD_LOCK();
 	fp->_flags = 0;		/* Release this FILE for reuse. */
-	STDIO_THREAD_UNLOCK();
 	FUNLOCKFILE(fp);
 	return (r);
 }
diff --git a/libc/upstream-freebsd/lib/libc/stdio/fopen.c b/libc/upstream-openbsd/lib/libc/stdio/fopen.c
similarity index 79%
rename from libc/upstream-freebsd/lib/libc/stdio/fopen.c
rename to libc/upstream-openbsd/lib/libc/stdio/fopen.c
index b08e336..1465052 100644
--- a/libc/upstream-freebsd/lib/libc/stdio/fopen.c
+++ b/libc/upstream-openbsd/lib/libc/stdio/fopen.c
@@ -1,3 +1,4 @@
+/*	$OpenBSD: fopen.c,v 1.7 2008/05/03 18:46:41 chl Exp $ */
 /*-
  * Copyright (c) 1990, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -30,26 +31,17 @@
  * SUCH DAMAGE.
  */
 
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)fopen.c	8.1 (Berkeley) 6/4/93";
-#endif /* LIBC_SCCS and not lint */
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
-
-#include "namespace.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#include <unistd.h>
+#include <limits.h>
 #include <stdio.h>
 #include <errno.h>
-#include <limits.h>
-#include "un-namespace.h"
-
+#include <unistd.h>
 #include "local.h"
 
 FILE *
-fopen(const char * __restrict file, const char * __restrict mode)
+fopen(const char *file, const char *mode)
 {
 	FILE *fp;
 	int f;
@@ -59,23 +51,19 @@
 		return (NULL);
 	if ((fp = __sfp()) == NULL)
 		return (NULL);
-	if ((f = _open(file, oflags, DEFFILEMODE)) < 0) {
+	if ((f = open(file, oflags, DEFFILEMODE)) < 0) {
 		fp->_flags = 0;			/* release */
 		return (NULL);
 	}
-	/*
-	 * File descriptors are a full int, but _file is only a short.
-	 * If we get a valid file descriptor that is greater than
-	 * SHRT_MAX, then the fd will get sign-extended into an
-	 * invalid file descriptor.  Handle this case by failing the
-	 * open.
-	 */
+
+	/* _file is only a short */
 	if (f > SHRT_MAX) {
 		fp->_flags = 0;			/* release */
-		_close(f);
+		close(f);
 		errno = EMFILE;
 		return (NULL);
 	}
+
 	fp->_file = f;
 	fp->_flags = flags;
 	fp->_cookie = fp;
@@ -83,6 +71,7 @@
 	fp->_write = __swrite;
 	fp->_seek = __sseek;
 	fp->_close = __sclose;
+
 	/*
 	 * When opening in append mode, even though we use O_APPEND,
 	 * we need to seek to the end so that ftell() gets the right
@@ -92,6 +81,6 @@
 	 * fseek and ftell.)
 	 */
 	if (oflags & O_APPEND)
-		(void)_sseek(fp, (fpos_t)0, SEEK_END);
+		(void) __sseek((void *)fp, (fpos_t)0, SEEK_END);
 	return (fp);
 }