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