xref: /original-bsd/lib/libc/stdio/findfp.c (revision 333da485)
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  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)findfp.c	8.2 (Berkeley) 01/04/94";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/param.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "local.h"
22 #include "glue.h"
23 
24 int	__sdidinit;
25 
26 #define	NDYNAMIC 10		/* add ten more whenever necessary */
27 
28 #define	std(flags, file) \
29 	{0,0,0,flags,file,{0},0,__sF+file,__sclose,__sread,__sseek,__swrite}
30 /*	 p r w flags file _bf z  cookie      close    read    seek    write */
31 
32 				/* the usual - (stdin + stdout + stderr) */
33 static FILE usual[FOPEN_MAX - 3];
34 static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
35 
36 FILE __sF[3] = {
37 	std(__SRD, STDIN_FILENO),		/* stdin */
38 	std(__SWR, STDOUT_FILENO),		/* stdout */
39 	std(__SWR|__SNBF, STDERR_FILENO)	/* stderr */
40 };
41 struct glue __sglue = { &uglue, 3, __sF };
42 
43 static struct glue *
44 moreglue(n)
45 	register int n;
46 {
47 	register struct glue *g;
48 	register FILE *p;
49 	static FILE empty;
50 
51 	g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE));
52 	if (g == NULL)
53 		return (NULL);
54 	p = (FILE *)ALIGN(g + 1);
55 	g->next = NULL;
56 	g->niobs = n;
57 	g->iobs = p;
58 	while (--n >= 0)
59 		*p++ = empty;
60 	return (g);
61 }
62 
63 /*
64  * Find a free FILE for fopen et al.
65  */
66 FILE *
67 __sfp()
68 {
69 	register FILE *fp;
70 	register int n;
71 	register struct glue *g;
72 
73 	if (!__sdidinit)
74 		__sinit();
75 	for (g = &__sglue;; g = g->next) {
76 		for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
77 			if (fp->_flags == 0)
78 				goto found;
79 		if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
80 			break;
81 	}
82 	return (NULL);
83 found:
84 	fp->_flags = 1;		/* reserve this slot; caller sets real flags */
85 	fp->_p = NULL;		/* no current pointer */
86 	fp->_w = 0;		/* nothing to read or write */
87 	fp->_r = 0;
88 	fp->_bf._base = NULL;	/* no buffer */
89 	fp->_bf._size = 0;
90 	fp->_lbfsize = 0;	/* not line buffered */
91 	fp->_file = -1;		/* no file */
92 /*	fp->_cookie = <any>; */	/* caller sets cookie, _read/_write etc */
93 	fp->_ub._base = NULL;	/* no ungetc buffer */
94 	fp->_ub._size = 0;
95 	fp->_lb._base = NULL;	/* no line buffer */
96 	fp->_lb._size = 0;
97 	return (fp);
98 }
99 
100 /*
101  * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
102  * but documented historically for certain applications.  Bad applications.
103  */
104 f_prealloc()
105 {
106 	register struct glue *g;
107 	int n;
108 
109 	n = getdtablesize() - FOPEN_MAX + 20;		/* 20 for slop. */
110 	for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
111 		/* void */;
112 	if (n > 0)
113 		g->next = moreglue(n);
114 }
115 
116 /*
117  * exit() calls _cleanup() through *__cleanup, set whenever we
118  * open or buffer a file.  This chicanery is done so that programs
119  * that do not use stdio need not link it all in.
120  *
121  * The name `_cleanup' is, alas, fairly well known outside stdio.
122  */
123 void
124 _cleanup()
125 {
126 	/* (void) _fwalk(fclose); */
127 	(void) _fwalk(__sflush);		/* `cheating' */
128 }
129 
130 /*
131  * __sinit() is called whenever stdio's internal variables must be set up.
132  */
133 void
134 __sinit()
135 {
136 	/* make sure we clean up on exit */
137 	__cleanup = _cleanup;		/* conservative */
138 	__sdidinit = 1;
139 }
140