1 /* $NetBSD: setvbuf.c,v 1.7 1995/02/02 02:10:34 jtc 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[] = "@(#)setvbuf.c 8.2 (Berkeley) 11/16/93"; 42 #endif 43 static char rcsid[] = "$NetBSD: setvbuf.c,v 1.7 1995/02/02 02:10:34 jtc Exp $"; 44 #endif /* LIBC_SCCS and not lint */ 45 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include "local.h" 49 50 /* 51 * Set one of the three kinds of buffering, optionally including 52 * a buffer. 53 */ 54 setvbuf(fp, buf, mode, size) 55 register FILE *fp; 56 char *buf; 57 register int mode; 58 register size_t size; 59 { 60 register int ret, flags; 61 size_t iosize; 62 int ttyflag; 63 64 /* 65 * Verify arguments. The `int' limit on `size' is due to this 66 * particular implementation. Note, buf and size are ignored 67 * when setting _IONBF. 68 */ 69 if (mode != _IONBF) 70 if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0) 71 return (EOF); 72 73 /* 74 * Write current buffer, if any. Discard unread input (including 75 * ungetc data), cancel line buffering, and free old buffer if 76 * malloc()ed. We also clear any eof condition, as if this were 77 * a seek. 78 */ 79 ret = 0; 80 (void)__sflush(fp); 81 if (HASUB(fp)) 82 FREEUB(fp); 83 fp->_r = fp->_lbfsize = 0; 84 flags = fp->_flags; 85 if (flags & __SMBF) 86 free((void *)fp->_bf._base); 87 flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF); 88 89 /* If setting unbuffered mode, skip all the hard work. */ 90 if (mode == _IONBF) 91 goto nbf; 92 93 /* 94 * Find optimal I/O size for seek optimization. This also returns 95 * a `tty flag' to suggest that we check isatty(fd), but we do not 96 * care since our caller told us how to buffer. 97 */ 98 flags |= __swhatbuf(fp, &iosize, &ttyflag); 99 if (size == 0) { 100 buf = NULL; /* force local allocation */ 101 size = iosize; 102 } 103 104 /* Allocate buffer if needed. */ 105 if (buf == NULL) { 106 if ((buf = malloc(size)) == NULL) { 107 /* 108 * Unable to honor user's request. We will return 109 * failure, but try again with file system size. 110 */ 111 ret = EOF; 112 if (size != iosize) { 113 size = iosize; 114 buf = malloc(size); 115 } 116 } 117 if (buf == NULL) { 118 /* No luck; switch to unbuffered I/O. */ 119 nbf: 120 fp->_flags = flags | __SNBF; 121 fp->_w = 0; 122 fp->_bf._base = fp->_p = fp->_nbuf; 123 fp->_bf._size = 1; 124 return (ret); 125 } 126 flags |= __SMBF; 127 } 128 129 /* 130 * Kill any seek optimization if the buffer is not the 131 * right size. 132 * 133 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)? 134 */ 135 if (size != iosize) 136 flags |= __SNPT; 137 138 /* 139 * Fix up the FILE fields, and set __cleanup for output flush on 140 * exit (since we are buffered in some way). 141 */ 142 if (mode == _IOLBF) 143 flags |= __SLBF; 144 fp->_flags = flags; 145 fp->_bf._base = fp->_p = (unsigned char *)buf; 146 fp->_bf._size = size; 147 /* fp->_lbfsize is still 0 */ 148 if (flags & __SWR) { 149 /* 150 * Begin or continue writing: see __swsetup(). Note 151 * that __SNBF is impossible (it was handled earlier). 152 */ 153 if (flags & __SLBF) { 154 fp->_w = 0; 155 fp->_lbfsize = -fp->_bf._size; 156 } else 157 fp->_w = size; 158 } else { 159 /* begin/continue reading, or stay in intermediate state */ 160 fp->_w = 0; 161 } 162 __cleanup = _cleanup; 163 164 return (ret); 165 } 166