Loading...
--- Libc/Libc-583/stdio/FreeBSD/fvwrite.c
+++ Libc/Libc-1669.60.4/stdio/FreeBSD/fvwrite.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,8 +34,9 @@
static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/lib/libc/stdio/fvwrite.c,v 1.17 2004/06/08 05:45:48 das Exp $");
-
+__FBSDID("$FreeBSD: src/lib/libc/stdio/fvwrite.c,v 1.19 2009/11/25 04:21:42 wollman Exp $");
+
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -53,9 +50,7 @@
* to the three different kinds of output buffering is handled here.
*/
int
-__sfvwrite(fp, uio)
- FILE *fp;
- struct __suio *uio;
+__sfvwrite(FILE *fp, struct __suio *uio)
{
size_t len;
char *p;
@@ -64,7 +59,7 @@
char *nl;
int nlknown, nldist;
- if ((len = uio->uio_resid) == 0)
+ if (uio->uio_resid == 0)
return (0);
/* make sure we can write */
if (prepwrite(fp) != 0)
@@ -86,11 +81,12 @@
}
if (fp->_flags & __SNBF) {
/*
- * Unbuffered: write up to BUFSIZ bytes at a time.
+ * Unbuffered: write up to INT_MAX bytes at a time, to not truncate
+ * the value of len if it is greater than 2^31 bytes.
*/
do {
GETIOV(;);
- w = _swrite(fp, p, MIN(len, BUFSIZ));
+ w = _swrite(fp, p, MIN(len, INT_MAX));
if (w <= 0)
goto err;
p += w;
@@ -100,7 +96,8 @@
/*
* Fully buffered: fill partially full buffer, if any,
* and then flush. If there is no partial buffer, write
- * one _bf._size byte chunk directly (without copying).
+ * entire payload directly (without copying) up to a multiple of
+ * the buffer size.
*
* String output is a special case: write as many bytes
* as fit, but pretend we wrote everything. This makes
@@ -144,7 +141,12 @@
if (__fflush(fp))
goto err;
} else if (len >= (w = fp->_bf._size)) {
- /* write directly */
+ /* write directly up to INT_MAX or greatest multiple of buffer
+ * size (whatever is smaller), keeping in the memory buffer the
+ * remaining part of payload that is smaller than buffer size.
+ */
+ if (w != 0)
+ w = MIN(w * (len / w), INT_MAX);
w = _swrite(fp, p, w);
if (w <= 0)
goto err;