fvwrite.c revision 82838
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[] = 4250476Speter "$FreeBSD: head/lib/libc/stdio/fvwrite.c 82838 2001-09-03 02:24:37Z ache $"; 431573Srgrimes#endif /* LIBC_SCCS and not lint */ 441573Srgrimes 451573Srgrimes#include <stdio.h> 4637487Speter#include <stdlib.h> 471573Srgrimes#include <string.h> 481573Srgrimes#include "local.h" 491573Srgrimes#include "fvwrite.h" 501573Srgrimes 511573Srgrimes/* 521573Srgrimes * Write some memory regions. Return zero on success, EOF on error. 531573Srgrimes * 541573Srgrimes * This routine is large and unsightly, but most of the ugliness due 551573Srgrimes * to the three different kinds of output buffering is handled here. 561573Srgrimes */ 5716586Sjraynardint 5816586Sjraynard__sfvwrite(fp, uio) 591573Srgrimes register FILE *fp; 601573Srgrimes register struct __suio *uio; 611573Srgrimes{ 621573Srgrimes register size_t len; 631573Srgrimes register char *p; 641573Srgrimes register struct __siov *iov; 651573Srgrimes register int w, s; 661573Srgrimes char *nl; 6782838Sache int nlknown, nldist; 681573Srgrimes 691573Srgrimes if ((len = uio->uio_resid) == 0) 701573Srgrimes return (0); 711573Srgrimes /* make sure we can write */ 721573Srgrimes if (cantwrite(fp)) 731573Srgrimes return (EOF); 741573Srgrimes 751573Srgrimes#define MIN(a, b) ((a) < (b) ? (a) : (b)) 761573Srgrimes#define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n)) 771573Srgrimes 781573Srgrimes iov = uio->uio_iov; 791573Srgrimes p = iov->iov_base; 801573Srgrimes len = iov->iov_len; 811573Srgrimes iov++; 821573Srgrimes#define GETIOV(extra_work) \ 831573Srgrimes while (len == 0) { \ 841573Srgrimes extra_work; \ 851573Srgrimes p = iov->iov_base; \ 861573Srgrimes len = iov->iov_len; \ 871573Srgrimes iov++; \ 881573Srgrimes } 891573Srgrimes if (fp->_flags & __SNBF) { 901573Srgrimes /* 911573Srgrimes * Unbuffered: write up to BUFSIZ bytes at a time. 921573Srgrimes */ 931573Srgrimes do { 941573Srgrimes GETIOV(;); 9582838Sache w = _swrite(fp, p, MIN(len, BUFSIZ)); 961573Srgrimes if (w <= 0) 971573Srgrimes goto err; 981573Srgrimes p += w; 991573Srgrimes len -= w; 1001573Srgrimes } while ((uio->uio_resid -= w) != 0); 1011573Srgrimes } else if ((fp->_flags & __SLBF) == 0) { 1021573Srgrimes /* 1031573Srgrimes * Fully buffered: fill partially full buffer, if any, 1041573Srgrimes * and then flush. If there is no partial buffer, write 1051573Srgrimes * one _bf._size byte chunk directly (without copying). 1061573Srgrimes * 1071573Srgrimes * String output is a special case: write as many bytes 10831981Sache * as fit, but pretend we wrote everything. This makes 10931981Sache * snprintf() return the number of bytes needed, rather 11031981Sache * than the number used, and avoids its write function 11131981Sache * (so that the write function can be invalid). 1121573Srgrimes */ 1131573Srgrimes do { 1141573Srgrimes GETIOV(;); 11537487Speter if ((fp->_flags & (__SALC | __SSTR)) == 11637487Speter (__SALC | __SSTR) && fp->_w < len) { 11737487Speter size_t blen = fp->_p - fp->_bf._base; 11837487Speter 11937487Speter /* 12037487Speter * Alloc an extra 128 bytes (+ 1 for NULL) 12137487Speter * so we don't call realloc(3) so often. 12237487Speter */ 12337487Speter fp->_w = len + 128; 12437487Speter fp->_bf._size = blen + len + 128; 12537487Speter fp->_bf._base = 12639327Simp reallocf(fp->_bf._base, fp->_bf._size + 1); 12737487Speter if (fp->_bf._base == NULL) 12837487Speter goto err; 12937487Speter fp->_p = fp->_bf._base + blen; 13037487Speter } 1311573Srgrimes w = fp->_w; 1321573Srgrimes if (fp->_flags & __SSTR) { 1331573Srgrimes if (len < w) 1341573Srgrimes w = len; 13531981Sache if (w > 0) { 13631981Sache COPY(w); /* copy MIN(fp->_w,len), */ 13731981Sache fp->_w -= w; 13831981Sache fp->_p += w; 13931981Sache } 1401573Srgrimes w = len; /* but pretend copied all */ 1411573Srgrimes } else if (fp->_p > fp->_bf._base && len > w) { 1421573Srgrimes /* fill and flush */ 1431573Srgrimes COPY(w); 1441573Srgrimes /* fp->_w -= w; */ /* unneeded */ 1451573Srgrimes fp->_p += w; 14671579Sdeischen if (__fflush(fp)) 1471573Srgrimes goto err; 1481573Srgrimes } else if (len >= (w = fp->_bf._size)) { 1491573Srgrimes /* write directly */ 15082838Sache w = _swrite(fp, p, w); 1511573Srgrimes if (w <= 0) 1521573Srgrimes goto err; 1531573Srgrimes } else { 1541573Srgrimes /* fill and done */ 1551573Srgrimes w = len; 1561573Srgrimes COPY(w); 1571573Srgrimes fp->_w -= w; 1581573Srgrimes fp->_p += w; 1591573Srgrimes } 1601573Srgrimes p += w; 1611573Srgrimes len -= w; 1621573Srgrimes } while ((uio->uio_resid -= w) != 0); 1631573Srgrimes } else { 1641573Srgrimes /* 1651573Srgrimes * Line buffered: like fully buffered, but we 1661573Srgrimes * must check for newlines. Compute the distance 1671573Srgrimes * to the first newline (including the newline), 1681573Srgrimes * or `infinity' if there is none, then pretend 1691573Srgrimes * that the amount to write is MIN(len,nldist). 1701573Srgrimes */ 1711573Srgrimes nlknown = 0; 1721573Srgrimes nldist = 0; /* XXX just to keep gcc happy */ 1731573Srgrimes do { 1741573Srgrimes GETIOV(nlknown = 0); 1751573Srgrimes if (!nlknown) { 1761573Srgrimes nl = memchr((void *)p, '\n', len); 1771573Srgrimes nldist = nl ? nl + 1 - p : len + 1; 1781573Srgrimes nlknown = 1; 1791573Srgrimes } 1801573Srgrimes s = MIN(len, nldist); 1811573Srgrimes w = fp->_w + fp->_bf._size; 1821573Srgrimes if (fp->_p > fp->_bf._base && s > w) { 1831573Srgrimes COPY(w); 1841573Srgrimes /* fp->_w -= w; */ 1851573Srgrimes fp->_p += w; 18671579Sdeischen if (__fflush(fp)) 1871573Srgrimes goto err; 1881573Srgrimes } else if (s >= (w = fp->_bf._size)) { 18982838Sache w = _swrite(fp, p, w); 1901573Srgrimes if (w <= 0) 1911573Srgrimes goto err; 1921573Srgrimes } else { 1931573Srgrimes w = s; 1941573Srgrimes COPY(w); 1951573Srgrimes fp->_w -= w; 1961573Srgrimes fp->_p += w; 1971573Srgrimes } 1981573Srgrimes if ((nldist -= w) == 0) { 1991573Srgrimes /* copied the newline: flush and forget */ 20071579Sdeischen if (__fflush(fp)) 2011573Srgrimes goto err; 2021573Srgrimes nlknown = 0; 2031573Srgrimes } 2041573Srgrimes p += w; 2051573Srgrimes len -= w; 2061573Srgrimes } while ((uio->uio_resid -= w) != 0); 2071573Srgrimes } 2081573Srgrimes return (0); 2091573Srgrimes 2101573Srgrimeserr: 2111573Srgrimes fp->_flags |= __SERR; 2121573Srgrimes return (EOF); 2131573Srgrimes} 214