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