1 /* $OpenBSD: setvbuf.c,v 1.8 2005/08/08 08:05:36 espie 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 ret = 0;
65 (void)__sflush(fp);
66 if (HASUB(fp))
67 FREEUB(fp);
68 WCIO_FREE(fp);
69 fp->_r = fp->_lbfsize = 0;
70 flags = fp->_flags;
71 if (flags & __SMBF)
72 free((void *)fp->_bf._base);
73 flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF);
74
75 /* If setting unbuffered mode, skip all the hard work. */
76 if (mode == _IONBF)
77 goto nbf;
78
79 /*
80 * Find optimal I/O size for seek optimization. This also returns
81 * a `tty flag' to suggest that we check isatty(fd), but we do not
82 * care since our caller told us how to buffer.
83 */
84 flags |= __swhatbuf(fp, &iosize, &ttyflag);
85 if (size == 0) {
86 buf = NULL; /* force local allocation */
87 size = iosize;
88 }
89
90 /* Allocate buffer if needed. */
91 if (buf == NULL) {
92 if ((buf = malloc(size)) == NULL) {
93 /*
94 * Unable to honor user's request. We will return
95 * failure, but try again with file system size.
96 */
97 ret = EOF;
98 if (size != iosize) {
99 size = iosize;
100 buf = malloc(size);
101 }
102 }
103 if (buf == NULL) {
104 /* No luck; switch to unbuffered I/O. */
105 nbf:
106 fp->_flags = flags | __SNBF;
107 fp->_w = 0;
108 fp->_bf._base = fp->_p = fp->_nbuf;
109 fp->_bf._size = 1;
110 return (ret);
111 }
112 flags |= __SMBF;
113 }
114
115 /*
116 * Kill any seek optimization if the buffer is not the
117 * right size.
118 *
119 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
120 */
121 if (size != iosize)
122 flags |= __SNPT;
123
124 /*
125 * Fix up the FILE fields, and set __cleanup for output flush on
126 * exit (since we are buffered in some way).
127 */
128 if (mode == _IOLBF)
129 flags |= __SLBF;
130 fp->_flags = flags;
131 fp->_bf._base = fp->_p = (unsigned char *)buf;
132 fp->_bf._size = size;
133 /* fp->_lbfsize is still 0 */
134 if (flags & __SWR) {
135 /*
136 * Begin or continue writing: see __swsetup(). Note
137 * that __SNBF is impossible (it was handled earlier).
138 */
139 if (flags & __SLBF) {
140 fp->_w = 0;
141 fp->_lbfsize = -fp->_bf._size;
142 } else
143 fp->_w = size;
144 } else {
145 /* begin/continue reading, or stay in intermediate state */
146 fp->_w = 0;
147 }
148 __atexit_register_cleanup(_cleanup);
149
150 return (ret);
151 }
152