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