xref: /netbsd-src/lib/libc/stdio/findfp.c (revision 7c7c171d130af9949261bc7dce2150a03c3d239c)
1 /*	$NetBSD: findfp.c,v 1.9 1998/02/03 18:41:10 perry 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.9 1998/02/03 18:41:10 perry Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47 
48 #include <sys/param.h>
49 #include <unistd.h>
50 #include <stdio.h>
51 #include <errno.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include "local.h"
55 #include "glue.h"
56 #include "reentrant.h"
57 
58 int	__sdidinit;
59 
60 #define	NDYNAMIC 10		/* add ten more whenever necessary */
61 
62 #define	std(flags, file) \
63 	{0,0,0,flags,file,{0},0,__sF+file,__sclose,__sread,__sseek,__swrite}
64 /*	 p r w flags file _bf z  cookie      close    read    seek    write */
65 
66 				/* the usual - (stdin + stdout + stderr) */
67 static FILE usual[FOPEN_MAX - 3];
68 static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
69 
70 FILE __sF[3] = {
71 	std(__SRD, STDIN_FILENO),		/* stdin */
72 	std(__SWR, STDOUT_FILENO),		/* stdout */
73 	std(__SWR|__SNBF, STDERR_FILENO)	/* stderr */
74 };
75 struct glue __sglue = { &uglue, 3, __sF };
76 
77 static struct glue *moreglue __P((int));
78 void f_prealloc __P((void));
79 
80 #ifdef _REENT
81 rwlock_t __sfp_lock = RWLOCK_INITIALIZER;
82 #endif
83 
84 static struct glue *
85 moreglue(n)
86 	int n;
87 {
88 	struct glue *g;
89 	FILE *p;
90 	static FILE empty;
91 
92 	g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE));
93 	if (g == NULL)
94 		return (NULL);
95 	p = (FILE *)ALIGN(g + 1);
96 	g->next = NULL;
97 	g->niobs = n;
98 	g->iobs = p;
99 	while (--n >= 0)
100 		*p++ = empty;
101 	return (g);
102 }
103 
104 /*
105  * Find a free FILE for fopen et al.
106  */
107 FILE *
108 __sfp()
109 {
110 	FILE *fp;
111 	int n;
112 	struct glue *g;
113 
114 	if (!__sdidinit)
115 		__sinit();
116 
117 	rwlock_wrlock(&__sfp_lock);
118 	for (g = &__sglue;; g = g->next) {
119 		for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
120 			if (fp->_flags == 0)
121 				goto found;
122 		if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
123 			break;
124 	}
125 	rwlock_unlock(&__sfp_lock);
126 	return (NULL);
127 found:
128 	fp->_flags = 1;		/* reserve this slot; caller sets real flags */
129 	fp->_p = NULL;		/* no current pointer */
130 	fp->_w = 0;		/* nothing to read or write */
131 	fp->_r = 0;
132 	fp->_bf._base = NULL;	/* no buffer */
133 	fp->_bf._size = 0;
134 	fp->_lbfsize = 0;	/* not line buffered */
135 	fp->_file = -1;		/* no file */
136 /*	fp->_cookie = <any>; */	/* caller sets cookie, _read/_write etc */
137 	fp->_ub._base = NULL;	/* no ungetc buffer */
138 	fp->_ub._size = 0;
139 	fp->_lb._base = NULL;	/* no line buffer */
140 	fp->_lb._size = 0;
141 	rwlock_unlock(&__sfp_lock);
142 	return (fp);
143 }
144 
145 /*
146  * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
147  * but documented historically for certain applications.  Bad applications.
148  */
149 void
150 f_prealloc()
151 {
152 	struct glue *g;
153 	int n;
154 
155 	n = getdtablesize() - FOPEN_MAX + 20;		/* 20 for slop. */
156 	for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
157 		/* void */;
158 	if (n > 0)
159 		g->next = moreglue(n);
160 }
161 
162 /*
163  * exit() calls _cleanup() through *__cleanup, set whenever we
164  * open or buffer a file.  This chicanery is done so that programs
165  * that do not use stdio need not link it all in.
166  *
167  * The name `_cleanup' is, alas, fairly well known outside stdio.
168  */
169 void
170 _cleanup()
171 {
172 	/* (void) _fwalk(fclose); */
173 	(void) _fwalk(__sflush);		/* `cheating' */
174 }
175 
176 /*
177  * __sinit() is called whenever stdio's internal variables must be set up.
178  */
179 void
180 __sinit()
181 {
182 	/* make sure we clean up on exit */
183 	__cleanup = _cleanup;		/* conservative */
184 	__sdidinit = 1;
185 }
186