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