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