fvwrite.c revision 1.9 1 /* $NetBSD: fvwrite.c,v 1.9 1998/08/28 21:33:11 perry Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93";
43 #else
44 __RCSID("$NetBSD: fvwrite.c,v 1.9 1998/08/28 21:33:11 perry Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 #include <errno.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include "local.h"
53 #include "fvwrite.h"
54
55 /*
56 * Write some memory regions. Return zero on success, EOF on error.
57 *
58 * This routine is large and unsightly, but most of the ugliness due
59 * to the three different kinds of output buffering is handled here.
60 */
61 int
62 __sfvwrite(fp, uio)
63 FILE *fp;
64 struct __suio *uio;
65 {
66 size_t len;
67 char *p;
68 struct __siov *iov;
69 int w, s;
70 char *nl;
71 int nlknown, nldist;
72
73 if ((len = uio->uio_resid) == 0)
74 return (0);
75 /* make sure we can write */
76 if (cantwrite(fp)) {
77 errno = EBADF;
78 return (EOF);
79 }
80
81 #define MIN(a, b) ((a) < (b) ? (a) : (b))
82 #define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
83
84 iov = uio->uio_iov;
85 p = iov->iov_base;
86 len = iov->iov_len;
87 iov++;
88 #define GETIOV(extra_work) \
89 while (len == 0) { \
90 extra_work; \
91 p = iov->iov_base; \
92 len = iov->iov_len; \
93 iov++; \
94 }
95 if (fp->_flags & __SNBF) {
96 /*
97 * Unbuffered: write up to BUFSIZ bytes at a time.
98 */
99 do {
100 GETIOV(;);
101 w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
102 if (w <= 0)
103 goto err;
104 p += w;
105 len -= w;
106 } while ((uio->uio_resid -= w) != 0);
107 } else if ((fp->_flags & __SLBF) == 0) {
108 /*
109 * Fully buffered: fill partially full buffer, if any,
110 * and then flush. If there is no partial buffer, write
111 * one _bf._size byte chunk directly (without copying).
112 *
113 * String output is a special case: write as many bytes
114 * as fit, but pretend we wrote everything. This makes
115 * snprintf() return the number of bytes needed, rather
116 * than the number used, and avoids its write function
117 * (so that the write function can be invalid).
118 */
119 do {
120 GETIOV(;);
121 if ((fp->_flags & (__SALC | __SSTR)) ==
122 (__SALC | __SSTR) && fp->_w < len) {
123 size_t blen = fp->_p - fp->_bf._base;
124 unsigned char *_base;
125
126 /*
127 * Alloc an extra 128 bytes (+ 1 for NULL)
128 * so we don't call realloc(3) so often.
129 */
130 fp->_w = len + 128;
131 fp->_bf._size = blen + len + 128;
132 _base = realloc(fp->_bf._base, fp->_bf._size + 1);
133 if (_base == NULL)
134 goto err;
135 fp->_bf._base = _base;
136 fp->_p = fp->_bf._base + blen;
137 }
138 w = fp->_w;
139 if (fp->_flags & __SSTR) {
140 if (len < w)
141 w = len;
142 COPY(w); /* copy MIN(fp->_w,len), */
143 fp->_w -= w;
144 fp->_p += w;
145 w = len; /* but pretend copied all */
146 } else if (fp->_p > fp->_bf._base && len > w) {
147 /* fill and flush */
148 COPY(w);
149 /* fp->_w -= w; */ /* unneeded */
150 fp->_p += w;
151 if (fflush(fp))
152 goto err;
153 } else if (len >= (w = fp->_bf._size)) {
154 /* write directly */
155 w = (*fp->_write)(fp->_cookie, p, w);
156 if (w <= 0)
157 goto err;
158 } else {
159 /* fill and done */
160 w = len;
161 COPY(w);
162 fp->_w -= w;
163 fp->_p += w;
164 }
165 p += w;
166 len -= w;
167 } while ((uio->uio_resid -= w) != 0);
168 } else {
169 /*
170 * Line buffered: like fully buffered, but we
171 * must check for newlines. Compute the distance
172 * to the first newline (including the newline),
173 * or `infinity' if there is none, then pretend
174 * that the amount to write is MIN(len,nldist).
175 */
176 nlknown = 0;
177 nldist = 0; /* XXX just to keep gcc happy */
178 do {
179 GETIOV(nlknown = 0);
180 if (!nlknown) {
181 nl = memchr((void *)p, '\n', len);
182 nldist = nl ? nl + 1 - p : len + 1;
183 nlknown = 1;
184 }
185 s = MIN(len, nldist);
186 w = fp->_w + fp->_bf._size;
187 if (fp->_p > fp->_bf._base && s > w) {
188 COPY(w);
189 /* fp->_w -= w; */
190 fp->_p += w;
191 if (fflush(fp))
192 goto err;
193 } else if (s >= (w = fp->_bf._size)) {
194 w = (*fp->_write)(fp->_cookie, p, w);
195 if (w <= 0)
196 goto err;
197 } else {
198 w = s;
199 COPY(w);
200 fp->_w -= w;
201 fp->_p += w;
202 }
203 if ((nldist -= w) == 0) {
204 /* copied the newline: flush and forget */
205 if (fflush(fp))
206 goto err;
207 nlknown = 0;
208 }
209 p += w;
210 len -= w;
211 } while ((uio->uio_resid -= w) != 0);
212 }
213 return (0);
214
215 err:
216 fp->_flags |= __SERR;
217 return (EOF);
218 }
219