Merge "Switch to current upstream stdio makebuf.c and setvbuf.c."
diff --git a/libc/Android.mk b/libc/Android.mk
index 49d93af..04741be 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -26,13 +26,11 @@
 	stdio/ftell.c \
 	stdio/fvwrite.c \
 	stdio/gets.c \
-	stdio/makebuf.c \
 	stdio/mktemp.c \
 	stdio/printf.c \
 	stdio/refill.c \
 	stdio/rewind.c \
 	stdio/scanf.c \
-	stdio/setvbuf.c \
 	stdio/snprintf.c\
 	stdio/sprintf.c \
 	stdio/sscanf.c \
@@ -268,6 +266,7 @@
     upstream-freebsd/lib/libc/stdio/fwrite.c \
     upstream-freebsd/lib/libc/stdio/getc.c \
     upstream-freebsd/lib/libc/stdio/getchar.c \
+    upstream-freebsd/lib/libc/stdio/makebuf.c \
     upstream-freebsd/lib/libc/stdio/putc.c \
     upstream-freebsd/lib/libc/stdio/putchar.c \
     upstream-freebsd/lib/libc/stdio/puts.c \
@@ -276,6 +275,7 @@
     upstream-freebsd/lib/libc/stdio/rget.c \
     upstream-freebsd/lib/libc/stdio/setbuf.c \
     upstream-freebsd/lib/libc/stdio/setbuffer.c \
+    upstream-freebsd/lib/libc/stdio/setvbuf.c \
     upstream-freebsd/lib/libc/stdio/tempnam.c \
     upstream-freebsd/lib/libc/stdio/tmpnam.c \
     upstream-freebsd/lib/libc/stdio/wsetup.c \
diff --git a/libc/upstream-freebsd/freebsd-compat.h b/libc/upstream-freebsd/freebsd-compat.h
index 697ddcb..5298663 100644
--- a/libc/upstream-freebsd/freebsd-compat.h
+++ b/libc/upstream-freebsd/freebsd-compat.h
@@ -21,6 +21,7 @@
 
 #define _close close
 #define _fcntl fcntl
+#define _fstat fstat
 #define _open open
 
 #define _sseek __sseek /* Needed as long as we have a mix of OpenBSD and FreeBSD stdio. */
diff --git a/libc/stdio/makebuf.c b/libc/upstream-freebsd/lib/libc/stdio/makebuf.c
similarity index 86%
rename from libc/stdio/makebuf.c
rename to libc/upstream-freebsd/lib/libc/stdio/makebuf.c
index 362cf05..a92087e 100644
--- a/libc/stdio/makebuf.c
+++ b/libc/upstream-freebsd/lib/libc/stdio/makebuf.c
@@ -1,4 +1,3 @@
-/*	$OpenBSD: makebuf.c,v 1.8 2005/12/28 18:50:22 millert Exp $ */
 /*-
  * Copyright (c) 1990, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -31,11 +30,21 @@
  * SUCH DAMAGE.
  */
 
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)makebuf.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 <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include "un-namespace.h"
+
+#include "libc_private.h"
 #include "local.h"
 
 /*
@@ -43,7 +52,7 @@
  * Per the ANSI C standard, ALL tty devices default to line buffered.
  *
  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
- * optimisation) right after the fstat() that finds the buffer size.
+ * optimisation) right after the _fstat() that finds the buffer size.
  */
 void
 __smakebuf(FILE *fp)
@@ -65,6 +74,7 @@
 		fp->_bf._size = 1;
 		return;
 	}
+	__cleanup = _cleanup;
 	flags |= __SMBF;
 	fp->_bf._base = fp->_p = p;
 	fp->_bf._size = size;
@@ -81,15 +91,15 @@
 {
 	struct stat st;
 
-	if (fp->_file < 0 || fstat(fp->_file, &st) < 0) {
+	if (fp->_file < 0 || _fstat(fp->_file, &st) < 0) {
 		*couldbetty = 0;
 		*bufsize = BUFSIZ;
 		return (__SNPT);
 	}
 
 	/* could be a tty iff it is a character device */
-	*couldbetty = S_ISCHR(st.st_mode);
-	if (st.st_blksize == 0) {
+	*couldbetty = (st.st_mode & S_IFMT) == S_IFCHR;
+	if (st.st_blksize <= 0) {
 		*bufsize = BUFSIZ;
 		return (__SNPT);
 	}
diff --git a/libc/stdio/setvbuf.c b/libc/upstream-freebsd/lib/libc/stdio/setvbuf.c
similarity index 88%
rename from libc/stdio/setvbuf.c
rename to libc/upstream-freebsd/lib/libc/stdio/setvbuf.c
index eb0ec68..d396960 100644
--- a/libc/stdio/setvbuf.c
+++ b/libc/upstream-freebsd/lib/libc/stdio/setvbuf.c
@@ -1,4 +1,3 @@
-/*	$OpenBSD: setvbuf.c,v 1.8 2005/08/08 08:05:36 espie Exp $ */
 /*-
  * Copyright (c) 1990, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -31,16 +30,25 @@
  * SUCH DAMAGE.
  */
 
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)setvbuf.c	8.2 (Berkeley) 11/16/93";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "namespace.h"
 #include <stdio.h>
 #include <stdlib.h>
+#include "un-namespace.h"
 #include "local.h"
+#include "libc_private.h"
 
 /*
  * Set one of the three kinds of buffering, optionally including
  * a buffer.
  */
 int
-setvbuf(FILE *fp, char *buf, int mode, size_t size)
+setvbuf(FILE * __restrict fp, char * __restrict buf, int mode, size_t size)
 {
 	int ret, flags;
 	size_t iosize;
@@ -55,23 +63,22 @@
 		if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0)
 			return (EOF);
 
+	FLOCKFILE(fp);
 	/*
 	 * Write current buffer, if any.  Discard unread input (including
 	 * ungetc data), cancel line buffering, and free old buffer if
 	 * malloc()ed.  We also clear any eof condition, as if this were
 	 * a seek.
 	 */
-	FLOCKFILE(fp);
 	ret = 0;
 	(void)__sflush(fp);
 	if (HASUB(fp))
 		FREEUB(fp);
-	WCIO_FREE(fp);
 	fp->_r = fp->_lbfsize = 0;
 	flags = fp->_flags;
 	if (flags & __SMBF)
 		free((void *)fp->_bf._base);
-	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF);
+	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SOFF | __SNPT | __SEOF);
 
 	/* If setting unbuffered mode, skip all the hard work. */
 	if (mode == _IONBF)
@@ -124,7 +131,8 @@
 		flags |= __SNPT;
 
 	/*
-	 * Fix up the FILE fields.
+	 * Fix up the FILE fields, and set __cleanup for output flush on
+	 * exit (since we are buffered in some way).
 	 */
 	if (mode == _IOLBF)
 		flags |= __SLBF;
@@ -146,7 +154,8 @@
 		/* begin/continue reading, or stay in intermediate state */
 		fp->_w = 0;
 	}
-	FUNLOCKFILE(fp);
+	__cleanup = _cleanup;
 
+	FUNLOCKFILE(fp);
 	return (ret);
 }