fvwrite.c revision 31981
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[] = 4231981Sache "$Id: fvwrite.c,v 1.6 1997/12/24 13:17:13 ache Exp $"; 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 10731981Sache * as fit, but pretend we wrote everything. This makes 10831981Sache * snprintf() return the number of bytes needed, rather 10931981Sache * than the number used, and avoids its write function 11031981Sache * (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; 11831981Sache if (w > 0) { 11931981Sache COPY(w); /* copy MIN(fp->_w,len), */ 12031981Sache fp->_w -= w; 12131981Sache fp->_p += w; 12231981Sache } 1231573Srgrimes w = len; /* but pretend copied all */ 1241573Srgrimes } else if (fp->_p > fp->_bf._base && len > w) { 1251573Srgrimes /* fill and flush */ 1261573Srgrimes COPY(w); 1271573Srgrimes /* fp->_w -= w; */ /* unneeded */ 1281573Srgrimes fp->_p += w; 1291573Srgrimes if (fflush(fp)) 1301573Srgrimes goto err; 1311573Srgrimes } else if (len >= (w = fp->_bf._size)) { 1321573Srgrimes /* write directly */ 1331573Srgrimes w = (*fp->_write)(fp->_cookie, p, w); 1341573Srgrimes if (w <= 0) 1351573Srgrimes goto err; 1361573Srgrimes } else { 1371573Srgrimes /* fill and done */ 1381573Srgrimes w = len; 1391573Srgrimes COPY(w); 1401573Srgrimes fp->_w -= w; 1411573Srgrimes fp->_p += w; 1421573Srgrimes } 1431573Srgrimes p += w; 1441573Srgrimes len -= w; 1451573Srgrimes } while ((uio->uio_resid -= w) != 0); 1461573Srgrimes } else { 1471573Srgrimes /* 1481573Srgrimes * Line buffered: like fully buffered, but we 1491573Srgrimes * must check for newlines. Compute the distance 1501573Srgrimes * to the first newline (including the newline), 1511573Srgrimes * or `infinity' if there is none, then pretend 1521573Srgrimes * that the amount to write is MIN(len,nldist). 1531573Srgrimes */ 1541573Srgrimes nlknown = 0; 1551573Srgrimes nldist = 0; /* XXX just to keep gcc happy */ 1561573Srgrimes do { 1571573Srgrimes GETIOV(nlknown = 0); 1581573Srgrimes if (!nlknown) { 1591573Srgrimes nl = memchr((void *)p, '\n', len); 1601573Srgrimes nldist = nl ? nl + 1 - p : len + 1; 1611573Srgrimes nlknown = 1; 1621573Srgrimes } 1631573Srgrimes s = MIN(len, nldist); 1641573Srgrimes w = fp->_w + fp->_bf._size; 1651573Srgrimes if (fp->_p > fp->_bf._base && s > w) { 1661573Srgrimes COPY(w); 1671573Srgrimes /* fp->_w -= w; */ 1681573Srgrimes fp->_p += w; 1691573Srgrimes if (fflush(fp)) 1701573Srgrimes goto err; 1711573Srgrimes } else if (s >= (w = fp->_bf._size)) { 1721573Srgrimes w = (*fp->_write)(fp->_cookie, p, w); 1731573Srgrimes if (w <= 0) 1741573Srgrimes goto err; 1751573Srgrimes } else { 1761573Srgrimes w = s; 1771573Srgrimes COPY(w); 1781573Srgrimes fp->_w -= w; 1791573Srgrimes fp->_p += w; 1801573Srgrimes } 1811573Srgrimes if ((nldist -= w) == 0) { 1821573Srgrimes /* copied the newline: flush and forget */ 1831573Srgrimes if (fflush(fp)) 1841573Srgrimes goto err; 1851573Srgrimes nlknown = 0; 1861573Srgrimes } 1871573Srgrimes p += w; 1881573Srgrimes len -= w; 1891573Srgrimes } while ((uio->uio_resid -= w) != 0); 1901573Srgrimes } 1911573Srgrimes return (0); 1921573Srgrimes 1931573Srgrimeserr: 1941573Srgrimes fp->_flags |= __SERR; 1951573Srgrimes return (EOF); 1961573Srgrimes} 197