]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/setvbuf.c
9e01ea9dc1f693b688799fca6a075cd3ea28dfb9
[mirror_edk2.git] / StdLib / LibC / Stdio / setvbuf.c
1 /** @file
2 Implementation of setvbuf as declared in <stdio.h>.
3
4 Copyright (c) 2010 - 2011, 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.
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 if(fp == NULL) {
71 errno = EINVAL;
72 return (EOF);
73 }
74
75 /*
76 * Verify arguments. The `int' limit on `size' is due to this
77 * particular implementation. Note, buf and size are ignored
78 * when setting _IONBF.
79 */
80 if (mode != _IONBF)
81 if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0)
82 return (-1);
83
84 FLOCKFILE(fp);
85 /*
86 * Write current buffer, if any. Discard unread input (including
87 * ungetc data), cancel line buffering, and free old buffer if
88 * malloc()ed. We also clear any eof condition, as if this were
89 * a seek.
90 */
91 ret = 0;
92 (void)__sflush(fp);
93 if (HASUB(fp))
94 FREEUB(fp);
95 WCIO_FREE(fp);
96 fp->_r = fp->_lbfsize = 0;
97 flags = fp->_flags;
98 if (flags & __SMBF)
99 free((void *)fp->_bf._base);
100 flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF);
101
102 /* If setting unbuffered mode, skip all the hard work. */
103 if (mode == _IONBF)
104 goto nbf;
105
106 /*
107 * Find optimal I/O size for seek optimization. This also returns
108 * a `tty flag' to suggest that we check isatty(fd), but we do not
109 * care since our caller told us how to buffer.
110 */
111 flags |= __swhatbuf(fp, &iosize, &ttyflag);
112 if (size == 0) {
113 buf = NULL; /* force local allocation */
114 size = iosize;
115 }
116
117 /* Allocate buffer if needed. */
118 if (buf == NULL) {
119 if ((buf = malloc(size)) == NULL) {
120 /*
121 * Unable to honor user's request. We will return
122 * failure, but try again with file system size.
123 */
124 ret = -1;
125 if (size != iosize) {
126 size = iosize;
127 buf = malloc(size);
128 }
129 }
130 if (buf == NULL) {
131 /* No luck; switch to unbuffered I/O. */
132 nbf:
133 fp->_flags = (unsigned short)(flags | __SNBF);
134 fp->_w = 0;
135 fp->_bf._base = fp->_p = fp->_nbuf;
136 fp->_bf._size = 1;
137 FUNLOCKFILE(fp);
138 return (ret);
139 }
140 flags |= __SMBF;
141 }
142
143 /*
144 * Kill any seek optimization if the buffer is not the
145 * right size.
146 *
147 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
148 */
149 if (size != iosize)
150 flags |= __SNPT;
151
152 /*
153 * Fix up the FILE fields, and set gMD->cleanup for output flush on
154 * exit (since we are buffered in some way).
155 */
156 if (mode == _IOLBF)
157 flags |= __SLBF;
158 fp->_flags = (unsigned short)flags;
159 fp->_bf._base = fp->_p = (unsigned char *)buf;
160 fp->_bf._size = (int)size;
161 /* fp->_lbfsize is still 0 */
162 if (flags & __SWR) {
163 /*
164 * Begin or continue writing: see __swsetup(). Note
165 * that __SNBF is impossible (it was handled earlier).
166 */
167 if (flags & __SLBF) {
168 fp->_w = 0;
169 fp->_lbfsize = -fp->_bf._size;
170 } else
171 fp->_w = (int)size;
172 } else {
173 /* begin/continue reading, or stay in intermediate state */
174 fp->_w = 0;
175 }
176 gMD->cleanup = _cleanup;
177
178 FUNLOCKFILE(fp);
179 return (ret);
180 }