xref: /freebsd/lib/libc/stdio/fvwrite.c (revision 2357939bc239bd5334a169b62313806178dd8f30)
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 <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include "local.h"
48 #include "fvwrite.h"
49 
50 /*
51  * Write some memory regions.  Return zero on success, EOF on error.
52  *
53  * This routine is large and unsightly, but most of the ugliness due
54  * to the three different kinds of output buffering is handled here.
55  */
56 int
57 __sfvwrite(fp, uio)
58 	FILE *fp;
59 	struct __suio *uio;
60 {
61 	size_t len;
62 	char *p;
63 	struct __siov *iov;
64 	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 		errno = EBADF;
73 		return (EOF);
74 	}
75 
76 #define	MIN(a, b) ((a) < (b) ? (a) : (b))
77 #define	COPY(n)	  (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
78 
79 	iov = uio->uio_iov;
80 	p = iov->iov_base;
81 	len = iov->iov_len;
82 	iov++;
83 #define GETIOV(extra_work) \
84 	while (len == 0) { \
85 		extra_work; \
86 		p = iov->iov_base; \
87 		len = iov->iov_len; \
88 		iov++; \
89 	}
90 	if (fp->_flags & __SNBF) {
91 		/*
92 		 * Unbuffered: write up to BUFSIZ bytes at a time.
93 		 */
94 		do {
95 			GETIOV(;);
96 			w = _swrite(fp, p, MIN(len, BUFSIZ));
97 			if (w <= 0)
98 				goto err;
99 			p += w;
100 			len -= w;
101 		} while ((uio->uio_resid -= w) != 0);
102 	} else if ((fp->_flags & __SLBF) == 0) {
103 		/*
104 		 * Fully buffered: fill partially full buffer, if any,
105 		 * and then flush.  If there is no partial buffer, write
106 		 * one _bf._size byte chunk directly (without copying).
107 		 *
108 		 * String output is a special case: write as many bytes
109 		 * as fit, but pretend we wrote everything.  This makes
110 		 * snprintf() return the number of bytes needed, rather
111 		 * than the number used, and avoids its write function
112 		 * (so that the write function can be invalid).
113 		 */
114 		do {
115 			GETIOV(;);
116 			if ((fp->_flags & (__SALC | __SSTR)) ==
117 			    (__SALC | __SSTR) && fp->_w < len) {
118 				size_t blen = fp->_p - fp->_bf._base;
119 
120 				/*
121 				 * Alloc an extra 128 bytes (+ 1 for NULL)
122 				 * so we don't call realloc(3) so often.
123 				 */
124 				fp->_w = len + 128;
125 				fp->_bf._size = blen + len + 128;
126 				fp->_bf._base =
127 				    reallocf(fp->_bf._base, fp->_bf._size + 1);
128 				if (fp->_bf._base == NULL)
129 					goto err;
130 				fp->_p = fp->_bf._base + blen;
131 			}
132 			w = fp->_w;
133 			if (fp->_flags & __SSTR) {
134 				if (len < w)
135 					w = len;
136 				if (w > 0) {
137 					COPY(w);        /* copy MIN(fp->_w,len), */
138 					fp->_w -= w;
139 					fp->_p += w;
140 				}
141 				w = len;	/* but pretend copied all */
142 			} else if (fp->_p > fp->_bf._base && len > w) {
143 				/* fill and flush */
144 				COPY(w);
145 				/* fp->_w -= w; */ /* unneeded */
146 				fp->_p += w;
147 				if (__fflush(fp))
148 					goto err;
149 			} else if (len >= (w = fp->_bf._size)) {
150 				/* write directly */
151 				w = _swrite(fp, p, w);
152 				if (w <= 0)
153 					goto err;
154 			} else {
155 				/* fill and done */
156 				w = len;
157 				COPY(w);
158 				fp->_w -= w;
159 				fp->_p += w;
160 			}
161 			p += w;
162 			len -= w;
163 		} while ((uio->uio_resid -= w) != 0);
164 	} else {
165 		/*
166 		 * Line buffered: like fully buffered, but we
167 		 * must check for newlines.  Compute the distance
168 		 * to the first newline (including the newline),
169 		 * or `infinity' if there is none, then pretend
170 		 * that the amount to write is MIN(len,nldist).
171 		 */
172 		nlknown = 0;
173 		nldist = 0;	/* XXX just to keep gcc happy */
174 		do {
175 			GETIOV(nlknown = 0);
176 			if (!nlknown) {
177 				nl = memchr((void *)p, '\n', len);
178 				nldist = nl ? nl + 1 - p : len + 1;
179 				nlknown = 1;
180 			}
181 			s = MIN(len, nldist);
182 			w = fp->_w + fp->_bf._size;
183 			if (fp->_p > fp->_bf._base && s > w) {
184 				COPY(w);
185 				/* fp->_w -= w; */
186 				fp->_p += w;
187 				if (__fflush(fp))
188 					goto err;
189 			} else if (s >= (w = fp->_bf._size)) {
190 				w = _swrite(fp, p, w);
191 				if (w <= 0)
192 				 	goto err;
193 			} else {
194 				w = s;
195 				COPY(w);
196 				fp->_w -= w;
197 				fp->_p += w;
198 			}
199 			if ((nldist -= w) == 0) {
200 				/* copied the newline: flush and forget */
201 				if (__fflush(fp))
202 					goto err;
203 				nlknown = 0;
204 			}
205 			p += w;
206 			len -= w;
207 		} while ((uio->uio_resid -= w) != 0);
208 	}
209 	return (0);
210 
211 err:
212 	fp->_flags |= __SERR;
213 	return (EOF);
214 }
215