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