]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Stdio/refill.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Stdio / refill.c
CommitLineData
2aa62f2b 1/* $NetBSD: refill.c,v 1.13 2003/08/07 16:43:30 agc Exp $ */\r
2\r
3/*-\r
4 * Copyright (c) 1990, 1993\r
5 * The Regents of the University of California. All rights reserved.\r
6 *\r
7 * This code is derived from software contributed to Berkeley by\r
8 * Chris Torek.\r
9 *\r
10 * Redistribution and use in source and binary forms, with or without\r
11 * modification, are permitted provided that the following conditions\r
12 * are met:\r
13 * 1. Redistributions of source code must retain the above copyright\r
14 * notice, this list of conditions and the following disclaimer.\r
15 * 2. Redistributions in binary form must reproduce the above copyright\r
16 * notice, this list of conditions and the following disclaimer in the\r
17 * documentation and/or other materials provided with the distribution.\r
18 * 3. Neither the name of the University nor the names of its contributors\r
19 * may be used to endorse or promote products derived from this software\r
20 * without specific prior written permission.\r
21 *\r
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
32 * SUCH DAMAGE.\r
33 */\r
34#include <Uefi.h> // REMOVE, For DEBUG only\r
35#include <Library/UefiLib.h> // REMOVE, For DEBUG only\r
36\r
37#include <LibConfig.h>\r
38#include <sys/EfiCdefs.h>\r
39#if defined(LIBC_SCCS) && !defined(lint)\r
40#if 0\r
41static char sccsid[] = "@(#)refill.c 8.1 (Berkeley) 6/4/93";\r
42#else\r
43__RCSID("$NetBSD: refill.c,v 1.13 2003/08/07 16:43:30 agc Exp $");\r
44#endif\r
45#endif /* LIBC_SCCS and not lint */\r
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
64 //_DIAGASSERT(fp != NULL);\r
65\r
66 if ((fp->_flags & (__SLBF|__SWR)) == (__SLBF|__SWR))\r
67 return (__sflush(fp));\r
68 return (0);\r
69}\r
70\r
71/*\r
72 * Refill a stdio buffer.\r
73 * Return EOF on eof or error, 0 otherwise.\r
74 */\r
75int\r
76__srefill(FILE *fp)\r
77{\r
78\r
79 //_DIAGASSERT(fp != NULL);\r
80\r
81 /* make sure stdio is set up */\r
82 if (!__sdidinit)\r
83 __sinit();\r
84\r
85//Print(L"%a( %d)\n", __func__, fp->_file);\r
86 fp->_r = 0; /* largely a convenience for callers */\r
87\r
88 /* SysV does not make this test; take it out for compatibility */\r
89 if (fp->_flags & __SEOF) {\r
90//Print(L"%a: %d\n", __func__, __LINE__);\r
91 return (EOF);\r
92 }\r
93\r
94 /* if not already reading, have to be reading and writing */\r
95 if ((fp->_flags & __SRD) == 0) {\r
96//Print(L"%a: %d\n", __func__, __LINE__);\r
97 if ((fp->_flags & __SRW) == 0) {\r
98 errno = EBADF;\r
99 fp->_flags |= __SERR; //<dvm> Allows differentiation between errors and EOF\r
100//Print(L"%a: %d\n", __func__, __LINE__);\r
101 return (EOF);\r
102 }\r
103 /* switch to reading */\r
104 if (fp->_flags & __SWR) {\r
105 if (__sflush(fp)) {\r
106//Print(L"%a: %d\n", __func__, __LINE__);\r
107 return (EOF);\r
108 }\r
109 fp->_flags &= ~__SWR;\r
110 fp->_w = 0;\r
111 fp->_lbfsize = 0;\r
112 }\r
113 fp->_flags |= __SRD;\r
114 } else {\r
115//Print(L"%a: %d\n", __func__, __LINE__);\r
116 /*\r
117 * We were reading. If there is an ungetc buffer,\r
118 * we must have been reading from that. Drop it,\r
119 * restoring the previous buffer (if any). If there\r
120 * is anything in that buffer, return.\r
121 */\r
122 if (HASUB(fp)) {\r
123 FREEUB(fp);\r
124 if ((fp->_r = fp->_ur) != 0) {\r
125 fp->_p = fp->_up;\r
126//Print(L"%a: %d\n", __func__, __LINE__);\r
127 return (0);\r
128 }\r
129 }\r
130 }\r
131\r
132 if (fp->_bf._base == NULL)\r
133 __smakebuf(fp);\r
134\r
135//Print(L"%a: %d\n", __func__, __LINE__);\r
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
145//Print(L"%a: %d\n", __func__, __LINE__);\r
146 }\r
147 fp->_p = fp->_bf._base;\r
148 fp->_r = (*fp->_read)(fp->_cookie, (char *)fp->_p, fp->_bf._size);\r
149 fp->_flags &= ~__SMOD; /* buffer contents are again pristine */\r
150 if (fp->_r <= 0) {\r
151 if (fp->_r == 0)\r
152 fp->_flags |= __SEOF;\r
153 else {\r
154 fp->_r = 0;\r
155 fp->_flags |= __SERR;\r
156 }\r
157//Print(L"%a: %d\n", __func__, __LINE__);\r
158 return (EOF);\r
159 }\r
160//Print(L"%a: %d\n", __func__, __LINE__);\r
161 return (0);\r
162}\r