fvwrite.c revision 1.4 1 /* $NetBSD: fvwrite.c,v 1.4 1995/02/02 02:09:45 jtc 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 #if defined(LIBC_SCCS) && !defined(lint)
40 #if 0
41 static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93";
42 #endif
43 static char rcsid[] = "$NetBSD: fvwrite.c,v 1.4 1995/02/02 02:09:45 jtc Exp $";
44 #endif /* LIBC_SCCS and not lint */
45
46 #include <stdio.h>
47 #include <string.h>
48 #include "local.h"
49 #include "fvwrite.h"
50
51 /*
52 * Write some memory regions. Return zero on success, EOF on error.
53 *
54 * This routine is large and unsightly, but most of the ugliness due
55 * to the three different kinds of output buffering is handled here.
56 */
57 __sfvwrite(fp, uio)
58 register FILE *fp;
59 register struct __suio *uio;
60 {
61 register size_t len;
62 register char *p;
63 register struct __siov *iov;
64 register int w, s;
65 char *nl;
66 int nlknown, nldist;
67
68 if ((len = uio->uio_resid) == 0)
69 return (0);
70 /* make sure we can write */
71 if (cantwrite(fp))
72 return (EOF);
73
74 #define MIN(a, b) ((a) < (b) ? (a) : (b))
75 #define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
76
77 iov = uio->uio_iov;
78 p = iov->iov_base;
79 len = iov->iov_len;
80 iov++;
81 #define GETIOV(extra_work) \
82 while (len == 0) { \
83 extra_work; \
84 p = iov->iov_base; \
85 len = iov->iov_len; \
86 iov++; \
87 }
88 if (fp->_flags & __SNBF) {
89 /*
90 * Unbuffered: write up to BUFSIZ bytes at a time.
91 */
92 do {
93 GETIOV(;);
94 w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
95 if (w <= 0)
96 goto err;
97 p += w;
98 len -= w;
99 } while ((uio->uio_resid -= w) != 0);
100 } else if ((fp->_flags & __SLBF) == 0) {
101 /*
102 * Fully buffered: fill partially full buffer, if any,
103 * and then flush. If there is no partial buffer, write
104 * one _bf._size byte chunk directly (without copying).
105 *
106 * String output is a special case: write as many bytes
107 * as fit, but pretend we wrote everything. This makes
108 * snprintf() return the number of bytes needed, rather
109 * than the number used, and avoids its write function
110 * (so that the write function can be invalid).
111 */
112 do {
113 GETIOV(;);
114 w = fp->_w;
115 if (fp->_flags & __SSTR) {
116 if (len < w)
117 w = len;
118 COPY(w); /* copy MIN(fp->_w,len), */
119 fp->_w -= w;
120 fp->_p += w;
121 w = len; /* but pretend copied all */
122 } else if (fp->_p > fp->_bf._base && len > w) {
123 /* fill and flush */
124 COPY(w);
125 /* fp->_w -= w; */ /* unneeded */
126 fp->_p += w;
127 if (fflush(fp))
128 goto err;
129 } else if (len >= (w = fp->_bf._size)) {
130 /* write directly */
131 w = (*fp->_write)(fp->_cookie, p, w);
132 if (w <= 0)
133 goto err;
134 } else {
135 /* fill and done */
136 w = len;
137 COPY(w);
138 fp->_w -= w;
139 fp->_p += w;
140 }
141 p += w;
142 len -= w;
143 } while ((uio->uio_resid -= w) != 0);
144 } else {
145 /*
146 * Line buffered: like fully buffered, but we
147 * must check for newlines. Compute the distance
148 * to the first newline (including the newline),
149 * or `infinity' if there is none, then pretend
150 * that the amount to write is MIN(len,nldist).
151 */
152 nlknown = 0;
153 nldist = 0; /* XXX just to keep gcc happy */
154 do {
155 GETIOV(nlknown = 0);
156 if (!nlknown) {
157 nl = memchr((void *)p, '\n', len);
158 nldist = nl ? nl + 1 - p : len + 1;
159 nlknown = 1;
160 }
161 s = MIN(len, nldist);
162 w = fp->_w + fp->_bf._size;
163 if (fp->_p > fp->_bf._base && s > w) {
164 COPY(w);
165 /* fp->_w -= w; */
166 fp->_p += w;
167 if (fflush(fp))
168 goto err;
169 } else if (s >= (w = fp->_bf._size)) {
170 w = (*fp->_write)(fp->_cookie, p, w);
171 if (w <= 0)
172 goto err;
173 } else {
174 w = s;
175 COPY(w);
176 fp->_w -= w;
177 fp->_p += w;
178 }
179 if ((nldist -= w) == 0) {
180 /* copied the newline: flush and forget */
181 if (fflush(fp))
182 goto err;
183 nlknown = 0;
184 }
185 p += w;
186 len -= w;
187 } while ((uio->uio_resid -= w) != 0);
188 }
189 return (0);
190
191 err:
192 fp->_flags |= __SERR;
193 return (EOF);
194 }
195