1 /* $NetBSD: fvwrite.c,v 1.6 1997/05/03 09:01:48 kleink 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 #if defined(LIBC_SCCS) && !defined(lint) 40 #if 0 41 static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93"; 42 #endif 43 static char rcsid[] = "$NetBSD: fvwrite.c,v 1.6 1997/05/03 09:01:48 kleink Exp $"; 44 #endif /* LIBC_SCCS and not lint */ 45 46 #include <errno.h> 47 #include <stdio.h> 48 #include <string.h> 49 #include "local.h" 50 #include "fvwrite.h" 51 52 /* 53 * Write some memory regions. Return zero on success, EOF on error. 54 * 55 * This routine is large and unsightly, but most of the ugliness due 56 * to the three different kinds of output buffering is handled here. 57 */ 58 int 59 __sfvwrite(fp, uio) 60 register FILE *fp; 61 register struct __suio *uio; 62 { 63 register size_t len; 64 register char *p; 65 register struct __siov *iov; 66 register int w, s; 67 char *nl; 68 int nlknown, nldist; 69 70 if ((len = uio->uio_resid) == 0) 71 return (0); 72 /* make sure we can write */ 73 if (cantwrite(fp)) { 74 errno = EBADF; 75 return (EOF); 76 } 77 78 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 79 #define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n)) 80 81 iov = uio->uio_iov; 82 p = iov->iov_base; 83 len = iov->iov_len; 84 iov++; 85 #define GETIOV(extra_work) \ 86 while (len == 0) { \ 87 extra_work; \ 88 p = iov->iov_base; \ 89 len = iov->iov_len; \ 90 iov++; \ 91 } 92 if (fp->_flags & __SNBF) { 93 /* 94 * Unbuffered: write up to BUFSIZ bytes at a time. 95 */ 96 do { 97 GETIOV(;); 98 w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ)); 99 if (w <= 0) 100 goto err; 101 p += w; 102 len -= w; 103 } while ((uio->uio_resid -= w) != 0); 104 } else if ((fp->_flags & __SLBF) == 0) { 105 /* 106 * Fully buffered: fill partially full buffer, if any, 107 * and then flush. If there is no partial buffer, write 108 * one _bf._size byte chunk directly (without copying). 109 * 110 * String output is a special case: write as many bytes 111 * as fit, but pretend we wrote everything. This makes 112 * snprintf() return the number of bytes needed, rather 113 * than the number used, and avoids its write function 114 * (so that the write function can be invalid). 115 */ 116 do { 117 GETIOV(;); 118 w = fp->_w; 119 if (fp->_flags & __SSTR) { 120 if (len < w) 121 w = len; 122 COPY(w); /* copy MIN(fp->_w,len), */ 123 fp->_w -= w; 124 fp->_p += w; 125 w = len; /* but pretend copied all */ 126 } else if (fp->_p > fp->_bf._base && len > w) { 127 /* fill and flush */ 128 COPY(w); 129 /* fp->_w -= w; */ /* unneeded */ 130 fp->_p += w; 131 if (fflush(fp)) 132 goto err; 133 } else if (len >= (w = fp->_bf._size)) { 134 /* write directly */ 135 w = (*fp->_write)(fp->_cookie, p, w); 136 if (w <= 0) 137 goto err; 138 } else { 139 /* fill and done */ 140 w = len; 141 COPY(w); 142 fp->_w -= w; 143 fp->_p += w; 144 } 145 p += w; 146 len -= w; 147 } while ((uio->uio_resid -= w) != 0); 148 } else { 149 /* 150 * Line buffered: like fully buffered, but we 151 * must check for newlines. Compute the distance 152 * to the first newline (including the newline), 153 * or `infinity' if there is none, then pretend 154 * that the amount to write is MIN(len,nldist). 155 */ 156 nlknown = 0; 157 nldist = 0; /* XXX just to keep gcc happy */ 158 do { 159 GETIOV(nlknown = 0); 160 if (!nlknown) { 161 nl = memchr((void *)p, '\n', len); 162 nldist = nl ? nl + 1 - p : len + 1; 163 nlknown = 1; 164 } 165 s = MIN(len, nldist); 166 w = fp->_w + fp->_bf._size; 167 if (fp->_p > fp->_bf._base && s > w) { 168 COPY(w); 169 /* fp->_w -= w; */ 170 fp->_p += w; 171 if (fflush(fp)) 172 goto err; 173 } else if (s >= (w = fp->_bf._size)) { 174 w = (*fp->_write)(fp->_cookie, p, w); 175 if (w <= 0) 176 goto err; 177 } else { 178 w = s; 179 COPY(w); 180 fp->_w -= w; 181 fp->_p += w; 182 } 183 if ((nldist -= w) == 0) { 184 /* copied the newline: flush and forget */ 185 if (fflush(fp)) 186 goto err; 187 nlknown = 0; 188 } 189 p += w; 190 len -= w; 191 } while ((uio->uio_resid -= w) != 0); 192 } 193 return (0); 194 195 err: 196 fp->_flags |= __SERR; 197 return (EOF); 198 } 199