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