2000-02-18 03:39:52 +08:00
|
|
|
/*
|
2006-11-30 05:36:54 +08:00
|
|
|
* Copyright (c) 1990, 2006 The Regents of the University of California.
|
2000-02-18 03:39:52 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the University of California, Berkeley. The name of the
|
|
|
|
* University may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
/* No user fns here. Pesch 15apr92. */
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
#include <_ansi.h>
|
2000-02-18 03:39:52 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2002-07-05 02:56:17 +08:00
|
|
|
#include <stdlib.h>
|
2006-06-15 04:49:11 +08:00
|
|
|
#include <errno.h>
|
2000-02-18 03:39:52 +08:00
|
|
|
#include "local.h"
|
|
|
|
#include "fvwrite.h"
|
|
|
|
|
|
|
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
#define COPY(n) _CAST_VOID memmove ((_PTR) fp->_p, (_PTR) p, (size_t) (n))
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
#define GETIOV(extra_work) \
|
|
|
|
while (len == 0) \
|
|
|
|
{ \
|
|
|
|
extra_work; \
|
|
|
|
p = iov->iov_base; \
|
|
|
|
len = iov->iov_len; \
|
|
|
|
iov++; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write some memory regions. Return zero on success, EOF on error.
|
|
|
|
*
|
|
|
|
* This routine is large and unsightly, but most of the ugliness due
|
|
|
|
* to the three different kinds of output buffering is handled here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2006-06-15 04:49:11 +08:00
|
|
|
_DEFUN(__sfvwrite_r, (ptr, fp, uio),
|
|
|
|
struct _reent *ptr _AND
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
register FILE *fp _AND
|
|
|
|
register struct __suio *uio)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
register size_t len;
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
register _CONST char *p = NULL;
|
2000-02-18 03:39:52 +08:00
|
|
|
register struct __siov *iov;
|
|
|
|
register int w, s;
|
|
|
|
char *nl;
|
|
|
|
int nlknown, nldist;
|
|
|
|
|
|
|
|
if ((len = uio->uio_resid) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* make sure we can write */
|
|
|
|
if (cantwrite (fp))
|
2006-06-15 04:49:11 +08:00
|
|
|
{
|
|
|
|
fp->_flags |= __SERR;
|
|
|
|
ptr->_errno = EBADF;
|
|
|
|
return EOF;
|
|
|
|
}
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
iov = uio->uio_iov;
|
|
|
|
len = 0;
|
2000-05-03 11:57:19 +08:00
|
|
|
|
|
|
|
#ifdef __SCLE
|
|
|
|
if (fp->_flags & __SCLE) /* text mode */
|
|
|
|
{
|
|
|
|
do
|
2002-05-01 02:23:40 +08:00
|
|
|
{
|
|
|
|
GETIOV (;);
|
|
|
|
while (len > 0)
|
|
|
|
{
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
if (putc (*p, fp) == EOF)
|
2002-05-01 02:23:40 +08:00
|
|
|
return EOF;
|
|
|
|
p++;
|
|
|
|
len--;
|
|
|
|
uio->uio_resid--;
|
|
|
|
}
|
|
|
|
}
|
2000-05-03 11:57:19 +08:00
|
|
|
while (uio->uio_resid > 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
if (fp->_flags & __SNBF)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Unbuffered: write up to BUFSIZ bytes at a time.
|
|
|
|
*/
|
|
|
|
do
|
|
|
|
{
|
|
|
|
GETIOV (;);
|
|
|
|
w = (*fp->_write) (fp->_cookie, p, MIN (len, BUFSIZ));
|
|
|
|
if (w <= 0)
|
|
|
|
goto err;
|
|
|
|
p += w;
|
|
|
|
len -= w;
|
|
|
|
}
|
|
|
|
while ((uio->uio_resid -= w) != 0);
|
|
|
|
}
|
|
|
|
else if ((fp->_flags & __SLBF) == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*
|
|
|
|
* String output is a special case: write as many bytes
|
|
|
|
* as fit, but pretend we wrote everything. This makes
|
|
|
|
* snprintf() return the number of bytes needed, rather
|
|
|
|
* than the number used, and avoids its write function
|
2002-07-05 02:56:17 +08:00
|
|
|
* (so that the write function can be invalid). If
|
|
|
|
* we are dealing with the asprintf routines, we will
|
|
|
|
* dynamically increase the buffer size as needed.
|
2000-02-18 03:39:52 +08:00
|
|
|
*/
|
|
|
|
do
|
|
|
|
{
|
|
|
|
GETIOV (;);
|
|
|
|
w = fp->_w;
|
|
|
|
if (fp->_flags & __SSTR)
|
|
|
|
{
|
2006-11-30 05:36:54 +08:00
|
|
|
if (len >= w && fp->_flags & __SMBF)
|
2002-07-05 02:56:17 +08:00
|
|
|
{ /* must be asprintf family */
|
|
|
|
unsigned char *ptr;
|
|
|
|
int curpos = (fp->_p - fp->_bf._base);
|
2006-11-30 05:36:54 +08:00
|
|
|
/* Choose a geometric growth factor to avoid
|
|
|
|
quadratic realloc behavior, but use a rate less
|
|
|
|
than (1+sqrt(5))/2 to accomodate malloc
|
|
|
|
overhead. asprintf EXPECTS us to overallocate, so
|
|
|
|
that it can add a trailing \0 without
|
|
|
|
reallocating. The new allocation should thus be
|
|
|
|
max(prev_size*1.5, curpos+len+1). */
|
|
|
|
int newsize = fp->_bf._size * 3 / 2;
|
|
|
|
if (newsize < curpos + len + 1)
|
|
|
|
newsize = curpos + len + 1;
|
2003-08-23 02:52:25 +08:00
|
|
|
ptr = (unsigned char *)_realloc_r (_REENT,
|
|
|
|
fp->_bf._base,
|
2006-11-30 05:36:54 +08:00
|
|
|
newsize);
|
2002-07-05 02:56:17 +08:00
|
|
|
if (!ptr)
|
2004-07-16 23:47:15 +08:00
|
|
|
{
|
|
|
|
/* Free buffer which is no longer used. */
|
|
|
|
_free_r (_REENT, fp->_bf._base);
|
|
|
|
goto err;
|
|
|
|
}
|
2002-07-05 02:56:17 +08:00
|
|
|
fp->_bf._base = ptr;
|
|
|
|
fp->_p = ptr + curpos;
|
2006-11-30 05:36:54 +08:00
|
|
|
fp->_bf._size = newsize;
|
|
|
|
w = len;
|
|
|
|
fp->_w = newsize - curpos;
|
2002-07-05 02:56:17 +08:00
|
|
|
}
|
2000-02-18 03:39:52 +08:00
|
|
|
if (len < w)
|
|
|
|
w = len;
|
|
|
|
COPY (w); /* copy MIN(fp->_w,len), */
|
|
|
|
fp->_w -= w;
|
|
|
|
fp->_p += w;
|
|
|
|
w = len; /* but pretend copied all */
|
|
|
|
}
|
|
|
|
else if (fp->_p > fp->_bf._base && len > w)
|
|
|
|
{
|
|
|
|
/* fill and flush */
|
|
|
|
COPY (w);
|
|
|
|
/* fp->_w -= w; *//* unneeded */
|
|
|
|
fp->_p += w;
|
|
|
|
if (fflush (fp))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
else if (len >= (w = fp->_bf._size))
|
|
|
|
{
|
|
|
|
/* write directly */
|
|
|
|
w = (*fp->_write) (fp->_cookie, p, w);
|
|
|
|
if (w <= 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* fill and done */
|
|
|
|
w = len;
|
|
|
|
COPY (w);
|
|
|
|
fp->_w -= w;
|
|
|
|
fp->_p += w;
|
|
|
|
}
|
|
|
|
p += w;
|
|
|
|
len -= w;
|
|
|
|
}
|
|
|
|
while ((uio->uio_resid -= w) != 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Line buffered: like fully buffered, but we
|
|
|
|
* must check for newlines. Compute the distance
|
|
|
|
* to the first newline (including the newline),
|
|
|
|
* or `infinity' if there is none, then pretend
|
|
|
|
* that the amount to write is MIN(len,nldist).
|
|
|
|
*/
|
|
|
|
nlknown = 0;
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
nldist = 0;
|
2000-02-18 03:39:52 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
GETIOV (nlknown = 0);
|
|
|
|
if (!nlknown)
|
|
|
|
{
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
nl = memchr ((_PTR) p, '\n', len);
|
2000-02-18 03:39:52 +08:00
|
|
|
nldist = nl ? nl + 1 - p : len + 1;
|
|
|
|
nlknown = 1;
|
|
|
|
}
|
|
|
|
s = MIN (len, nldist);
|
|
|
|
w = fp->_w + fp->_bf._size;
|
|
|
|
if (fp->_p > fp->_bf._base && s > w)
|
|
|
|
{
|
|
|
|
COPY (w);
|
|
|
|
/* fp->_w -= w; */
|
|
|
|
fp->_p += w;
|
|
|
|
if (fflush (fp))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
else if (s >= (w = fp->_bf._size))
|
|
|
|
{
|
|
|
|
w = (*fp->_write) (fp->_cookie, p, w);
|
|
|
|
if (w <= 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
w = s;
|
|
|
|
COPY (w);
|
|
|
|
fp->_w -= w;
|
|
|
|
fp->_p += w;
|
|
|
|
}
|
|
|
|
if ((nldist -= w) == 0)
|
|
|
|
{
|
|
|
|
/* copied the newline: flush and forget */
|
|
|
|
if (fflush (fp))
|
|
|
|
goto err;
|
|
|
|
nlknown = 0;
|
|
|
|
}
|
|
|
|
p += w;
|
|
|
|
len -= w;
|
|
|
|
}
|
|
|
|
while ((uio->uio_resid -= w) != 0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
fp->_flags |= __SERR;
|
|
|
|
return EOF;
|
|
|
|
}
|