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