xref: /openbsd-src/lib/libc/stdio/fvwrite.c (revision b725ae7711052a2233e31a66fefb8a752c388d7a)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Torek.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char rcsid[] = "$OpenBSD: fvwrite.c,v 1.12 2003/06/02 20:18:37 millert Exp $";
35 #endif /* LIBC_SCCS and not lint */
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.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(fp, uio)
52 	register FILE *fp;
53 	register struct __suio *uio;
54 {
55 	register size_t len;
56 	register char *p;
57 	register struct __siov *iov;
58 	register int w, s;
59 	char *nl;
60 	int nlknown, nldist;
61 
62 	if ((len = uio->uio_resid) == 0)
63 		return (0);
64 	/* make sure we can write */
65 	if (cantwrite(fp)) {
66 		errno = EBADF;
67 		return (EOF);
68 	}
69 
70 #define	MIN(a, b) ((a) < (b) ? (a) : (b))
71 #define	COPY(n)	  (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
72 
73 	iov = uio->uio_iov;
74 	p = iov->iov_base;
75 	len = iov->iov_len;
76 	iov++;
77 #define GETIOV(extra_work) \
78 	while (len == 0) { \
79 		extra_work; \
80 		p = iov->iov_base; \
81 		len = iov->iov_len; \
82 		iov++; \
83 	}
84 	if (fp->_flags & __SNBF) {
85 		/*
86 		 * Unbuffered: write up to BUFSIZ bytes at a time.
87 		 */
88 		do {
89 			GETIOV(;);
90 			w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
91 			if (w <= 0)
92 				goto err;
93 			p += w;
94 			len -= w;
95 		} while ((uio->uio_resid -= w) != 0);
96 	} else if ((fp->_flags & __SLBF) == 0) {
97 		/*
98 		 * Fully buffered: fill partially full buffer, if any,
99 		 * and then flush.  If there is no partial buffer, write
100 		 * one _bf._size byte chunk directly (without copying).
101 		 *
102 		 * String output is a special case: write as many bytes
103 		 * as fit, but pretend we wrote everything.  This makes
104 		 * snprintf() return the number of bytes needed, rather
105 		 * than the number used, and avoids its write function
106 		 * (so that the write function can be invalid).
107 		 */
108 		do {
109 			GETIOV(;);
110 			if ((fp->_flags & (__SALC | __SSTR)) ==
111 			    (__SALC | __SSTR) && fp->_w < len) {
112 				size_t blen = fp->_p - fp->_bf._base;
113 				unsigned char *_base;
114 				int _size;
115 
116 				/* Allocate space exponentially. */
117 				_size = fp->_bf._size;
118 				do {
119 					_size = (_size << 1) + 1;
120 				} while (_size < blen + len);
121 				_base = realloc(fp->_bf._base, _size + 1);
122 				if (_base == NULL)
123 					goto err;
124 				fp->_w += _size - fp->_bf._size;
125 				fp->_bf._base = _base;
126 				fp->_bf._size = _size;
127 				fp->_p = _base + blen;
128 			}
129 			w = fp->_w;
130 			if (fp->_flags & __SSTR) {
131 				if (len < w)
132 					w = len;
133 				COPY(w);	/* copy MIN(fp->_w,len), */
134 				fp->_w -= w;
135 				fp->_p += w;
136 				w = len;	/* but pretend copied all */
137 			} else if (fp->_p > fp->_bf._base && len > w) {
138 				/* fill and flush */
139 				COPY(w);
140 				/* fp->_w -= w; */ /* unneeded */
141 				fp->_p += w;
142 				if (fflush(fp))
143 					goto err;
144 			} else if (len >= (w = fp->_bf._size)) {
145 				/* write directly */
146 				w = (*fp->_write)(fp->_cookie, p, w);
147 				if (w <= 0)
148 					goto err;
149 			} else {
150 				/* fill and done */
151 				w = len;
152 				COPY(w);
153 				fp->_w -= w;
154 				fp->_p += w;
155 			}
156 			p += w;
157 			len -= w;
158 		} while ((uio->uio_resid -= w) != 0);
159 	} else {
160 		/*
161 		 * Line buffered: like fully buffered, but we
162 		 * must check for newlines.  Compute the distance
163 		 * to the first newline (including the newline),
164 		 * or `infinity' if there is none, then pretend
165 		 * that the amount to write is MIN(len,nldist).
166 		 */
167 		nlknown = 0;
168 		nldist = 0;	/* XXX just to keep gcc happy */
169 		do {
170 			GETIOV(nlknown = 0);
171 			if (!nlknown) {
172 				nl = memchr((void *)p, '\n', len);
173 				nldist = nl ? nl + 1 - p : len + 1;
174 				nlknown = 1;
175 			}
176 			s = MIN(len, nldist);
177 			w = fp->_w + fp->_bf._size;
178 			if (fp->_p > fp->_bf._base && s > w) {
179 				COPY(w);
180 				/* fp->_w -= w; */
181 				fp->_p += w;
182 				if (fflush(fp))
183 					goto err;
184 			} else if (s >= (w = fp->_bf._size)) {
185 				w = (*fp->_write)(fp->_cookie, p, w);
186 				if (w <= 0)
187 				 	goto err;
188 			} else {
189 				w = s;
190 				COPY(w);
191 				fp->_w -= w;
192 				fp->_p += w;
193 			}
194 			if ((nldist -= w) == 0) {
195 				/* copied the newline: flush and forget */
196 				if (fflush(fp))
197 					goto err;
198 				nlknown = 0;
199 			}
200 			p += w;
201 			len -= w;
202 		} while ((uio->uio_resid -= w) != 0);
203 	}
204 	return (0);
205 
206 err:
207 	fp->_flags |= __SERR;
208 	return (EOF);
209 }
210