findfp.c revision 1.11 1 /* $NetBSD: findfp.c,v 1.11 1998/10/18 13:56:22 kleink 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94";
43 #else
44 __RCSID("$NetBSD: findfp.c,v 1.11 1998/10/18 13:56:22 kleink Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 #include "namespace.h"
49 #include <sys/param.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <errno.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include "local.h"
56 #include "glue.h"
57 #include "reentrant.h"
58
59 int __sdidinit;
60
61 #define NDYNAMIC 10 /* add ten more whenever necessary */
62
63 #define std(flags, file) \
64 {0,0,0,flags,file,{0},0,__sF+file,__sclose,__sread,__sseek,__swrite}
65 /* p r w flags file _bf z cookie close read seek write */
66
67 /* the usual - (stdin + stdout + stderr) */
68 static FILE usual[FOPEN_MAX - 3];
69 static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
70
71 FILE __sF[3] = {
72 std(__SRD, STDIN_FILENO), /* stdin */
73 std(__SWR, STDOUT_FILENO), /* stdout */
74 std(__SWR|__SNBF, STDERR_FILENO) /* stderr */
75 };
76 struct glue __sglue = { &uglue, 3, __sF };
77
78 static struct glue *moreglue __P((int));
79 void f_prealloc __P((void));
80
81 #ifdef _REENT
82 rwlock_t __sfp_lock = RWLOCK_INITIALIZER;
83 #endif
84
85 static struct glue *
86 moreglue(n)
87 int n;
88 {
89 struct glue *g;
90 FILE *p;
91 static FILE empty;
92
93 g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE));
94 if (g == NULL)
95 return (NULL);
96 p = (FILE *)ALIGN(g + 1);
97 g->next = NULL;
98 g->niobs = n;
99 g->iobs = p;
100 while (--n >= 0)
101 *p++ = empty;
102 return (g);
103 }
104
105 /*
106 * Find a free FILE for fopen et al.
107 */
108 FILE *
109 __sfp()
110 {
111 FILE *fp;
112 int n;
113 struct glue *g;
114
115 if (!__sdidinit)
116 __sinit();
117
118 rwlock_wrlock(&__sfp_lock);
119 for (g = &__sglue;; g = g->next) {
120 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
121 if (fp->_flags == 0)
122 goto found;
123 if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
124 break;
125 }
126 rwlock_unlock(&__sfp_lock);
127 return (NULL);
128 found:
129 fp->_flags = 1; /* reserve this slot; caller sets real flags */
130 fp->_p = NULL; /* no current pointer */
131 fp->_w = 0; /* nothing to read or write */
132 fp->_r = 0;
133 fp->_bf._base = NULL; /* no buffer */
134 fp->_bf._size = 0;
135 fp->_lbfsize = 0; /* not line buffered */
136 fp->_file = -1; /* no file */
137 /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
138 fp->_ub._base = NULL; /* no ungetc buffer */
139 fp->_ub._size = 0;
140 fp->_lb._base = NULL; /* no line buffer */
141 fp->_lb._size = 0;
142 rwlock_unlock(&__sfp_lock);
143 return (fp);
144 }
145
146 /*
147 * XXX. Force immediate allocation of internal memory. Not used by stdio,
148 * but documented historically for certain applications. Bad applications.
149 */
150 void
151 f_prealloc()
152 {
153 struct glue *g;
154 int n;
155
156 n = (int)sysconf(_SC_OPEN_MAX) - FOPEN_MAX + 20; /* 20 for slop. */
157 for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
158 /* void */;
159 if (n > 0)
160 g->next = moreglue(n);
161 }
162
163 /*
164 * exit() calls _cleanup() through *__cleanup, set whenever we
165 * open or buffer a file. This chicanery is done so that programs
166 * that do not use stdio need not link it all in.
167 *
168 * The name `_cleanup' is, alas, fairly well known outside stdio.
169 */
170 void
171 _cleanup()
172 {
173 /* (void) _fwalk(fclose); */
174 (void) _fwalk(__sflush); /* `cheating' */
175 }
176
177 /*
178 * __sinit() is called whenever stdio's internal variables must be set up.
179 */
180 void
181 __sinit()
182 {
183 /* make sure we clean up on exit */
184 __cleanup = _cleanup; /* conservative */
185 __sdidinit = 1;
186 }
187