findfp.c revision b4867734b06a18494976934c6f33ff2549595e3f
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
43int	__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},0,__sF+file,__sclose,__sread,__sseek,__swrite, \
49	 {(unsigned char *)(__sFext+file), 0},NULL,0,{0,0,0},{0},{0,0},0,0}
50/*	 p r w flags file _bf z  cookie      close    read    seek    write
51	 ext */
52
53/* the usual - (stdin + stdout + stderr) */
54static FILE usual[FOPEN_MAX - 3];
55static struct __sfileext usualext[FOPEN_MAX - 3];
56static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
57
58static struct __sfileext __sFext[3];
59FILE __sF[3] = {
60	std(__SRD, STDIN_FILENO),		/* stdin */
61	std(__SWR, STDOUT_FILENO),		/* stdout */
62	std(__SWR|__SNBF, STDERR_FILENO)	/* stderr */
63};
64struct glue __sglue = { &uglue, 3, __sF };
65
66static struct glue *
67moreglue(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 */
98FILE *
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);
115found:
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#if 0
132#define getdtablesize()	sysconf(_SC_OPEN_MAX)
133
134/*
135 * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
136 * but documented historically for certain applications.  Bad applications.
137 */
138void
139f_prealloc(void)
140{
141	struct glue *g;
142	int n;
143
144	n = getdtablesize() - FOPEN_MAX + 20;		/* 20 for slop. */
145	for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
146		/* void */;
147	if (n > 0)
148		g->next = moreglue(n);
149}
150#endif
151
152/*
153 * exit() and abort() call _cleanup() through the callback registered
154 * with __atexit_register_cleanup(), set whenever we open or buffer a
155 * file. This chicanery is done so that programs that do not use stdio
156 * need not link it all in.
157 *
158 * The name `_cleanup' is, alas, fairly well known outside stdio.
159 */
160void
161_cleanup(void)
162{
163	/* (void) _fwalk(fclose); */
164	(void) _fwalk(__sflush);		/* `cheating' */
165}
166
167/*
168 * __sinit() is called whenever stdio's internal variables must be set up.
169 */
170void
171__sinit(void)
172{
173	int i;
174
175	for (i = 0; i < FOPEN_MAX - 3; i++) {
176		_FILEEXT_SETUP(usual+i, usualext+i);
177	}
178	/* make sure we clean up on exit */
179	__atexit_register_cleanup(_cleanup); /* conservative */
180	__sdidinit = 1;
181}
182