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