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