1 | /*-␊ |
2 | * Copyright (c) 1990, 1993␊ |
3 | *␉The Regents of the University of California. All rights reserved.␊ |
4 | *␊ |
5 | * This code is derived from software contributed to Berkeley by␊ |
6 | * Chris Torek.␊ |
7 | *␊ |
8 | * Redistribution and use in source and binary forms, with or without␊ |
9 | * modification, are permitted provided that the following conditions␊ |
10 | * are met:␊ |
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 | * 4. 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.␊ |
19 | *␊ |
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␊ |
30 | * SUCH DAMAGE.␊ |
31 | */␊ |
32 | ␊ |
33 | #if defined(LIBC_SCCS) && !defined(lint)␊ |
34 | static char sccsid[] = "@(#)fvwrite.c␉8.1 (Berkeley) 6/4/93";␊ |
35 | #endif /* LIBC_SCCS and not lint */␊ |
36 | #include <sys/cdefs.h>␊ |
37 | //__FBSDID("$FreeBSD: src/lib/libc/stdio/fvwrite.c,v 1.19 2009/11/25 04:21:42 wollman Exp $");␊ |
38 | ␊ |
39 | #include "stdio.h"␊ |
40 | #include "errno.h"␊ |
41 | #include "local.h"␊ |
42 | #include "fvwrite.h"␊ |
43 | ␊ |
44 | /*␊ |
45 | * Write some memory regions. Return zero on success, EOF on error.␊ |
46 | *␊ |
47 | * This routine is large and unsightly, but most of the ugliness due␊ |
48 | * to the three different kinds of output buffering is handled here.␊ |
49 | */␊ |
50 | int␊ |
51 | __sfvwrite(fp, uio)␊ |
52 | ␉FILE *fp;␊ |
53 | ␉struct __suio *uio;␊ |
54 | {␊ |
55 | ␉size_t len;␊ |
56 | ␉char *p;␊ |
57 | ␉struct __siov *iov;␊ |
58 | ␉int w, s;␊ |
59 | ␉char *nl;␊ |
60 | ␉int nlknown, nldist;␊ |
61 | ␊ |
62 | ␉if (uio->uio_resid == 0)␊ |
63 | ␉␉return (0);␊ |
64 | ␉/* make sure we can write */␊ |
65 | ␉if (prepwrite(fp) != 0)␊ |
66 | ␉␉return (EOF);␊ |
67 | ␊ |
68 | #define␉MIN(a, b) ((a) < (b) ? (a) : (b))␊ |
69 | #define␉COPY(n)␉ (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))␊ |
70 | ␊ |
71 | ␉iov = uio->uio_iov;␊ |
72 | ␉p = iov->iov_base;␊ |
73 | ␉len = iov->iov_len;␊ |
74 | ␉iov++;␊ |
75 | #define GETIOV(extra_work) \␊ |
76 | ␉while (len == 0) { \␊ |
77 | ␉␉extra_work; \␊ |
78 | ␉␉p = iov->iov_base; \␊ |
79 | ␉␉len = iov->iov_len; \␊ |
80 | ␉␉iov++; \␊ |
81 | ␉}␊ |
82 | ␉if (fp->_flags & __SNBF) {␊ |
83 | ␉␉/*␊ |
84 | ␉␉ * Unbuffered: write up to BUFSIZ bytes at a time.␊ |
85 | ␉␉ */␊ |
86 | ␉␉do {␊ |
87 | ␉␉␉GETIOV(;);␊ |
88 | ␉␉␉w = _swrite(fp, p, MIN(len, BUFSIZ));␊ |
89 | ␉␉␉if (w <= 0)␊ |
90 | ␉␉␉␉goto err;␊ |
91 | ␉␉␉p += w;␊ |
92 | ␉␉␉len -= w;␊ |
93 | ␉␉} while ((uio->uio_resid -= w) != 0);␊ |
94 | ␉} else if ((fp->_flags & __SLBF) == 0) {␊ |
95 | ␉␉/*␊ |
96 | ␉␉ * Fully buffered: fill partially full buffer, if any,␊ |
97 | ␉␉ * and then flush. If there is no partial buffer, write␊ |
98 | ␉␉ * one _bf._size byte chunk directly (without copying).␊ |
99 | ␉␉ *␊ |
100 | ␉␉ * String output is a special case: write as many bytes␊ |
101 | ␉␉ * as fit, but pretend we wrote everything. This makes␊ |
102 | ␉␉ * snprintf() return the number of bytes needed, rather␊ |
103 | ␉␉ * than the number used, and avoids its write function␊ |
104 | ␉␉ * (so that the write function can be invalid).␊ |
105 | ␉␉ */␊ |
106 | ␉␉do {␊ |
107 | ␉␉␉GETIOV(;);␊ |
108 | ␉␉␉if ((fp->_flags & (__SALC | __SSTR)) ==␊ |
109 | ␉␉␉ (__SALC | __SSTR) && fp->_w < len) {␊ |
110 | ␉␉␉␉size_t blen = fp->_p - fp->_bf._base;␊ |
111 | ␊ |
112 | ␉␉␉␉/*␊ |
113 | ␉␉␉␉ * Alloc an extra 128 bytes (+ 1 for NULL)␊ |
114 | ␉␉␉␉ * so we don't call realloc(3) so often.␊ |
115 | ␉␉␉␉ */␊ |
116 | ␉␉␉␉fp->_w = len + 128;␊ |
117 | ␉␉␉␉fp->_bf._size = blen + len + 128;␊ |
118 | ␉␉␉␉fp->_bf._base =␊ |
119 | ␉␉␉␉ reallocf(fp->_bf._base, fp->_bf._size + 1);␊ |
120 | ␉␉␉␉if (fp->_bf._base == NULL)␊ |
121 | ␉␉␉␉␉goto err;␊ |
122 | ␉␉␉␉fp->_p = fp->_bf._base + blen;␊ |
123 | ␉␉␉}␊ |
124 | ␉␉␉w = fp->_w;␊ |
125 | ␉␉␉if (fp->_flags & __SSTR) {␊ |
126 | ␉␉␉␉if (len < w)␊ |
127 | ␉␉␉␉␉w = len;␊ |
128 | ␉␉␉␉if (w > 0) {␊ |
129 | ␉␉␉␉␉COPY(w); /* copy MIN(fp->_w,len), */␊ |
130 | ␉␉␉␉␉fp->_w -= w;␊ |
131 | ␉␉␉␉␉fp->_p += w;␊ |
132 | ␉␉␉␉}␊ |
133 | ␉␉␉␉w = len;␉/* but pretend copied all */␊ |
134 | ␉␉␉} else if (fp->_p > fp->_bf._base && len > w) {␊ |
135 | ␉␉␉␉/* fill and flush */␊ |
136 | ␉␉␉␉COPY(w);␊ |
137 | ␉␉␉␉/* fp->_w -= w; */ /* unneeded */␊ |
138 | ␉␉␉␉fp->_p += w;␊ |
139 | ␉␉␉␉if (__fflush(fp))␊ |
140 | ␉␉␉␉␉goto err;␊ |
141 | ␉␉␉} else if (len >= (w = fp->_bf._size)) {␊ |
142 | ␉␉␉␉/* write directly */␊ |
143 | ␉␉␉␉w = _swrite(fp, p, w);␊ |
144 | ␉␉␉␉if (w <= 0)␊ |
145 | ␉␉␉␉␉goto err;␊ |
146 | ␉␉␉} else {␊ |
147 | ␉␉␉␉/* fill and done */␊ |
148 | ␉␉␉␉w = len;␊ |
149 | ␉␉␉␉COPY(w);␊ |
150 | ␉␉␉␉fp->_w -= w;␊ |
151 | ␉␉␉␉fp->_p += w;␊ |
152 | ␉␉␉}␊ |
153 | ␉␉␉p += w;␊ |
154 | ␉␉␉len -= w;␊ |
155 | ␉␉} while ((uio->uio_resid -= w) != 0);␊ |
156 | ␉} else {␊ |
157 | ␉␉/*␊ |
158 | ␉␉ * Line buffered: like fully buffered, but we␊ |
159 | ␉␉ * must check for newlines. Compute the distance␊ |
160 | ␉␉ * to the first newline (including the newline),␊ |
161 | ␉␉ * or `infinity' if there is none, then pretend␊ |
162 | ␉␉ * that the amount to write is MIN(len,nldist).␊ |
163 | ␉␉ */␊ |
164 | ␉␉nlknown = 0;␊ |
165 | ␉␉nldist = 0;␉/* XXX just to keep gcc happy */␊ |
166 | ␉␉do {␊ |
167 | ␉␉␉GETIOV(nlknown = 0);␊ |
168 | ␉␉␉if (!nlknown) {␊ |
169 | ␉␉␉␉nl = memchr((void *)p, '\n', len);␊ |
170 | ␉␉␉␉nldist = nl ? nl + 1 - p : len + 1;␊ |
171 | ␉␉␉␉nlknown = 1;␊ |
172 | ␉␉␉}␊ |
173 | ␉␉␉s = MIN(len, nldist);␊ |
174 | ␉␉␉w = fp->_w + fp->_bf._size;␊ |
175 | ␉␉␉if (fp->_p > fp->_bf._base && s > w) {␊ |
176 | ␉␉␉␉COPY(w);␊ |
177 | ␉␉␉␉/* fp->_w -= w; */␊ |
178 | ␉␉␉␉fp->_p += w;␊ |
179 | ␉␉␉␉if (__fflush(fp))␊ |
180 | ␉␉␉␉␉goto err;␊ |
181 | ␉␉␉} else if (s >= (w = fp->_bf._size)) {␊ |
182 | ␉␉␉␉w = _swrite(fp, p, w);␊ |
183 | ␉␉␉␉if (w <= 0)␊ |
184 | ␉␉␉␉ ␉goto err;␊ |
185 | ␉␉␉} else {␊ |
186 | ␉␉␉␉w = s;␊ |
187 | ␉␉␉␉COPY(w);␊ |
188 | ␉␉␉␉fp->_w -= w;␊ |
189 | ␉␉␉␉fp->_p += w;␊ |
190 | ␉␉␉}␊ |
191 | ␉␉␉if ((nldist -= w) == 0) {␊ |
192 | ␉␉␉␉/* copied the newline: flush and forget */␊ |
193 | ␉␉␉␉if (__fflush(fp))␊ |
194 | ␉␉␉␉␉goto err;␊ |
195 | ␉␉␉␉nlknown = 0;␊ |
196 | ␉␉␉}␊ |
197 | ␉␉␉p += w;␊ |
198 | ␉␉␉len -= w;␊ |
199 | ␉␉} while ((uio->uio_resid -= w) != 0);␊ |
200 | ␉}␊ |
201 | ␉return (0);␊ |
202 | ␊ |
203 | err:␊ |
204 | ␉fp->_flags |= __SERR;␊ |
205 | ␉return (EOF);␊ |
206 | }␊ |
207 | |