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