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