xref: /openbsd-src/lib/libc/stdio/findfp.c (revision 11efff7f3ac2b3cfeff0c0cddc14294d9b3aca4f)
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.5 2004/09/28 18:12:44 otto 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(int n)
67 {
68 	struct glue *g;
69 	FILE *p;
70 	static FILE empty;
71 
72 	g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE));
73 	if (g == NULL)
74 		return (NULL);
75 	p = (FILE *)ALIGN(g + 1);
76 	g->next = NULL;
77 	g->niobs = n;
78 	g->iobs = p;
79 	while (--n >= 0)
80 		*p++ = empty;
81 	return (g);
82 }
83 
84 /*
85  * Find a free FILE for fopen et al.
86  */
87 FILE *
88 __sfp(void)
89 {
90 	FILE *fp;
91 	int n;
92 	struct glue *g;
93 
94 	if (!__sdidinit)
95 		__sinit();
96 	for (g = &__sglue;; g = g->next) {
97 		for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
98 			if (fp->_flags == 0)
99 				goto found;
100 		if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
101 			break;
102 	}
103 	return (NULL);
104 found:
105 	fp->_flags = 1;		/* reserve this slot; caller sets real flags */
106 	fp->_p = NULL;		/* no current pointer */
107 	fp->_w = 0;		/* nothing to read or write */
108 	fp->_r = 0;
109 	fp->_bf._base = NULL;	/* no buffer */
110 	fp->_bf._size = 0;
111 	fp->_lbfsize = 0;	/* not line buffered */
112 	fp->_file = -1;		/* no file */
113 /*	fp->_cookie = <any>; */	/* caller sets cookie, _read/_write etc */
114 	fp->_ub._base = NULL;	/* no ungetc buffer */
115 	fp->_ub._size = 0;
116 	fp->_lb._base = NULL;	/* no line buffer */
117 	fp->_lb._size = 0;
118 	return (fp);
119 }
120 
121 /*
122  * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
123  * but documented historically for certain applications.  Bad applications.
124  */
125 void
126 f_prealloc(void)
127 {
128 	struct glue *g;
129 	int n;
130 
131 	n = getdtablesize() - FOPEN_MAX + 20;		/* 20 for slop. */
132 	for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
133 		/* void */;
134 	if (n > 0)
135 		g->next = moreglue(n);
136 }
137 
138 /*
139  * exit() and abort() call _cleanup() through the callback registered
140  * with __atexit_register_cleanup(), set whenever we open or buffer a
141  * file. This chicanery is done so that programs that do not use stdio
142  * need not link it all in.
143  *
144  * The name `_cleanup' is, alas, fairly well known outside stdio.
145  */
146 void
147 _cleanup(void)
148 {
149 	/* (void) _fwalk(fclose); */
150 	(void) _fwalk(__sflush);		/* `cheating' */
151 }
152 
153 /*
154  * __sinit() is called whenever stdio's internal variables must be set up.
155  */
156 void
157 __sinit(void)
158 {
159 	/* make sure we clean up on exit */
160 	__atexit_register_cleanup(_cleanup); /* conservative */
161 	__sdidinit = 1;
162 }
163