1 /* $OpenBSD: fvwrite.c,v 1.16 2009/10/22 01:23:16 guenther Exp $ */
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * Write some memory regions. Return zero on success, EOF on error.
45 * This routine is large and unsightly, but most of the ugliness due
46 * to the three different kinds of output buffering is handled here.
49 __sfvwrite(LFILE
*fp
, struct __suio
*uio
)
58 if ((len
= uio
->uio_resid
) == 0)
60 /* make sure we can write */
66 #define MIN(a, b) ((a) < (b) ? (a) : (b))
67 #define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
73 #define GETIOV(extra_work) \
80 if (fp
->_flags
& __SNBF
) {
82 * Unbuffered: write up to BUFSIZ bytes at a time.
86 w
= (*fp
->_write
)(fp
->_cookie
, p
, MIN(len
, BUFSIZ
));
91 } while ((uio
->uio_resid
-= w
) != 0);
92 } else if ((fp
->_flags
& __SLBF
) == 0) {
94 * Fully buffered: fill partially full buffer, if any,
95 * and then flush. If there is no partial buffer, write
96 * one _bf._size byte chunk directly (without copying).
98 * String output is a special case: write as many bytes
99 * as fit, but pretend we wrote everything. This makes
100 * snprintf() return the number of bytes needed, rather
101 * than the number used, and avoids its write function
102 * (so that the write function can be invalid).
106 if ((fp
->_flags
& (__SALC
| __SSTR
)) ==
107 (__SALC
| __SSTR
) && fp
->_w
< len
) {
108 size_t blen
= fp
->_p
- fp
->_bf
._base
;
109 unsigned char *_base
;
112 /* Allocate space exponentially. */
113 _size
= fp
->_bf
._size
;
115 _size
= (_size
<< 1) + 1;
116 } while (_size
< blen
+ len
);
117 _base
= realloc(fp
->_bf
._base
, _size
+ 1);
120 fp
->_w
+= _size
- fp
->_bf
._size
;
121 fp
->_bf
._base
= _base
;
122 fp
->_bf
._size
= _size
;
123 fp
->_p
= _base
+ blen
;
126 if (fp
->_flags
& __SSTR
) {
129 COPY(w
); /* copy MIN(fp->_w,len), */
132 w
= len
; /* but pretend copied all */
133 } else if (fp
->_p
> fp
->_bf
._base
&& len
> w
) {
136 /* fp->_w -= w; */ /* unneeded */
138 if (ust_safe_fflush(fp
))
140 } else if (len
>= (w
= fp
->_bf
._size
)) {
142 w
= (*fp
->_write
)(fp
->_cookie
, p
, w
);
154 } while ((uio
->uio_resid
-= w
) != 0);
157 * Line buffered: like fully buffered, but we
158 * must check for newlines. Compute the distance
159 * to the first newline (including the newline),
160 * or `infinity' if there is none, then pretend
161 * that the amount to write is MIN(len,nldist).
164 nldist
= 0; /* XXX just to keep gcc happy */
168 nl
= memchr((void *)p
, '\n', len
);
169 nldist
= nl
? nl
+ 1 - p
: len
+ 1;
172 s
= MIN(len
, nldist
);
173 w
= fp
->_w
+ fp
->_bf
._size
;
174 if (fp
->_p
> fp
->_bf
._base
&& s
> w
) {
178 if (ust_safe_fflush(fp
))
180 } else if (s
>= (w
= fp
->_bf
._size
)) {
181 w
= (*fp
->_write
)(fp
->_cookie
, p
, w
);
190 if ((nldist
-= w
) == 0) {
191 /* copied the newline: flush and forget */
192 if (ust_safe_fflush(fp
))
198 } while ((uio
->uio_resid
-= w
) != 0);
203 fp
->_flags
|= __SERR
;
This page took 0.038555 seconds and 4 git commands to generate.