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