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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)fvwrite.c 8.1 (Berkeley) 6/4/93
33 * $FreeBSD: head/lib/libc/stdio/fvwrite.c 249810 2013-04-23 14:36:44Z emaste $
42 * Write some memory regions. Return zero on success, EOF on error.
44 * This routine is large and unsightly, but most of the ugliness due
45 * to the three different kinds of output buffering is handled here.
48 __sfvwrite(FILE *fp
, struct __suio
*uio
)
57 if (uio
->uio_resid
== 0)
59 /* make sure we can write */
60 if (prepwrite(fp
) != 0)
63 #define MIN(a, b) ((a) < (b) ? (a) : (b))
64 #define COPY(n) (void)memcpy((void *)fp->pub._p, (void *)p, (size_t)(n))
70 #define GETIOV(extra_work) \
77 if (fp
->pub
._flags
& __SNBF
) {
79 * Unbuffered: write up to BUFSIZ bytes at a time.
83 w
= _swrite(fp
, p
, MIN(len
, BUFSIZ
));
88 } while ((uio
->uio_resid
-= w
) != 0);
89 } else if ((fp
->pub
._flags
& __SLBF
) == 0) {
91 * Fully buffered: fill partially full buffer, if any,
92 * and then flush. If there is no partial buffer, write
93 * one _bf._size byte chunk directly (without copying).
95 * String output is a special case: write as many bytes
96 * as fit, but pretend we wrote everything. This makes
97 * snprintf() return the number of bytes needed, rather
98 * than the number used, and avoids its write function
99 * (so that the write function can be invalid).
103 if ((fp
->pub
._flags
& (__SALC
| __SSTR
)) ==
104 (__SALC
| __SSTR
) && fp
->pub
._w
< len
) {
105 size_t blen
= fp
->pub
._p
- fp
->_bf
._base
;
108 * Alloc an extra 128 bytes (+ 1 for NULL)
109 * so we don't call realloc(3) so often.
111 fp
->pub
._w
= len
+ 128;
112 fp
->_bf
._size
= blen
+ len
+ 128;
114 reallocf(fp
->_bf
._base
, fp
->_bf
._size
+ 1);
115 if (fp
->_bf
._base
== NULL
)
117 fp
->pub
._p
= fp
->_bf
._base
+ blen
;
120 if (fp
->pub
._flags
& __SSTR
) {
124 COPY(w
); /* copy MIN(fp->pub._w,len), */
128 w
= len
; /* but pretend copied all */
129 } else if (fp
->pub
._p
> fp
->_bf
._base
&& len
> w
) {
132 /* fp->pub._w -= w; */ /* unneeded */
136 } else if (len
>= (w
= fp
->_bf
._size
)) {
138 w
= _swrite(fp
, p
, w
);
150 } while ((uio
->uio_resid
-= w
) != 0);
153 * Line buffered: like fully buffered, but we
154 * must check for newlines. Compute the distance
155 * to the first newline (including the newline),
156 * or `infinity' if there is none, then pretend
157 * that the amount to write is MIN(len,nldist).
160 nldist
= 0; /* XXX just to keep gcc happy */
164 nl
= memchr((void *)p
, '\n', len
);
165 nldist
= nl
? nl
+ 1 - p
: len
+ 1;
168 s
= MIN(len
, nldist
);
169 w
= fp
->pub
._w
+ fp
->_bf
._size
;
170 if (fp
->pub
._p
> fp
->_bf
._base
&& s
> w
) {
172 /* fp->pub._w -= w; */
176 } else if (s
>= (w
= fp
->_bf
._size
)) {
177 w
= _swrite(fp
, p
, w
);
186 if ((nldist
-= w
) == 0) {
187 /* copied the newline: flush and forget */
194 } while ((uio
->uio_resid
-= w
) != 0);
199 fp
->pub
._flags
|= __SERR
;