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