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