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