2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)fvwrite.c 8.1 (Berkeley) 6/4/93
37 * $FreeBSD: src/lib/libc/stdio/fvwrite.c,v 1.10 1999/08/28 00:01:06 peter Exp $
38 * $DragonFly: src/lib/libc/stdio/fvwrite.c,v 1.7 2005/07/23 20:23:06 joerg Exp $
46 #include "priv_stdio.h"
49 * Write some memory regions. Return zero on success, EOF on error.
51 * This routine is large and unsightly, but most of the ugliness due
52 * to the three different kinds of output buffering is handled here.
55 __sfvwrite(FILE *fp, struct __suio *uio)
64 if ((len = uio->uio_resid) == 0)
66 /* make sure we can write */
72 #define MIN(a, b) ((a) < (b) ? (a) : (b))
73 #define COPY(n) (void)memcpy((void *)fp->pub._p, (void *)p, (size_t)(n))
79 #define GETIOV(extra_work) \
86 if (fp->pub._flags & __SNBF) {
88 * Unbuffered: write up to BUFSIZ bytes at a time.
92 w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
97 } while ((uio->uio_resid -= w) != 0);
98 } else if ((fp->pub._flags & __SLBF) == 0) {
100 * Fully buffered: fill partially full buffer, if any,
101 * and then flush. If there is no partial buffer, write
102 * one _bf._size byte chunk directly (without copying).
104 * String output is a special case: write as many bytes
105 * as fit, but pretend we wrote everything. This makes
106 * snprintf() return the number of bytes needed, rather
107 * than the number used, and avoids its write function
108 * (so that the write function can be invalid).
112 if ((fp->pub._flags & (__SALC | __SSTR)) ==
113 (__SALC | __SSTR) && fp->pub._w < len) {
114 size_t blen = fp->pub._p - fp->_bf._base;
117 * Alloc an extra 128 bytes (+ 1 for NULL)
118 * so we don't call realloc(3) so often.
120 fp->pub._w = len + 128;
121 fp->_bf._size = blen + len + 128;
123 reallocf(fp->_bf._base, fp->_bf._size + 1);
124 if (fp->_bf._base == NULL)
126 fp->pub._p = fp->_bf._base + blen;
129 if (fp->pub._flags & __SSTR) {
133 COPY(w); /* copy MIN(fp->_w,len), */
137 w = len; /* but pretend copied all */
138 } else if (fp->pub._p > fp->_bf._base && len > w) {
141 /* fp->_w -= w; */ /* unneeded */
145 } else if (len >= (w = fp->_bf._size)) {
147 w = (*fp->_write)(fp->_cookie, p, w);
159 } while ((uio->uio_resid -= w) != 0);
162 * Line buffered: like fully buffered, but we
163 * must check for newlines. Compute the distance
164 * to the first newline (including the newline),
165 * or `infinity' if there is none, then pretend
166 * that the amount to write is MIN(len,nldist).
169 nldist = 0; /* XXX just to keep gcc happy */
173 nl = memchr((void *)p, '\n', len);
174 nldist = nl ? nl + 1 - p : len + 1;
177 s = MIN(len, nldist);
178 w = fp->pub._w + fp->_bf._size;
179 if (fp->pub._p > fp->_bf._base && s > w) {
185 } else if (s >= (w = fp->_bf._size)) {
186 w = (*fp->_write)(fp->_cookie, p, w);
195 if ((nldist -= w) == 0) {
196 /* copied the newline: flush and forget */
203 } while ((uio->uio_resid -= w) != 0);
208 fp->pub._flags |= __SERR;