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