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