]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Stdio/refill.c
SignedCapsulePkg: Add license file.
[mirror_edk2.git] / StdLib / LibC / Stdio / refill.c
CommitLineData
53e1e5c6 1/*\r
a9c12422 2 Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
53e1e5c6 3 This program and the accompanying materials are licensed and made available\r
4 under the terms and conditions of the BSD License that accompanies this\r
5 distribution. The full text of the license may be found at\r
6 http://opensource.org/licenses/bsd-license.\r
7\r
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
2aa62f2b 10\r
2aa62f2b 11 * Copyright (c) 1990, 1993\r
12 * The Regents of the University of California. All rights reserved.\r
13 *\r
14 * This code is derived from software contributed to Berkeley by\r
15 * Chris Torek.\r
16 *\r
17 * Redistribution and use in source and binary forms, with or without\r
18 * modification, are permitted provided that the following conditions\r
19 * are met:\r
20 * 1. Redistributions of source code must retain the above copyright\r
21 * notice, this list of conditions and the following disclaimer.\r
22 * 2. Redistributions in binary form must reproduce the above copyright\r
23 * notice, this list of conditions and the following disclaimer in the\r
24 * documentation and/or other materials provided with the distribution.\r
25 * 3. Neither the name of the University nor the names of its contributors\r
26 * may be used to endorse or promote products derived from this software\r
27 * without specific prior written permission.\r
28 *\r
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
39 * SUCH DAMAGE.\r
53e1e5c6 40\r
41 NetBSD: refill.c,v 1.13 2003/08/07 16:43:30 agc Exp\r
42 refill.c 8.1 (Berkeley) 6/4/93\r
43*/\r
2aa62f2b 44#include <LibConfig.h>\r
45#include <sys/EfiCdefs.h>\r
2aa62f2b 46\r
47#include <assert.h>\r
48#include <errno.h>\r
49#include <stdio.h>\r
50#include <stdlib.h>\r
51#include "reentrant.h"\r
52#include "local.h"\r
53\r
54#ifdef _REENTRANT\r
55extern rwlock_t __sfp_lock;\r
56#endif\r
57\r
58static int lflush(FILE *);\r
59\r
60static int\r
61lflush(FILE *fp)\r
62{\r
63\r
53e1e5c6 64 _DIAGASSERT(fp != NULL);\r
65 if(fp == NULL) {\r
66 errno = EINVAL;\r
67 return (EOF);\r
68 }\r
2aa62f2b 69\r
70 if ((fp->_flags & (__SLBF|__SWR)) == (__SLBF|__SWR))\r
71 return (__sflush(fp));\r
72 return (0);\r
73}\r
74\r
75/*\r
76 * Refill a stdio buffer.\r
77 * Return EOF on eof or error, 0 otherwise.\r
78 */\r
79int\r
80__srefill(FILE *fp)\r
81{\r
82\r
53e1e5c6 83 _DIAGASSERT(fp != NULL);\r
84 if(fp == NULL) {\r
85 errno = EINVAL;\r
86 return (EOF);\r
87 }\r
2aa62f2b 88\r
89 /* make sure stdio is set up */\r
90 if (!__sdidinit)\r
91 __sinit();\r
92\r
2aa62f2b 93 fp->_r = 0; /* largely a convenience for callers */\r
94\r
95 /* SysV does not make this test; take it out for compatibility */\r
96 if (fp->_flags & __SEOF) {\r
2aa62f2b 97 return (EOF);\r
98 }\r
99\r
100 /* if not already reading, have to be reading and writing */\r
101 if ((fp->_flags & __SRD) == 0) {\r
2aa62f2b 102 if ((fp->_flags & __SRW) == 0) {\r
103 errno = EBADF;\r
104 fp->_flags |= __SERR; //<dvm> Allows differentiation between errors and EOF\r
2aa62f2b 105 return (EOF);\r
106 }\r
107 /* switch to reading */\r
108 if (fp->_flags & __SWR) {\r
109 if (__sflush(fp)) {\r
2aa62f2b 110 return (EOF);\r
111 }\r
112 fp->_flags &= ~__SWR;\r
113 fp->_w = 0;\r
114 fp->_lbfsize = 0;\r
115 }\r
116 fp->_flags |= __SRD;\r
117 } else {\r
2aa62f2b 118 /*\r
119 * We were reading. If there is an ungetc buffer,\r
120 * we must have been reading from that. Drop it,\r
121 * restoring the previous buffer (if any). If there\r
122 * is anything in that buffer, return.\r
123 */\r
124 if (HASUB(fp)) {\r
125 FREEUB(fp);\r
126 if ((fp->_r = fp->_ur) != 0) {\r
127 fp->_p = fp->_up;\r
2aa62f2b 128 return (0);\r
129 }\r
130 }\r
131 }\r
132\r
133 if (fp->_bf._base == NULL)\r
134 __smakebuf(fp);\r
135\r
2aa62f2b 136 /*\r
137 * Before reading from a line buffered or unbuffered file,\r
138 * flush all line buffered output files, per the ANSI C\r
139 * standard.\r
140 */\r
141 if (fp->_flags & (__SLBF|__SNBF)) {\r
142 rwlock_rdlock(&__sfp_lock);\r
143 (void) _fwalk(lflush);\r
144 rwlock_unlock(&__sfp_lock);\r
2aa62f2b 145 }\r
146 fp->_p = fp->_bf._base;\r
147 fp->_r = (*fp->_read)(fp->_cookie, (char *)fp->_p, fp->_bf._size);\r
148 fp->_flags &= ~__SMOD; /* buffer contents are again pristine */\r
149 if (fp->_r <= 0) {\r
150 if (fp->_r == 0)\r
151 fp->_flags |= __SEOF;\r
152 else {\r
153 fp->_r = 0;\r
154 fp->_flags |= __SERR;\r
155 }\r
2aa62f2b 156 return (EOF);\r
157 }\r
2aa62f2b 158 return (0);\r
159}\r