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