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