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