xref: /netbsd-src/lib/libc/stdio/findfp.c (revision de001ba2a53bc20787a4ae96999d5ddddde49d02)
1*de001ba2Schristos /*	$NetBSD: findfp.c,v 1.28 2012/03/27 15:05:42 christos Exp $	*/
2255db7b2Sjtc 
361f28255Scgd /*-
4255db7b2Sjtc  * Copyright (c) 1990, 1993
5255db7b2Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Chris Torek.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
3523312f88Schristos #include <sys/cdefs.h>
3661f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
37255db7b2Sjtc #if 0
38255db7b2Sjtc static char sccsid[] = "@(#)findfp.c	8.2 (Berkeley) 1/4/94";
3923312f88Schristos #else
40*de001ba2Schristos __RCSID("$NetBSD: findfp.c,v 1.28 2012/03/27 15:05:42 christos Exp $");
41255db7b2Sjtc #endif
4261f28255Scgd #endif /* LIBC_SCCS and not lint */
4361f28255Scgd 
4445f04f24Skleink #include "namespace.h"
45fee4815eScgd #include <sys/param.h>
4661f28255Scgd #include <unistd.h>
4761f28255Scgd #include <stdio.h>
4861f28255Scgd #include <errno.h>
4961f28255Scgd #include <stdlib.h>
5061f28255Scgd #include <string.h>
513fdac2b8Sthorpej #include "reentrant.h"
5261f28255Scgd #include "local.h"
5361f28255Scgd #include "glue.h"
5461f28255Scgd 
5561f28255Scgd int	__sdidinit;
5661f28255Scgd 
5761f28255Scgd #define	NDYNAMIC 10		/* add ten more whenever necessary */
5861f28255Scgd 
59*de001ba2Schristos #define	std(flags, file) { \
60*de001ba2Schristos 	._p = NULL, \
61*de001ba2Schristos 	._r = 0, \
62*de001ba2Schristos 	._w = 0, \
63*de001ba2Schristos 	._flags = (flags), \
64*de001ba2Schristos 	._file = (file),  \
65*de001ba2Schristos 	._bf = { ._base = NULL, ._size = 0 }, \
66*de001ba2Schristos 	._lbfsize = 0,  \
67*de001ba2Schristos 	._cookie = __sF + (file), \
68*de001ba2Schristos 	._close = __sclose, \
69*de001ba2Schristos 	._read = __sread, \
70*de001ba2Schristos 	._seek = __sseek, \
71*de001ba2Schristos 	._write = __swrite, \
72*de001ba2Schristos 	._ext = { ._base = (void *)(__sFext + (file)), ._size = 0 }, \
73*de001ba2Schristos 	._up = NULL, \
74*de001ba2Schristos         ._ur = 0, \
75*de001ba2Schristos 	._ubuf = { [0] = '\0', [1] = '\0', [2] = '\0' }, \
76*de001ba2Schristos 	._nbuf = { [0] = '\0' }, \
77*de001ba2Schristos 	._flush = NULL, \
78*de001ba2Schristos 	._lb_unused = { '\0' }, \
79*de001ba2Schristos 	._blksize = 0, \
80*de001ba2Schristos 	._offset = (off_t)0, \
81*de001ba2Schristos }
8261f28255Scgd 
83255db7b2Sjtc 				/* the usual - (stdin + stdout + stderr) */
84255db7b2Sjtc static FILE usual[FOPEN_MAX - 3];
8517f3654aSyamt static struct __sfileext usualext[FOPEN_MAX - 3];
86255db7b2Sjtc static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
8761f28255Scgd 
882e2d300fSthorpej #if defined(_REENTRANT) && !defined(__lint__) /* XXX lint is busted */
89569c003cSthorpej #define	STDEXT { ._lock = MUTEX_INITIALIZER, ._lockcond = COND_INITIALIZER }
9034c915ebSnathanw struct __sfileext __sFext[3] = { STDEXT,
9134c915ebSnathanw 				 STDEXT,
9234c915ebSnathanw 				 STDEXT};
9334c915ebSnathanw #else
9417f3654aSyamt struct __sfileext __sFext[3];
9534c915ebSnathanw #endif
9617f3654aSyamt 
9761f28255Scgd FILE __sF[3] = {
9861f28255Scgd 	std(__SRD, STDIN_FILENO),		/* stdin */
9961f28255Scgd 	std(__SWR, STDOUT_FILENO),		/* stdout */
10061f28255Scgd 	std(__SWR|__SNBF, STDERR_FILENO)	/* stderr */
10161f28255Scgd };
10261f28255Scgd struct glue __sglue = { &uglue, 3, __sF };
10361f28255Scgd 
104526d9427Schristos void f_prealloc(void);
10523312f88Schristos 
1063fdac2b8Sthorpej #ifdef _REENTRANT
107a1de770eSjtc rwlock_t __sfp_lock = RWLOCK_INITIALIZER;
108a1de770eSjtc #endif
109a1de770eSjtc 
11061f28255Scgd static struct glue *
moreglue(int n)111526d9427Schristos moreglue(int n)
11261f28255Scgd {
113c8bafd62Sperry 	struct glue *g;
114c8bafd62Sperry 	FILE *p;
11517f3654aSyamt 	struct __sfileext *pext;
11661f28255Scgd 	static FILE empty;
11761f28255Scgd 
118*de001ba2Schristos 	g = malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE)
11917f3654aSyamt 	    + n * sizeof(struct __sfileext));
12061f28255Scgd 	if (g == NULL)
121526d9427Schristos 		return NULL;
122e7f7fdbaSchristos 	p = (FILE *)ALIGN((u_long)(g + 1));
12361f28255Scgd 	g->next = NULL;
12461f28255Scgd 	g->niobs = n;
12561f28255Scgd 	g->iobs = p;
12617f3654aSyamt 	pext = (void *)(p + n);
12717f3654aSyamt 	while (--n >= 0) {
12817f3654aSyamt 		*p = empty;
12917f3654aSyamt 		_FILEEXT_SETUP(p, pext);
13017f3654aSyamt 		p++;
13117f3654aSyamt 		pext++;
13217f3654aSyamt 	}
133526d9427Schristos 	return g;
13461f28255Scgd }
13561f28255Scgd 
1364ddc2fb3Schristos void
__sfpinit(FILE * fp)1374ddc2fb3Schristos __sfpinit(FILE *fp)
1384ddc2fb3Schristos {
1394ddc2fb3Schristos 	fp->_flags = 1;		/* reserve this slot; caller sets real flags */
1404ddc2fb3Schristos 	fp->_p = NULL;		/* no current pointer */
1414ddc2fb3Schristos 	fp->_w = 0;		/* nothing to read or write */
1424ddc2fb3Schristos 	fp->_r = 0;
1434ddc2fb3Schristos 	fp->_bf._base = NULL;	/* no buffer */
1444ddc2fb3Schristos 	fp->_bf._size = 0;
1454ddc2fb3Schristos 	fp->_lbfsize = 0;	/* not line buffered */
1464ddc2fb3Schristos 	fp->_file = -1;		/* no file */
1474ddc2fb3Schristos /*	fp->_cookie = <any>; */	/* caller sets cookie, _read/_write etc */
148*de001ba2Schristos 	fp->_flush = NULL;	/* default flush */
1494ddc2fb3Schristos 	_UB(fp)._base = NULL;	/* no ungetc buffer */
1504ddc2fb3Schristos 	_UB(fp)._size = 0;
1514ddc2fb3Schristos 	memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data));
1524ddc2fb3Schristos }
1534ddc2fb3Schristos 
15461f28255Scgd /*
15561f28255Scgd  * Find a free FILE for fopen et al.
15661f28255Scgd  */
15761f28255Scgd FILE *
__sfp(void)158526d9427Schristos __sfp(void)
15961f28255Scgd {
160c8bafd62Sperry 	FILE *fp;
161c8bafd62Sperry 	int n;
162c8bafd62Sperry 	struct glue *g;
16361f28255Scgd 
16461f28255Scgd 	if (!__sdidinit)
16561f28255Scgd 		__sinit();
166a1de770eSjtc 
167a1de770eSjtc 	rwlock_wrlock(&__sfp_lock);
16861f28255Scgd 	for (g = &__sglue;; g = g->next) {
16961f28255Scgd 		for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
17061f28255Scgd 			if (fp->_flags == 0)
17161f28255Scgd 				goto found;
17261f28255Scgd 		if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
17361f28255Scgd 			break;
17461f28255Scgd 	}
175a1de770eSjtc 	rwlock_unlock(&__sfp_lock);
176526d9427Schristos 	return NULL;
17761f28255Scgd found:
1784ddc2fb3Schristos 	__sfpinit(fp);
179a1de770eSjtc 	rwlock_unlock(&__sfp_lock);
180526d9427Schristos 	return fp;
18161f28255Scgd }
18261f28255Scgd 
18361f28255Scgd /*
18461f28255Scgd  * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
18561f28255Scgd  * but documented historically for certain applications.  Bad applications.
18661f28255Scgd  */
18758f143a5Sjtc void
f_prealloc(void)188526d9427Schristos f_prealloc(void)
18961f28255Scgd {
190c8bafd62Sperry 	struct glue *g;
191255db7b2Sjtc 	int n;
19261f28255Scgd 
1933e8c90f6Skleink 	n = (int)sysconf(_SC_OPEN_MAX) - FOPEN_MAX + 20; /* 20 for slop. */
19461f28255Scgd 	for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
195526d9427Schristos 		continue;
19661f28255Scgd 	if (n > 0)
19761f28255Scgd 		g->next = moreglue(n);
19861f28255Scgd }
19961f28255Scgd 
20061f28255Scgd /*
20161f28255Scgd  * exit() calls _cleanup() through *__cleanup, set whenever we
20261f28255Scgd  * open or buffer a file.  This chicanery is done so that programs
20361f28255Scgd  * that do not use stdio need not link it all in.
20461f28255Scgd  *
20561f28255Scgd  * The name `_cleanup' is, alas, fairly well known outside stdio.
20661f28255Scgd  */
20761f28255Scgd void
_cleanup(void)208526d9427Schristos _cleanup(void)
20961f28255Scgd {
21061f28255Scgd 	/* (void) _fwalk(fclose); */
211b714bb51Smycroft 	(void) fflush(NULL);			/* `cheating' */
21261f28255Scgd }
21361f28255Scgd 
21461f28255Scgd /*
21561f28255Scgd  * __sinit() is called whenever stdio's internal variables must be set up.
21661f28255Scgd  */
21761f28255Scgd void
__sinit(void)218526d9427Schristos __sinit(void)
21961f28255Scgd {
22017f3654aSyamt 	int i;
22117f3654aSyamt 
22217f3654aSyamt 	for (i = 0; i < FOPEN_MAX - 3; i++)
22317f3654aSyamt 		_FILEEXT_SETUP(&usual[i], &usualext[i]);
22417f3654aSyamt 
22561f28255Scgd 	/* make sure we clean up on exit */
22661f28255Scgd 	__cleanup = _cleanup;		/* conservative */
22761f28255Scgd 	__sdidinit = 1;
22861f28255Scgd }
229