fvwrite.c revision 16586
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1990, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Chris Torek.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. All advertising materials mentioning features or use of this software
171573Srgrimes *    must display the following acknowledgement:
181573Srgrimes *	This product includes software developed by the University of
191573Srgrimes *	California, Berkeley and its contributors.
201573Srgrimes * 4. Neither the name of the University nor the names of its contributors
211573Srgrimes *    may be used to endorse or promote products derived from this software
221573Srgrimes *    without specific prior written permission.
231573Srgrimes *
241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341573Srgrimes * SUCH DAMAGE.
351573Srgrimes */
361573Srgrimes
371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
3816586Sjraynard#if 0
391573Srgrimesstatic char sccsid[] = "@(#)fvwrite.c	8.1 (Berkeley) 6/4/93";
4016586Sjraynard#endif
4116586Sjraynardstatic const char rcsid[] =
4216586Sjraynard		"$Id$";
431573Srgrimes#endif /* LIBC_SCCS and not lint */
441573Srgrimes
451573Srgrimes#include <stdio.h>
461573Srgrimes#include <string.h>
471573Srgrimes#include "local.h"
481573Srgrimes#include "fvwrite.h"
491573Srgrimes
501573Srgrimes/*
511573Srgrimes * Write some memory regions.  Return zero on success, EOF on error.
521573Srgrimes *
531573Srgrimes * This routine is large and unsightly, but most of the ugliness due
541573Srgrimes * to the three different kinds of output buffering is handled here.
551573Srgrimes */
5616586Sjraynardint
5716586Sjraynard__sfvwrite(fp, uio)
581573Srgrimes	register FILE *fp;
591573Srgrimes	register struct __suio *uio;
601573Srgrimes{
611573Srgrimes	register size_t len;
621573Srgrimes	register char *p;
631573Srgrimes	register struct __siov *iov;
641573Srgrimes	register int w, s;
651573Srgrimes	char *nl;
661573Srgrimes	int nlknown, nldist;
671573Srgrimes
681573Srgrimes	if ((len = uio->uio_resid) == 0)
691573Srgrimes		return (0);
701573Srgrimes	/* make sure we can write */
711573Srgrimes	if (cantwrite(fp))
721573Srgrimes		return (EOF);
731573Srgrimes
741573Srgrimes#define	MIN(a, b) ((a) < (b) ? (a) : (b))
751573Srgrimes#define	COPY(n)	  (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
761573Srgrimes
771573Srgrimes	iov = uio->uio_iov;
781573Srgrimes	p = iov->iov_base;
791573Srgrimes	len = iov->iov_len;
801573Srgrimes	iov++;
811573Srgrimes#define GETIOV(extra_work) \
821573Srgrimes	while (len == 0) { \
831573Srgrimes		extra_work; \
841573Srgrimes		p = iov->iov_base; \
851573Srgrimes		len = iov->iov_len; \
861573Srgrimes		iov++; \
871573Srgrimes	}
881573Srgrimes	if (fp->_flags & __SNBF) {
891573Srgrimes		/*
901573Srgrimes		 * Unbuffered: write up to BUFSIZ bytes at a time.
911573Srgrimes		 */
921573Srgrimes		do {
931573Srgrimes			GETIOV(;);
941573Srgrimes			w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
951573Srgrimes			if (w <= 0)
961573Srgrimes				goto err;
971573Srgrimes			p += w;
981573Srgrimes			len -= w;
991573Srgrimes		} while ((uio->uio_resid -= w) != 0);
1001573Srgrimes	} else if ((fp->_flags & __SLBF) == 0) {
1011573Srgrimes		/*
1021573Srgrimes		 * Fully buffered: fill partially full buffer, if any,
1031573Srgrimes		 * and then flush.  If there is no partial buffer, write
1041573Srgrimes		 * one _bf._size byte chunk directly (without copying).
1051573Srgrimes		 *
1061573Srgrimes		 * String output is a special case: write as many bytes
1071573Srgrimes		 * as fit, but pretend we wrote everything.  This makes
1081573Srgrimes		 * snprintf() return the number of bytes needed, rather
1091573Srgrimes		 * than the number used, and avoids its write function
1101573Srgrimes		 * (so that the write function can be invalid).
1111573Srgrimes		 */
1121573Srgrimes		do {
1131573Srgrimes			GETIOV(;);
1141573Srgrimes			w = fp->_w;
1151573Srgrimes			if (fp->_flags & __SSTR) {
1161573Srgrimes				if (len < w)
1171573Srgrimes					w = len;
1181573Srgrimes				COPY(w);	/* copy MIN(fp->_w,len), */
1191573Srgrimes				fp->_w -= w;
1201573Srgrimes				fp->_p += w;
1211573Srgrimes				w = len;	/* but pretend copied all */
1221573Srgrimes			} else if (fp->_p > fp->_bf._base && len > w) {
1231573Srgrimes				/* fill and flush */
1241573Srgrimes				COPY(w);
1251573Srgrimes				/* fp->_w -= w; */ /* unneeded */
1261573Srgrimes				fp->_p += w;
1271573Srgrimes				if (fflush(fp))
1281573Srgrimes					goto err;
1291573Srgrimes			} else if (len >= (w = fp->_bf._size)) {
1301573Srgrimes				/* write directly */
1311573Srgrimes				w = (*fp->_write)(fp->_cookie, p, w);
1321573Srgrimes				if (w <= 0)
1331573Srgrimes					goto err;
1341573Srgrimes			} else {
1351573Srgrimes				/* fill and done */
1361573Srgrimes				w = len;
1371573Srgrimes				COPY(w);
1381573Srgrimes				fp->_w -= w;
1391573Srgrimes				fp->_p += w;
1401573Srgrimes			}
1411573Srgrimes			p += w;
1421573Srgrimes			len -= w;
1431573Srgrimes		} while ((uio->uio_resid -= w) != 0);
1441573Srgrimes	} else {
1451573Srgrimes		/*
1461573Srgrimes		 * Line buffered: like fully buffered, but we
1471573Srgrimes		 * must check for newlines.  Compute the distance
1481573Srgrimes		 * to the first newline (including the newline),
1491573Srgrimes		 * or `infinity' if there is none, then pretend
1501573Srgrimes		 * that the amount to write is MIN(len,nldist).
1511573Srgrimes		 */
1521573Srgrimes		nlknown = 0;
1531573Srgrimes		nldist = 0;	/* XXX just to keep gcc happy */
1541573Srgrimes		do {
1551573Srgrimes			GETIOV(nlknown = 0);
1561573Srgrimes			if (!nlknown) {
1571573Srgrimes				nl = memchr((void *)p, '\n', len);
1581573Srgrimes				nldist = nl ? nl + 1 - p : len + 1;
1591573Srgrimes				nlknown = 1;
1601573Srgrimes			}
1611573Srgrimes			s = MIN(len, nldist);
1621573Srgrimes			w = fp->_w + fp->_bf._size;
1631573Srgrimes			if (fp->_p > fp->_bf._base && s > w) {
1641573Srgrimes				COPY(w);
1651573Srgrimes				/* fp->_w -= w; */
1661573Srgrimes				fp->_p += w;
1671573Srgrimes				if (fflush(fp))
1681573Srgrimes					goto err;
1691573Srgrimes			} else if (s >= (w = fp->_bf._size)) {
1701573Srgrimes				w = (*fp->_write)(fp->_cookie, p, w);
1711573Srgrimes				if (w <= 0)
1721573Srgrimes				 	goto err;
1731573Srgrimes			} else {
1741573Srgrimes				w = s;
1751573Srgrimes				COPY(w);
1761573Srgrimes				fp->_w -= w;
1771573Srgrimes				fp->_p += w;
1781573Srgrimes			}
1791573Srgrimes			if ((nldist -= w) == 0) {
1801573Srgrimes				/* copied the newline: flush and forget */
1811573Srgrimes				if (fflush(fp))
1821573Srgrimes					goto err;
1831573Srgrimes				nlknown = 0;
1841573Srgrimes			}
1851573Srgrimes			p += w;
1861573Srgrimes			len -= w;
1871573Srgrimes		} while ((uio->uio_resid -= w) != 0);
1881573Srgrimes	}
1891573Srgrimes	return (0);
1901573Srgrimes
1911573Srgrimeserr:
1921573Srgrimes	fp->_flags |= __SERR;
1931573Srgrimes	return (EOF);
1941573Srgrimes}
195