Update contrib.
3 * Portions Copyright (c) 1990-2004 Nokia Corporation and/or its subsidiary(-ies).
7 /* No user fns here. Pesch 15apr92. */
10 * Copyright (c) 1990 The Regents of the University of California.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms are permitted
14 * provided that the above copyright notice and this paragraph are
15 * duplicated in all such forms and that any documentation,
16 * advertising materials, and other materials related to such
17 * distribution and use acknowledge that the software was developed
18 * by the University of California, Berkeley. The name of the
19 * University may not be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31 #define MIN(a, b) ((a) < (b) ? (a) : (b))
32 #define COPY(n) (void) memmove((void *) fp->_p, (void *) p, (size_t) (n))
34 #define GETIOV(extra_work) \
38 p = (const char*)iov->iov_base; \
44 * Write some memory regions. Return zero on success, EOF on error.
46 * This routine is large and unsightly, but most of the ugliness due
47 * to the three different kinds of output buffering is handled here.
51 __sfvwrite (register FILE *fp,register struct __suio *uio)
54 register const char *p;
55 register struct __siov *iov;
58 int nlknown, nldist = -1;
60 if ((len = uio->uio_resid) == 0)
63 /* make sure we can write */
69 if (fp->_flags & __SNBF)
72 * Unbuffered: write up to BUFSIZ bytes at a time.
77 w = (*fp->_write) (fp->_cookie, p, MIN (len, BUFSIZ));
83 while ((uio->uio_resid -= w) != 0);
85 else if ((fp->_flags & __SLBF) == 0)
88 * Fully buffered: fill partially full buffer, if any,
89 * and then flush. If there is no partial buffer, write
90 * one _bf._size byte chunk directly (without copying).
92 * String output is a special case: write as many bytes
93 * as fit, but pretend we wrote everything. This makes
94 * snprintf() return the number of bytes needed, rather
95 * than the number used, and avoids its write function
96 * (so that the write function can be invalid).
102 if (fp->_flags & __SSTR)
106 COPY (w); /* copy MIN(fp->_w,len), */
109 w = len; /* but pretend copied all */
111 else if (fp->_p > fp->_bf._base && (int)len > w)
115 /* fp->_w -= w; *//* unneeded */
120 else if ((int)len >= (w = fp->_bf._size))
123 w = (*fp->_write) (fp->_cookie, p, w);
138 while ((uio->uio_resid -= w) != 0);
143 * Line buffered: like fully buffered, but we
144 * must check for newlines. Compute the distance
145 * to the first newline (including the newline),
146 * or `infinity' if there is none, then pretend
147 * that the amount to write is MIN(len,nldist).
152 GETIOV (nlknown = 0);
155 nl = (char *)memchr ((void *) p, '\n', len);
156 nldist = nl ? nl + 1 - p : len + 1;
159 s = MIN ((int)len, nldist);
160 w = fp->_w + fp->_bf._size;
161 if (fp->_p > fp->_bf._base && s > w)
169 else if (s >= (w = fp->_bf._size))
171 w = (*fp->_write) (fp->_cookie, p, w);
182 if ((nldist -= w) == 0)
184 /* copied the newline: flush and forget */
192 while ((uio->uio_resid -= w) != 0);
197 fp->_flags |= __SERR;