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