xref: /netbsd/lib/libc/stdio/findfp.c (revision bf9ec67e)
1 /*	$NetBSD: findfp.c,v 1.15 2002/03/12 22:56:16 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. 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.15 2002/03/12 22:56:16 christos 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 /*	  p     r  w  flags  file  bf     lfbsize  cookie       close */ \
65 	{ NULL, 0, 0, flags, file, { NULL, 0 }, 0, __sF + file, __sclose, \
66 /*	  read      seek     write     ext                              up */ \
67 	  __sread,  __sseek, __swrite, { (void *)(__sFext + file), 0 }, NULL, \
68 /*	  ur ubuf,                 nbuf      lb     blksize  offset */ \
69 	  0, { '\0', '\0', '\0' }, { '\0' }, { NULL, 0 }, 0, (fpos_t)0 }
70 
71 				/* the usual - (stdin + stdout + stderr) */
72 static FILE usual[FOPEN_MAX - 3];
73 static struct __sfileext usualext[FOPEN_MAX - 3];
74 static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
75 
76 struct __sfileext __sFext[3];
77 
78 FILE __sF[3] = {
79 	std(__SRD, STDIN_FILENO),		/* stdin */
80 	std(__SWR, STDOUT_FILENO),		/* stdout */
81 	std(__SWR|__SNBF, STDERR_FILENO)	/* stderr */
82 };
83 struct glue __sglue = { &uglue, 3, __sF };
84 
85 static struct glue *moreglue __P((int));
86 void f_prealloc __P((void));
87 
88 #ifdef _REENT
89 rwlock_t __sfp_lock = RWLOCK_INITIALIZER;
90 #endif
91 
92 static struct glue *
93 moreglue(n)
94 	int n;
95 {
96 	struct glue *g;
97 	FILE *p;
98 	struct __sfileext *pext;
99 	static FILE empty;
100 
101 	g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE)
102 		+ n * sizeof(struct __sfileext));
103 	if (g == NULL)
104 		return (NULL);
105 	p = (FILE *)ALIGN((u_long)(g + 1));
106 	g->next = NULL;
107 	g->niobs = n;
108 	g->iobs = p;
109 	pext = (void *)(p + n);
110 	while (--n >= 0) {
111 		*p = empty;
112 		_FILEEXT_SETUP(p, pext);
113 		p++;
114 		pext++;
115 	}
116 	return (g);
117 }
118 
119 /*
120  * Find a free FILE for fopen et al.
121  */
122 FILE *
123 __sfp()
124 {
125 	FILE *fp;
126 	int n;
127 	struct glue *g;
128 
129 	if (!__sdidinit)
130 		__sinit();
131 
132 	rwlock_wrlock(&__sfp_lock);
133 	for (g = &__sglue;; g = g->next) {
134 		for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
135 			if (fp->_flags == 0)
136 				goto found;
137 		if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
138 			break;
139 	}
140 	rwlock_unlock(&__sfp_lock);
141 	return (NULL);
142 found:
143 	fp->_flags = 1;		/* reserve this slot; caller sets real flags */
144 	fp->_p = NULL;		/* no current pointer */
145 	fp->_w = 0;		/* nothing to read or write */
146 	fp->_r = 0;
147 	fp->_bf._base = NULL;	/* no buffer */
148 	fp->_bf._size = 0;
149 	fp->_lbfsize = 0;	/* not line buffered */
150 	fp->_file = -1;		/* no file */
151 /*	fp->_cookie = <any>; */	/* caller sets cookie, _read/_write etc */
152 	_UB(fp)._base = NULL;	/* no ungetc buffer */
153 	_UB(fp)._size = 0;
154 	fp->_lb._base = NULL;	/* no line buffer */
155 	fp->_lb._size = 0;
156 	memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data));
157 	rwlock_unlock(&__sfp_lock);
158 	return (fp);
159 }
160 
161 /*
162  * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
163  * but documented historically for certain applications.  Bad applications.
164  */
165 void
166 f_prealloc()
167 {
168 	struct glue *g;
169 	int n;
170 
171 	n = (int)sysconf(_SC_OPEN_MAX) - FOPEN_MAX + 20; /* 20 for slop. */
172 	for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
173 		/* void */;
174 	if (n > 0)
175 		g->next = moreglue(n);
176 }
177 
178 /*
179  * exit() calls _cleanup() through *__cleanup, set whenever we
180  * open or buffer a file.  This chicanery is done so that programs
181  * that do not use stdio need not link it all in.
182  *
183  * The name `_cleanup' is, alas, fairly well known outside stdio.
184  */
185 void
186 _cleanup()
187 {
188 	/* (void) _fwalk(fclose); */
189 	(void) fflush(NULL);			/* `cheating' */
190 }
191 
192 /*
193  * __sinit() is called whenever stdio's internal variables must be set up.
194  */
195 void
196 __sinit()
197 {
198 	int i;
199 
200 	for (i = 0; i < FOPEN_MAX - 3; i++)
201 		_FILEEXT_SETUP(&usual[i], &usualext[i]);
202 
203 	/* make sure we clean up on exit */
204 	__cleanup = _cleanup;		/* conservative */
205 	__sdidinit = 1;
206 }
207