Home | History | Annotate | Line # | Download | only in stdio
fvwrite.c revision 1.10
      1 /*	$NetBSD: fvwrite.c,v 1.10 1998/10/15 07:10:38 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Chris Torek.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 #if 0
     42 static char sccsid[] = "@(#)fvwrite.c	8.1 (Berkeley) 6/4/93";
     43 #else
     44 __RCSID("$NetBSD: fvwrite.c,v 1.10 1998/10/15 07:10:38 mycroft Exp $");
     45 #endif
     46 #endif /* LIBC_SCCS and not lint */
     47 
     48 #include <errno.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include "local.h"
     53 #include "fvwrite.h"
     54 
     55 /*
     56  * Write some memory regions.  Return zero on success, EOF on error.
     57  *
     58  * This routine is large and unsightly, but most of the ugliness due
     59  * to the three different kinds of output buffering is handled here.
     60  */
     61 int
     62 __sfvwrite(fp, uio)
     63 	FILE *fp;
     64 	struct __suio *uio;
     65 {
     66 	size_t len;
     67 	char *p;
     68 	struct __siov *iov;
     69 	int w, s;
     70 	char *nl;
     71 	int nlknown, nldist;
     72 
     73 	if ((len = uio->uio_resid) == 0)
     74 		return (0);
     75 	/* make sure we can write */
     76 	if (cantwrite(fp)) {
     77 		errno = EBADF;
     78 		return (EOF);
     79 	}
     80 
     81 #define	MIN(a, b) ((a) < (b) ? (a) : (b))
     82 #define	COPY(n)	  (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
     83 
     84 	iov = uio->uio_iov;
     85 	p = iov->iov_base;
     86 	len = iov->iov_len;
     87 	iov++;
     88 #define GETIOV(extra_work) \
     89 	while (len == 0) { \
     90 		extra_work; \
     91 		p = iov->iov_base; \
     92 		len = iov->iov_len; \
     93 		iov++; \
     94 	}
     95 	if (fp->_flags & __SNBF) {
     96 		/*
     97 		 * Unbuffered: write up to BUFSIZ bytes at a time.
     98 		 */
     99 		do {
    100 			GETIOV(;);
    101 			w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
    102 			if (w <= 0)
    103 				goto err;
    104 			p += w;
    105 			len -= w;
    106 		} while ((uio->uio_resid -= w) != 0);
    107 	} else if ((fp->_flags & __SLBF) == 0) {
    108 		/*
    109 		 * Fully buffered: fill partially full buffer, if any,
    110 		 * and then flush.  If there is no partial buffer, write
    111 		 * one _bf._size byte chunk directly (without copying).
    112 		 *
    113 		 * String output is a special case: write as many bytes
    114 		 * as fit, but pretend we wrote everything.  This makes
    115 		 * snprintf() return the number of bytes needed, rather
    116 		 * than the number used, and avoids its write function
    117 		 * (so that the write function can be invalid).
    118 		 */
    119 		do {
    120 			GETIOV(;);
    121 			if ((fp->_flags & (__SALC | __SSTR)) ==
    122 			    (__SALC | __SSTR) && fp->_w < len) {
    123 				size_t blen = fp->_p - fp->_bf._base;
    124 				unsigned char *_base;
    125 				int _size;
    126 
    127 				/* Allocate space exponentially. */
    128 				_size = fp->_bf._size;
    129 				do {
    130 					_size = (_size << 1) + 1;
    131 				} while (_size < blen + len);
    132 				_base = realloc(fp->_bf._base, _size + 1);
    133 				if (_base == NULL)
    134 					goto err;
    135 				fp->_w += _size - fp->_bf._size;
    136 				fp->_bf._base = _base;
    137 				fp->_bf._size = _size;
    138 				fp->_p = _base + blen;
    139 			}
    140 			w = fp->_w;
    141 			if (fp->_flags & __SSTR) {
    142 				if (len < w)
    143 					w = len;
    144 				COPY(w);	/* copy MIN(fp->_w,len), */
    145 				fp->_w -= w;
    146 				fp->_p += w;
    147 				w = len;	/* but pretend copied all */
    148 			} else if (fp->_p > fp->_bf._base && len > w) {
    149 				/* fill and flush */
    150 				COPY(w);
    151 				/* fp->_w -= w; */ /* unneeded */
    152 				fp->_p += w;
    153 				if (fflush(fp))
    154 					goto err;
    155 			} else if (len >= (w = fp->_bf._size)) {
    156 				/* write directly */
    157 				w = (*fp->_write)(fp->_cookie, p, w);
    158 				if (w <= 0)
    159 					goto err;
    160 			} else {
    161 				/* fill and done */
    162 				w = len;
    163 				COPY(w);
    164 				fp->_w -= w;
    165 				fp->_p += w;
    166 			}
    167 			p += w;
    168 			len -= w;
    169 		} while ((uio->uio_resid -= w) != 0);
    170 	} else {
    171 		/*
    172 		 * Line buffered: like fully buffered, but we
    173 		 * must check for newlines.  Compute the distance
    174 		 * to the first newline (including the newline),
    175 		 * or `infinity' if there is none, then pretend
    176 		 * that the amount to write is MIN(len,nldist).
    177 		 */
    178 		nlknown = 0;
    179 		nldist = 0;	/* XXX just to keep gcc happy */
    180 		do {
    181 			GETIOV(nlknown = 0);
    182 			if (!nlknown) {
    183 				nl = memchr((void *)p, '\n', len);
    184 				nldist = nl ? nl + 1 - p : len + 1;
    185 				nlknown = 1;
    186 			}
    187 			s = MIN(len, nldist);
    188 			w = fp->_w + fp->_bf._size;
    189 			if (fp->_p > fp->_bf._base && s > w) {
    190 				COPY(w);
    191 				/* fp->_w -= w; */
    192 				fp->_p += w;
    193 				if (fflush(fp))
    194 					goto err;
    195 			} else if (s >= (w = fp->_bf._size)) {
    196 				w = (*fp->_write)(fp->_cookie, p, w);
    197 				if (w <= 0)
    198 				 	goto err;
    199 			} else {
    200 				w = s;
    201 				COPY(w);
    202 				fp->_w -= w;
    203 				fp->_p += w;
    204 			}
    205 			if ((nldist -= w) == 0) {
    206 				/* copied the newline: flush and forget */
    207 				if (fflush(fp))
    208 					goto err;
    209 				nlknown = 0;
    210 			}
    211 			p += w;
    212 			len -= w;
    213 		} while ((uio->uio_resid -= w) != 0);
    214 	}
    215 	return (0);
    216 
    217 err:
    218 	fp->_flags |= __SERR;
    219 	return (EOF);
    220 }
    221