xref: /minix3/lib/libc/stdio/fvwrite.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: fvwrite.c,v 1.25 2012/03/27 15:05:42 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1990, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Chris Torek.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
332fe8fb19SBen Gras  */
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)fvwrite.c	8.1 (Berkeley) 6/4/93";
392fe8fb19SBen Gras #else
40*f14fb602SLionel Sambuc __RCSID("$NetBSD: fvwrite.c,v 1.25 2012/03/27 15:05:42 christos Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include <assert.h>
45*f14fb602SLionel Sambuc #include <stddef.h>
462fe8fb19SBen Gras #include <errno.h>
472fe8fb19SBen Gras #include <stdio.h>
482fe8fb19SBen Gras #include <stdlib.h>
492fe8fb19SBen Gras #include <string.h>
502fe8fb19SBen Gras #include "reentrant.h"
512fe8fb19SBen Gras #include "local.h"
522fe8fb19SBen Gras #include "fvwrite.h"
532fe8fb19SBen Gras 
542fe8fb19SBen Gras /*
552fe8fb19SBen Gras  * Write some memory regions.  Return zero on success, EOF on error.
562fe8fb19SBen Gras  *
572fe8fb19SBen Gras  * This routine is large and unsightly, but most of the ugliness due
582fe8fb19SBen Gras  * to the three different kinds of output buffering is handled here.
592fe8fb19SBen Gras  */
602fe8fb19SBen Gras int
__sfvwrite(FILE * fp,struct __suio * uio)61*f14fb602SLionel Sambuc __sfvwrite(FILE *fp, struct __suio *uio)
622fe8fb19SBen Gras {
63*f14fb602SLionel Sambuc 	size_t len;
642fe8fb19SBen Gras 	char *p;
652fe8fb19SBen Gras 	struct __siov *iov;
66*f14fb602SLionel Sambuc 	int s;
67*f14fb602SLionel Sambuc 	ssize_t w;
682fe8fb19SBen Gras 	char *nl;
69*f14fb602SLionel Sambuc 	size_t nlknown, nldist;
702fe8fb19SBen Gras 
712fe8fb19SBen Gras 	_DIAGASSERT(fp != NULL);
722fe8fb19SBen Gras 	_DIAGASSERT(uio != NULL);
732fe8fb19SBen Gras 
74*f14fb602SLionel Sambuc 	if ((ssize_t)uio->uio_resid < 0) {
752fe8fb19SBen Gras 		errno = EINVAL;
76*f14fb602SLionel Sambuc 		return EOF;
772fe8fb19SBen Gras 	}
78*f14fb602SLionel Sambuc 	if (uio->uio_resid == 0)
79*f14fb602SLionel Sambuc 		return 0;
802fe8fb19SBen Gras 	/* make sure we can write */
812fe8fb19SBen Gras 	if (cantwrite(fp)) {
822fe8fb19SBen Gras 		errno = EBADF;
83*f14fb602SLionel Sambuc 		return EOF;
842fe8fb19SBen Gras 	}
852fe8fb19SBen Gras 
862fe8fb19SBen Gras #define	MIN(a, b) ((a) < (b) ? (a) : (b))
872fe8fb19SBen Gras #define	COPY(n)	  (void)memcpy(fp->_p, p, (size_t)(n))
882fe8fb19SBen Gras 
892fe8fb19SBen Gras 	iov = uio->uio_iov;
902fe8fb19SBen Gras 	p = iov->iov_base;
912fe8fb19SBen Gras 	len = iov->iov_len;
922fe8fb19SBen Gras 	iov++;
932fe8fb19SBen Gras #define GETIOV(extra_work) \
942fe8fb19SBen Gras 	while (len == 0) { \
952fe8fb19SBen Gras 		extra_work; \
962fe8fb19SBen Gras 		p = iov->iov_base; \
972fe8fb19SBen Gras 		len = iov->iov_len; \
982fe8fb19SBen Gras 		iov++; \
992fe8fb19SBen Gras 	}
1002fe8fb19SBen Gras 	if (fp->_flags & __SNBF) {
1012fe8fb19SBen Gras 		/*
1022fe8fb19SBen Gras 		 * Unbuffered: write up to BUFSIZ bytes at a time.
1032fe8fb19SBen Gras 		 */
1042fe8fb19SBen Gras 		do {
1052fe8fb19SBen Gras 			GETIOV(;);
106*f14fb602SLionel Sambuc 			w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
1072fe8fb19SBen Gras 			if (w <= 0)
1082fe8fb19SBen Gras 				goto err;
1092fe8fb19SBen Gras 			p += w;
1102fe8fb19SBen Gras 			len -= w;
1112fe8fb19SBen Gras 		} while ((uio->uio_resid -= w) != 0);
1122fe8fb19SBen Gras 	} else if ((fp->_flags & __SLBF) == 0) {
1132fe8fb19SBen Gras 		/*
1142fe8fb19SBen Gras 		 * Fully buffered: fill partially full buffer, if any,
1152fe8fb19SBen Gras 		 * and then flush.  If there is no partial buffer, write
1162fe8fb19SBen Gras 		 * one _bf._size byte chunk directly (without copying).
1172fe8fb19SBen Gras 		 *
1182fe8fb19SBen Gras 		 * String output is a special case: write as many bytes
1192fe8fb19SBen Gras 		 * as fit, but pretend we wrote everything.  This makes
1202fe8fb19SBen Gras 		 * snprintf() return the number of bytes needed, rather
1212fe8fb19SBen Gras 		 * than the number used, and avoids its write function
1222fe8fb19SBen Gras 		 * (so that the write function can be invalid).
1232fe8fb19SBen Gras 		 */
1242fe8fb19SBen Gras 		do {
1252fe8fb19SBen Gras 			GETIOV(;);
1262fe8fb19SBen Gras 			if ((fp->_flags & (__SALC | __SSTR)) ==
127*f14fb602SLionel Sambuc 			    (__SALC | __SSTR) && (size_t)fp->_w < len) {
128*f14fb602SLionel Sambuc 				ptrdiff_t blen = fp->_p - fp->_bf._base;
1292fe8fb19SBen Gras 				unsigned char *_base;
1302fe8fb19SBen Gras 				int _size;
1312fe8fb19SBen Gras 
1322fe8fb19SBen Gras 				/* Allocate space exponentially. */
1332fe8fb19SBen Gras 				_size = fp->_bf._size;
1342fe8fb19SBen Gras 				do {
1352fe8fb19SBen Gras 					_size = (_size << 1) + 1;
136*f14fb602SLionel Sambuc 				} while ((size_t)_size < blen + len);
1372fe8fb19SBen Gras 				_base = realloc(fp->_bf._base,
1382fe8fb19SBen Gras 				    (size_t)(_size + 1));
1392fe8fb19SBen Gras 				if (_base == NULL)
1402fe8fb19SBen Gras 					goto err;
1412fe8fb19SBen Gras 				fp->_w += _size - fp->_bf._size;
1422fe8fb19SBen Gras 				fp->_bf._base = _base;
1432fe8fb19SBen Gras 				fp->_bf._size = _size;
1442fe8fb19SBen Gras 				fp->_p = _base + blen;
1452fe8fb19SBen Gras 			}
1462fe8fb19SBen Gras 			w = fp->_w;
1472fe8fb19SBen Gras 			if (fp->_flags & __SSTR) {
148*f14fb602SLionel Sambuc 				if (len < (size_t)w)
1492fe8fb19SBen Gras 					w = len;
1502fe8fb19SBen Gras 				COPY(w);	/* copy MIN(fp->_w,len), */
151*f14fb602SLionel Sambuc 				fp->_w -= (int)w;
1522fe8fb19SBen Gras 				fp->_p += w;
1532fe8fb19SBen Gras 				w = len;	/* but pretend copied all */
154*f14fb602SLionel Sambuc 			} else if (fp->_p > fp->_bf._base && len > (size_t)w) {
1552fe8fb19SBen Gras 				/* fill and flush */
1562fe8fb19SBen Gras 				COPY(w);
1572fe8fb19SBen Gras 				/* fp->_w -= w; */ /* unneeded */
1582fe8fb19SBen Gras 				fp->_p += w;
1592fe8fb19SBen Gras 				if (fflush(fp))
1602fe8fb19SBen Gras 					goto err;
161*f14fb602SLionel Sambuc 			} else if (len >= (size_t)(w = fp->_bf._size)) {
1622fe8fb19SBen Gras 				/* write directly */
163*f14fb602SLionel Sambuc 				w = (*fp->_write)(fp->_cookie, p, (size_t)w);
1642fe8fb19SBen Gras 				if (w <= 0)
1652fe8fb19SBen Gras 					goto err;
1662fe8fb19SBen Gras 			} else {
1672fe8fb19SBen Gras 				/* fill and done */
1682fe8fb19SBen Gras 				w = len;
1692fe8fb19SBen Gras 				COPY(w);
170*f14fb602SLionel Sambuc 				fp->_w -= (int)w;
1712fe8fb19SBen Gras 				fp->_p += w;
1722fe8fb19SBen Gras 			}
1732fe8fb19SBen Gras 			p += w;
1742fe8fb19SBen Gras 			len -= w;
1752fe8fb19SBen Gras 		} while ((uio->uio_resid -= w) != 0);
1762fe8fb19SBen Gras 	} else {
1772fe8fb19SBen Gras 		/*
1782fe8fb19SBen Gras 		 * Line buffered: like fully buffered, but we
1792fe8fb19SBen Gras 		 * must check for newlines.  Compute the distance
1802fe8fb19SBen Gras 		 * to the first newline (including the newline),
1812fe8fb19SBen Gras 		 * or `infinity' if there is none, then pretend
1822fe8fb19SBen Gras 		 * that the amount to write is MIN(len,nldist).
1832fe8fb19SBen Gras 		 */
1842fe8fb19SBen Gras 		nlknown = 0;
1852fe8fb19SBen Gras 		nldist = 0;	/* XXX just to keep gcc happy */
1862fe8fb19SBen Gras 		do {
1872fe8fb19SBen Gras 			GETIOV(nlknown = 0);
1882fe8fb19SBen Gras 			if (!nlknown) {
189*f14fb602SLionel Sambuc 				nl = memchr(p, '\n', len);
190*f14fb602SLionel Sambuc 				nldist = nl ? (size_t)(nl + 1 - p) : len + 1;
1912fe8fb19SBen Gras 				nlknown = 1;
1922fe8fb19SBen Gras 			}
193*f14fb602SLionel Sambuc 			s = (int)MIN(len, nldist);
1942fe8fb19SBen Gras 			w = fp->_w + fp->_bf._size;
1952fe8fb19SBen Gras 			if (fp->_p > fp->_bf._base && s > w) {
1962fe8fb19SBen Gras 				COPY(w);
1972fe8fb19SBen Gras 				/* fp->_w -= w; */
1982fe8fb19SBen Gras 				fp->_p += w;
1992fe8fb19SBen Gras 				if (fflush(fp))
2002fe8fb19SBen Gras 					goto err;
2012fe8fb19SBen Gras 			} else if (s >= (w = fp->_bf._size)) {
202*f14fb602SLionel Sambuc 				w = (*fp->_write)(fp->_cookie, p, (size_t)w);
2032fe8fb19SBen Gras 				if (w <= 0)
2042fe8fb19SBen Gras 				 	goto err;
2052fe8fb19SBen Gras 			} else {
2062fe8fb19SBen Gras 				w = s;
2072fe8fb19SBen Gras 				COPY(w);
208*f14fb602SLionel Sambuc 				fp->_w -= (int)w;
2092fe8fb19SBen Gras 				fp->_p += w;
2102fe8fb19SBen Gras 			}
2112fe8fb19SBen Gras 			if ((nldist -= w) == 0) {
2122fe8fb19SBen Gras 				/* copied the newline: flush and forget */
2132fe8fb19SBen Gras 				if (fflush(fp))
2142fe8fb19SBen Gras 					goto err;
2152fe8fb19SBen Gras 				nlknown = 0;
2162fe8fb19SBen Gras 			}
2172fe8fb19SBen Gras 			p += w;
2182fe8fb19SBen Gras 			len -= w;
2192fe8fb19SBen Gras 		} while ((uio->uio_resid -= w) != 0);
2202fe8fb19SBen Gras 	}
221*f14fb602SLionel Sambuc 	return 0;
2222fe8fb19SBen Gras 
2232fe8fb19SBen Gras err:
2242fe8fb19SBen Gras 	fp->_flags |= __SERR;
225*f14fb602SLionel Sambuc 	return EOF;
2262fe8fb19SBen Gras }
227