1 /* $OpenBSD: setvbuf.c,v 1.15 2022/09/28 16:44:14 gnezdo Exp $ */ 2 /*- 3 * Copyright (c) 1990, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Chris Torek. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <limits.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include "local.h" 38 39 /* 40 * Set one of the three kinds of buffering, optionally including 41 * a buffer. 42 */ 43 int 44 setvbuf(FILE *fp, char *buf, int mode, size_t size) 45 { 46 int ret, flags; 47 size_t iosize; 48 int ttyflag; 49 50 /* 51 * Verify arguments. The `int' limit on `size' is due to this 52 * particular implementation. Note, buf and size are ignored 53 * when setting _IONBF. 54 */ 55 if (mode != _IONBF) 56 if ((mode != _IOFBF && mode != _IOLBF) || size > INT_MAX) 57 return (EOF); 58 59 /* 60 * Write current buffer, if any. Discard unread input (including 61 * ungetc data), cancel line buffering, and free old buffer if 62 * malloc()ed. We also clear any eof condition, as if this were 63 * a seek. 64 */ 65 FLOCKFILE(fp); 66 ret = 0; 67 (void)__sflush(fp); 68 if (HASUB(fp)) 69 FREEUB(fp); 70 WCIO_FREE(fp); 71 fp->_r = fp->_lbfsize = 0; 72 flags = fp->_flags; 73 if (flags & __SMBF) 74 free(fp->_bf._base); 75 flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF); 76 77 /* If setting unbuffered mode, skip all the hard work. */ 78 if (mode == _IONBF) 79 goto nbf; 80 81 /* 82 * Find optimal I/O size for seek optimization. This also returns 83 * a `tty flag' to suggest that we check isatty(fd), but we do not 84 * care since our caller told us how to buffer. 85 */ 86 flags |= __swhatbuf(fp, &iosize, &ttyflag); 87 if (size == 0) { 88 buf = NULL; /* force local allocation */ 89 size = iosize; 90 } 91 92 /* Allocate buffer if needed. */ 93 if (buf == NULL) { 94 if ((buf = malloc(size)) == NULL) { 95 /* 96 * Unable to honor user's request. We will return 97 * failure, but try again with file system size. 98 */ 99 ret = EOF; 100 if (size != iosize) { 101 size = iosize; 102 buf = malloc(size); 103 } 104 } 105 if (buf == NULL) { 106 /* No luck; switch to unbuffered I/O. */ 107 nbf: 108 fp->_flags = flags | __SNBF; 109 fp->_w = 0; 110 fp->_bf._base = fp->_p = fp->_nbuf; 111 fp->_bf._size = 1; 112 FUNLOCKFILE(fp); 113 return (ret); 114 } 115 flags |= __SMBF; 116 } 117 118 /* 119 * We're committed to buffering from here, so make sure we've 120 * registered to flush buffers on exit. 121 */ 122 if (!__sdidinit) 123 __sinit(); 124 125 /* 126 * Kill any seek optimization if the buffer is not the 127 * right size. 128 * 129 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)? 130 */ 131 if (size != iosize) 132 flags |= __SNPT; 133 134 /* 135 * Fix up the FILE fields, and set __cleanup for output flush on 136 * exit (since we are buffered in some way). 137 */ 138 if (mode == _IOLBF) 139 flags |= __SLBF; 140 fp->_flags = flags; 141 fp->_bf._base = fp->_p = (unsigned char *)buf; 142 fp->_bf._size = size; 143 /* fp->_lbfsize is still 0 */ 144 if (flags & __SWR) { 145 /* 146 * Begin or continue writing: see __swsetup(). Note 147 * that __SNBF is impossible (it was handled earlier). 148 */ 149 if (flags & __SLBF) { 150 fp->_w = 0; 151 fp->_lbfsize = -fp->_bf._size; 152 } else 153 fp->_w = size; 154 } else { 155 /* begin/continue reading, or stay in intermediate state */ 156 fp->_w = 0; 157 } 158 FUNLOCKFILE(fp); 159 160 return (ret); 161 } 162 DEF_STRONG(setvbuf); 163