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