xref: /minix/lib/libc/stdio/findfp.c (revision 0a6a1f1d)
1 /*	$NetBSD: findfp.c,v 1.28 2012/03/27 15:05:42 christos 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 
35 #include <sys/cdefs.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.28 2012/03/27 15:05:42 christos Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <unistd.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 
55 int	__sdidinit;
56 
57 #define	NDYNAMIC 10		/* add ten more whenever necessary */
58 
59 #if !defined(_LIBMINC) && !defined(__kernel__) && defined(__minix)
60 
61 #define	std(flags, file) { \
62 	._p = NULL, \
63 	._r = 0, \
64 	._w = 0, \
65 	._flags = (flags), \
66 	._file = (file),  \
67 	._bf = { ._base = NULL, ._size = 0 }, \
68 	._lbfsize = 0,  \
69 	._cookie = __sF + (file), \
70 	._close = __sclose, \
71 	._read = __sread, \
72 	._seek = __sseek, \
73 	._write = __swrite, \
74 	._ext = { ._base = (void *)(__sFext + (file)), ._size = 0 }, \
75 	._up = NULL, \
76         ._ur = 0, \
77 	._ubuf = { [0] = '\0', [1] = '\0', [2] = '\0' }, \
78 	._nbuf = { [0] = '\0' }, \
79 	._flush = NULL, \
80 	._lb_unused = { '\0' }, \
81 	._blksize = 0, \
82 	._offset = (off_t)0, \
83 }
84 
85 #else
86 
87 #define	std(flags, file) { \
88 	._p = NULL, \
89 	._r = 0, \
90 	._w = 0, \
91 	._flags = (flags), \
92 	._file = (file),  \
93 	._bf = { ._base = NULL, ._size = 0 }, \
94 	._lbfsize = 0,  \
95 	._cookie = __sF + (file), \
96 	._close = NULL, \
97 	._read = NULL, \
98 	._seek = NULL, \
99 	._write = NULL, \
100 	._ext = { ._base = (void *)(__sFext + (file)), ._size = 0 }, \
101 	._up = NULL, \
102         ._ur = 0, \
103 	._ubuf = { [0] = '\0', [1] = '\0', [2] = '\0' }, \
104 	._nbuf = { [0] = '\0' }, \
105 	._flush = NULL, \
106 	._lb_unused = { '\0' }, \
107 	._blksize = 0, \
108 	._offset = (off_t)0, \
109 }
110 
111 #endif /* !defined(_LIBMINC) && !defined(__kernel__) */
112 
113 #if !defined(__kernel__) && defined(__minix)
114 				/* the usual - (stdin + stdout + stderr) */
115 static FILE usual[FOPEN_MAX - 3];
116 static struct __sfileext usualext[FOPEN_MAX - 3];
117 static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
118 #endif /* !defined(__kernel__) && defined(__minix) */
119 
120 #if defined(_REENTRANT) && !defined(__lint__) /* XXX lint is busted */
121 #define	STDEXT { ._lock = MUTEX_INITIALIZER, ._lockcond = COND_INITIALIZER }
122 struct __sfileext __sFext[3] = { STDEXT,
123 				 STDEXT,
124 				 STDEXT};
125 #else
126 struct __sfileext __sFext[3];
127 #endif
128 
129 FILE __sF[3] = {
130 	std(__SRD, STDIN_FILENO),		/* stdin */
131 	std(__SWR, STDOUT_FILENO),		/* stdout */
132 	std(__SWR|__SNBF, STDERR_FILENO)	/* stderr */
133 };
134 
135 #if !defined(__kernel__) && defined(__minix)
136 struct glue __sglue = { &uglue, 3, __sF };
137 
138 void f_prealloc(void);
139 
140 #ifdef _REENTRANT
141 rwlock_t __sfp_lock = RWLOCK_INITIALIZER;
142 #endif
143 
144 static struct glue *
moreglue(int n)145 moreglue(int n)
146 {
147 	struct glue *g;
148 	FILE *p;
149 	struct __sfileext *pext;
150 	static FILE empty;
151 
152 	g = malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE)
153 	    + n * sizeof(struct __sfileext));
154 	if (g == NULL)
155 		return NULL;
156 	p = (FILE *)ALIGN((u_long)(g + 1));
157 	g->next = NULL;
158 	g->niobs = n;
159 	g->iobs = p;
160 	pext = (void *)(p + n);
161 	while (--n >= 0) {
162 		*p = empty;
163 		_FILEEXT_SETUP(p, pext);
164 		p++;
165 		pext++;
166 	}
167 	return g;
168 }
169 
170 void
__sfpinit(FILE * fp)171 __sfpinit(FILE *fp)
172 {
173 	fp->_flags = 1;		/* reserve this slot; caller sets real flags */
174 	fp->_p = NULL;		/* no current pointer */
175 	fp->_w = 0;		/* nothing to read or write */
176 	fp->_r = 0;
177 	fp->_bf._base = NULL;	/* no buffer */
178 	fp->_bf._size = 0;
179 	fp->_lbfsize = 0;	/* not line buffered */
180 	fp->_file = -1;		/* no file */
181 /*	fp->_cookie = <any>; */	/* caller sets cookie, _read/_write etc */
182 	fp->_flush = NULL;	/* default flush */
183 	_UB(fp)._base = NULL;	/* no ungetc buffer */
184 	_UB(fp)._size = 0;
185 	memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data));
186 }
187 
188 /*
189  * Find a free FILE for fopen et al.
190  */
191 FILE *
__sfp(void)192 __sfp(void)
193 {
194 	FILE *fp;
195 	int n;
196 	struct glue *g;
197 
198 	if (!__sdidinit)
199 		__sinit();
200 
201 	rwlock_wrlock(&__sfp_lock);
202 	for (g = &__sglue;; g = g->next) {
203 		for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
204 			if (fp->_flags == 0)
205 				goto found;
206 		if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
207 			break;
208 	}
209 	rwlock_unlock(&__sfp_lock);
210 	return NULL;
211 found:
212 	__sfpinit(fp);
213 	rwlock_unlock(&__sfp_lock);
214 	return fp;
215 }
216 
217 /*
218  * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
219  * but documented historically for certain applications.  Bad applications.
220  */
221 void
f_prealloc(void)222 f_prealloc(void)
223 {
224 #if !defined(_LIBMINC) && defined(__minix)
225 	struct glue *g;
226 	int n;
227 
228 	n = (int)sysconf(_SC_OPEN_MAX) - FOPEN_MAX + 20; /* 20 for slop. */
229 	for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
230 		continue;
231 	if (n > 0)
232 		g->next = moreglue(n);
233 #endif /* !defined(_LIBMINC) && defined(__minix) */
234 }
235 
236 /*
237  * exit() calls _cleanup() through *__cleanup, set whenever we
238  * open or buffer a file.  This chicanery is done so that programs
239  * that do not use stdio need not link it all in.
240  *
241  * The name `_cleanup' is, alas, fairly well known outside stdio.
242  */
243 void
_cleanup(void)244 _cleanup(void)
245 {
246 #if !defined(_LIBMINC) && defined(__minix)
247 	/* (void) _fwalk(fclose); */
248 	(void) fflush(NULL);			/* `cheating' */
249 #endif /* !defined(_LIBMINC) && defined(__minix) */
250 }
251 
252 /*
253  * __sinit() is called whenever stdio's internal variables must be set up.
254  */
255 void
__sinit(void)256 __sinit(void)
257 {
258 	int i;
259 
260 	for (i = 0; i < FOPEN_MAX - 3; i++)
261 		_FILEEXT_SETUP(&usual[i], &usualext[i]);
262 
263 	/* make sure we clean up on exit */
264 	__cleanup = _cleanup;		/* conservative */
265 	__sdidinit = 1;
266 }
267 #endif /* !defined(__kernel__) && defined(__minix) */
268