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