Loading...
stdio/FreeBSD/makebuf.c Libc-391 Libc-825.26
--- Libc/Libc-391/stdio/FreeBSD/makebuf.c
+++ Libc/Libc-825.26/stdio/FreeBSD/makebuf.c
@@ -13,10 +13,6 @@
  * 2. 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.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *	This product includes software developed by the University of
- *	California, Berkeley and its contributors.
  * 4. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
@@ -38,7 +34,7 @@
 static char sccsid[] = "@(#)makebuf.c	8.1 (Berkeley) 6/4/93";
 #endif /* LIBC_SCCS and not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/lib/libc/stdio/makebuf.c,v 1.4 2002/03/22 21:53:04 obrien Exp $");
+__FBSDID("$FreeBSD: src/lib/libc/stdio/makebuf.c,v 1.6 2007/01/09 00:28:07 imp Exp $");
 
 #include "namespace.h"
 #include <sys/types.h>
@@ -46,8 +42,13 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include "un-namespace.h"
+
+#include "libc_private.h"
 #include "local.h"
-#include "un-namespace.h"
+
+#define MAXBUFSIZE	(1 << 16)
+#define TTYBUFSIZE	4096
 
 /*
  * Allocate a file buffer, or switch to unbuffered I/O.
@@ -71,6 +72,12 @@
 		return;
 	}
 	flags = __swhatbuf(fp, &size, &couldbetty);
+	if (couldbetty && isatty(fp->_file)) {
+		flags |= __SLBF;
+		/* st_blksize for ttys is 128K, so make it more reasonable */
+		if (size > TTYBUFSIZE)
+			fp->_blksize = size = TTYBUFSIZE;
+	}
 	if ((p = malloc(size)) == NULL) {
 		fp->_flags |= __SNBF;
 		fp->_bf._base = fp->_p = fp->_nbuf;
@@ -81,8 +88,6 @@
 	flags |= __SMBF;
 	fp->_bf._base = fp->_p = p;
 	fp->_bf._size = size;
-	if (couldbetty && isatty(fp->_file))
-		flags |= __SLBF;
 	fp->_flags |= flags;
 }
 
@@ -115,8 +120,7 @@
 	 * __sseek is mainly paranoia.)  It is safe to set _blksize
 	 * unconditionally; it will only be used if __SOPT is also set.
 	 */
-	*bufsize = st.st_blksize;
-	fp->_blksize = st.st_blksize;
+	fp->_blksize = *bufsize = st.st_blksize > MAXBUFSIZE ? MAXBUFSIZE : st.st_blksize;
 	return ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek ?
 	    __SOPT : __SNPT);
 }