]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/findfp.c
b6495c3bcb9037f01b58fdcad799a4ca74e8a50f
[mirror_edk2.git] / StdLib / LibC / Stdio / findfp.c
1 /* $NetBSD: findfp.c,v 1.23 2006/10/07 21:40:46 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34 #include <LibConfig.h>
35 #include <sys/EfiCdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94";
39 #else
40 __RCSID("$NetBSD: findfp.c,v 1.23 2006/10/07 21:40:46 thorpej Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <sys/EfiSysCall.h>
47 #include <stdio.h>
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include "reentrant.h"
52 #include "local.h"
53 #include "glue.h"
54 #include <MainData.h>
55
56 int __sdidinit;
57
58 #define NDYNAMIC 10 /* add ten more whenever necessary */
59
60 #define std(flags, file) \
61 /* p r w flags file bf lfbsize cookie close */ \
62 { NULL, 0, 0, flags, file, { NULL, 0 }, 0, __sF + file, __sclose, \
63 /* read seek write ext up */ \
64 __sread, __sseek, __swrite, { (void *)(__sFext + file), 0 }, NULL, \
65 /* ur ubuf, nbuf lb blksize offset */ \
66 0, { '\0', '\0', '\0' }, { '\0' }, { NULL, 0 }, 0, (fpos_t)0 }
67
68 /* the usual - (stdin + stdout + stderr) */
69 static FILE usual[FOPEN_MAX - 3];
70 static struct __sfileext usualext[FOPEN_MAX - 3];
71 static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
72
73 #if defined(_REENTRANT) && !defined(__lint__) /* XXX lint is busted */
74 #define STDEXT { ._lock = MUTEX_INITIALIZER, ._lockcond = COND_INITIALIZER }
75 struct __sfileext __sFext[3] = { STDEXT,
76 STDEXT,
77 STDEXT};
78 #else
79 struct __sfileext __sFext[3];
80 #endif
81
82 FILE __sF[3] = {
83 std(__SRD, STDIN_FILENO), /* stdin */
84 std(__SWR, STDOUT_FILENO), /* stdout */
85 std(__SWR|__SNBF, STDERR_FILENO) /* stderr */
86 };
87 struct glue __sglue = { &uglue, 3, __sF };
88
89 static struct glue *moreglue(int);
90 void f_prealloc(void);
91
92 #ifdef _REENTRANT
93 rwlock_t __sfp_lock = RWLOCK_INITIALIZER;
94 #endif
95
96 static struct glue *
97 moreglue(int n)
98 {
99 struct glue *g;
100 FILE *p;
101 struct __sfileext *pext;
102 static FILE empty;
103
104 g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE)
105 + n * sizeof(struct __sfileext));
106 if (g == NULL)
107 return (NULL);
108 p = (FILE *)ALIGN((g + 1));
109 g->next = NULL;
110 g->niobs = n;
111 g->iobs = p;
112 pext = (void *)(p + n);
113 while (--n >= 0) {
114 *p = empty;
115 _FILEEXT_SETUP(p, pext);
116 p++;
117 pext++;
118 }
119 return (g);
120 }
121
122 /*
123 * Find a free FILE for fopen et al.
124 */
125 FILE *
126 __sfp()
127 {
128 FILE *fp;
129 int n;
130 struct glue *g;
131
132 if (!__sdidinit)
133 __sinit();
134
135 rwlock_wrlock(&__sfp_lock);
136 for (g = &__sglue;; g = g->next) {
137 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
138 if (fp->_flags == 0)
139 goto found;
140 if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
141 break;
142 }
143 rwlock_unlock(&__sfp_lock);
144 return (NULL);
145 found:
146 fp->_flags = 1; /* reserve this slot; caller sets real flags */
147 fp->_p = NULL; /* no current pointer */
148 fp->_w = 0; /* nothing to read or write */
149 fp->_r = 0;
150 fp->_bf._base = NULL; /* no buffer */
151 fp->_bf._size = 0;
152 fp->_lbfsize = 0; /* not line buffered */
153 fp->_file = -1; /* no file */
154 /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
155 _UB(fp)._base = NULL; /* no ungetc buffer */
156 _UB(fp)._size = 0;
157 fp->_lb._base = NULL; /* no line buffer */
158 fp->_lb._size = 0;
159 memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data));
160 rwlock_unlock(&__sfp_lock);
161 return (fp);
162 }
163
164 #if 0
165 /*
166 * XXX. Force immediate allocation of internal memory. Not used by stdio,
167 * but documented historically for certain applications. Bad applications.
168 */
169 void
170 f_prealloc()
171 {
172 struct glue *g;
173 int n;
174
175 n = (int)sysconf(_SC_OPEN_MAX) - FOPEN_MAX + 20; /* 20 for slop. */
176 for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
177 /* void */;
178 if (n > 0)
179 g->next = moreglue(n);
180 }
181 #endif
182
183 /*
184 * exit() calls _cleanup() through *gMD->cleanup, set whenever we
185 * open or buffer a file. This chicanery is done so that programs
186 * that do not use stdio need not link it all in.
187 *
188 * The name `_cleanup' is, alas, fairly well known outside stdio.
189 */
190 void
191 _cleanup( void )
192 {
193 /* (void) _fwalk(fclose); */
194 (void) fflush(NULL); /* `cheating' */
195 }
196
197 /*
198 * __sinit() is called whenever stdio's internal variables must be set up.
199 */
200 void
201 __sinit( void )
202 {
203 int i;
204
205 for (i = 0; i < FOPEN_MAX - 3; i++)
206 _FILEEXT_SETUP(&usual[i], &usualext[i]);
207
208 /* make sure we clean up on exit */
209 gMD->cleanup = _cleanup; /* conservative */
210 __sdidinit = 1;
211 }