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