]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/findfp.c
3 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials are licensed and made available under
5 the terms and conditions of the BSD License that accompanies this distribution.
6 The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 * Copyright (c) 1990, 1993
13 * The Regents of the University of California. All rights reserved.
15 * This code is derived from software contributed to Berkeley by
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 NetBSD: findfp.c,v 1.23 2006/10/07 21:40:46 thorpej Exp
43 findfp.c 8.2 (Berkeley) 1/4/94
45 #include <LibConfig.h>
46 #include <sys/EfiCdefs.h>
48 #include "namespace.h"
49 #include <sys/param.h>
55 #include "reentrant.h"
62 #define NDYNAMIC 10 /* add ten more whenever necessary */
64 #define std(flags, file) \
65 /* p r w flags file bf lfbsize cookie close */ \
66 { NULL, 0, 0, flags, file, { NULL, 0 }, 0, __sF + file, __sclose, \
67 /* read seek write ext up */ \
68 __sread, __sseek, __swrite, { (void *)(__sFext + file), 0 }, NULL, \
69 /* ur ubuf, nbuf lb blksize offset */ \
70 0, { '\0', '\0', '\0' }, { '\0' }, { NULL, 0 }, 0, (fpos_t)0 }
72 /* the usual - (stdin + stdout + stderr) */
73 static FILE usual
[FOPEN_MAX
- 3];
74 static struct __sfileext usualext
[FOPEN_MAX
- 3];
75 static struct glue uglue
= { 0, FOPEN_MAX
- 3, usual
};
77 #if defined(_REENTRANT) && !defined(__lint__) /* XXX lint is busted */
78 #define STDEXT { ._lock = MUTEX_INITIALIZER, ._lockcond = COND_INITIALIZER }
79 struct __sfileext __sFext
[3] = { STDEXT
,
83 struct __sfileext __sFext
[3];
87 std(__SRD
, STDIN_FILENO
), /* stdin */
88 std(__SWR
, STDOUT_FILENO
), /* stdout */
89 std(__SWR
|__SNBF
, STDERR_FILENO
) /* stderr */
91 struct glue __sglue
= { &uglue
, 3, __sF
};
93 static struct glue
*moreglue(int);
94 void f_prealloc(void);
97 rwlock_t __sfp_lock
= RWLOCK_INITIALIZER
;
105 struct __sfileext
*pext
;
108 g
= (struct glue
*)malloc(sizeof(*g
) + ALIGNBYTES
+ n
* sizeof(FILE)
109 + n
* sizeof(struct __sfileext
));
112 p
= (FILE *)ALIGN((g
+ 1));
116 pext
= (void *)(p
+ n
);
119 _FILEEXT_SETUP(p
, pext
);
127 * Find a free FILE for fopen et al.
139 rwlock_wrlock(&__sfp_lock
);
140 for (g
= &__sglue
;; g
= g
->next
) {
141 for (fp
= g
->iobs
, n
= g
->niobs
; --n
>= 0; fp
++)
144 if (g
->next
== NULL
&& (g
->next
= moreglue(NDYNAMIC
)) == NULL
)
147 rwlock_unlock(&__sfp_lock
);
150 fp
->_flags
= 1; /* reserve this slot; caller sets real flags */
151 fp
->_p
= NULL
; /* no current pointer */
152 fp
->_w
= 0; /* nothing to read or write */
154 fp
->_bf
._base
= NULL
; /* no buffer */
156 fp
->_lbfsize
= 0; /* not line buffered */
157 fp
->_file
= -1; /* no file */
158 /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
159 _UB(fp
)._base
= NULL
; /* no ungetc buffer */
161 fp
->_lb
._base
= NULL
; /* no line buffer */
163 memset(WCIO_GET(fp
), 0, sizeof(struct wchar_io_data
));
164 rwlock_unlock(&__sfp_lock
);
170 * XXX. Force immediate allocation of internal memory. Not used by stdio,
171 * but documented historically for certain applications. Bad applications.
179 n
= (int)sysconf(_SC_OPEN_MAX
) - FOPEN_MAX
+ 20; /* 20 for slop. */
180 for (g
= &__sglue
; (n
-= g
->niobs
) > 0 && g
->next
; g
= g
->next
)
183 g
->next
= moreglue(n
);
188 * exit() calls _cleanup() through *gMD->cleanup, set whenever we
189 * open or buffer a file. This chicanery is done so that programs
190 * that do not use stdio need not link it all in.
192 * The name `_cleanup' is, alas, fairly well known outside stdio.
197 /* (void) _fwalk(fclose); */
198 (void) fflush(NULL
); /* `cheating' */
202 * __sinit() is called whenever stdio's internal variables must be set up.
209 for (i
= 0; i
< FOPEN_MAX
- 3; i
++)
210 _FILEEXT_SETUP(&usual
[i
], &usualext
[i
]);
212 /* make sure we clean up on exit */
213 gMD
->cleanup
= _cleanup
; /* conservative */