1 /* $NetBSD: fvwrite.c,v 1.25 2012/03/27 15:05:42 christos Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93";
40 __RCSID("$NetBSD: fvwrite.c,v 1.25 2012/03/27 15:05:42 christos Exp $");
42 #endif /* LIBC_SCCS and not lint */
50 #include "reentrant.h"
55 * Write some memory regions. Return zero on success, EOF on error.
57 * This routine is large and unsightly, but most of the ugliness due
58 * to the three different kinds of output buffering is handled here.
61 __sfvwrite(FILE *fp
, struct __suio
*uio
)
69 size_t nlknown
, nldist
;
71 _DIAGASSERT(fp
!= NULL
);
72 _DIAGASSERT(uio
!= NULL
);
74 if ((ssize_t
)uio
->uio_resid
< 0) {
78 if (uio
->uio_resid
== 0)
80 /* make sure we can write */
86 #define MIN(a, b) ((a) < (b) ? (a) : (b))
87 #define COPY(n) (void)memcpy(fp->_p, p, (size_t)(n))
93 #define GETIOV(extra_work) \
100 if (fp
->_flags
& __SNBF
) {
102 * Unbuffered: write up to BUFSIZ bytes at a time.
106 w
= (*fp
->_write
)(fp
->_cookie
, p
, MIN(len
, BUFSIZ
));
111 } while ((uio
->uio_resid
-= w
) != 0);
112 } else if ((fp
->_flags
& __SLBF
) == 0) {
114 * Fully buffered: fill partially full buffer, if any,
115 * and then flush. If there is no partial buffer, write
116 * one _bf._size byte chunk directly (without copying).
118 * String output is a special case: write as many bytes
119 * as fit, but pretend we wrote everything. This makes
120 * snprintf() return the number of bytes needed, rather
121 * than the number used, and avoids its write function
122 * (so that the write function can be invalid).
126 if ((fp
->_flags
& (__SALC
| __SSTR
)) ==
127 (__SALC
| __SSTR
) && (size_t)fp
->_w
< len
) {
128 ptrdiff_t blen
= fp
->_p
- fp
->_bf
._base
;
129 unsigned char *_base
;
132 /* Allocate space exponentially. */
133 _size
= fp
->_bf
._size
;
135 _size
= (_size
<< 1) + 1;
136 } while ((size_t)_size
< blen
+ len
);
137 _base
= realloc(fp
->_bf
._base
,
138 (size_t)(_size
+ 1));
141 fp
->_w
+= _size
- fp
->_bf
._size
;
142 fp
->_bf
._base
= _base
;
143 fp
->_bf
._size
= _size
;
144 fp
->_p
= _base
+ blen
;
147 if (fp
->_flags
& __SSTR
) {
150 COPY(w
); /* copy MIN(fp->_w,len), */
153 w
= len
; /* but pretend copied all */
154 } else if (fp
->_p
> fp
->_bf
._base
&& len
> (size_t)w
) {
157 /* fp->_w -= w; */ /* unneeded */
161 } else if (len
>= (size_t)(w
= fp
->_bf
._size
)) {
163 w
= (*fp
->_write
)(fp
->_cookie
, p
, (size_t)w
);
175 } while ((uio
->uio_resid
-= w
) != 0);
178 * Line buffered: like fully buffered, but we
179 * must check for newlines. Compute the distance
180 * to the first newline (including the newline),
181 * or `infinity' if there is none, then pretend
182 * that the amount to write is MIN(len,nldist).
185 nldist
= 0; /* XXX just to keep gcc happy */
189 nl
= memchr(p
, '\n', len
);
190 nldist
= nl
? (size_t)(nl
+ 1 - p
) : len
+ 1;
193 s
= (int)MIN(len
, nldist
);
194 w
= fp
->_w
+ fp
->_bf
._size
;
195 if (fp
->_p
> fp
->_bf
._base
&& s
> w
) {
201 } else if (s
>= (w
= fp
->_bf
._size
)) {
202 w
= (*fp
->_write
)(fp
->_cookie
, p
, (size_t)w
);
211 if ((nldist
-= w
) == 0) {
212 /* copied the newline: flush and forget */
219 } while ((uio
->uio_resid
-= w
) != 0);
224 fp
->_flags
|= __SERR
;