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