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