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