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