1 /* $NetBSD: findfp.c,v 1.7 1997/07/13 20:14:58 christos 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 #include <sys/cdefs.h> 40 #if defined(LIBC_SCCS) && !defined(lint) 41 #if 0 42 static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94"; 43 #else 44 __RCSID("$NetBSD: findfp.c,v 1.7 1997/07/13 20:14:58 christos Exp $"); 45 #endif 46 #endif /* LIBC_SCCS and not lint */ 47 48 #include <sys/param.h> 49 #include <unistd.h> 50 #include <stdio.h> 51 #include <errno.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include "local.h" 55 #include "glue.h" 56 57 int __sdidinit; 58 59 #define NDYNAMIC 10 /* add ten more whenever necessary */ 60 61 #define std(flags, file) \ 62 {0,0,0,flags,file,{0},0,__sF+file,__sclose,__sread,__sseek,__swrite} 63 /* p r w flags file _bf z cookie close read seek write */ 64 65 /* the usual - (stdin + stdout + stderr) */ 66 static FILE usual[FOPEN_MAX - 3]; 67 static struct glue uglue = { 0, FOPEN_MAX - 3, usual }; 68 69 FILE __sF[3] = { 70 std(__SRD, STDIN_FILENO), /* stdin */ 71 std(__SWR, STDOUT_FILENO), /* stdout */ 72 std(__SWR|__SNBF, STDERR_FILENO) /* stderr */ 73 }; 74 struct glue __sglue = { &uglue, 3, __sF }; 75 76 static struct glue *moreglue __P((int)); 77 void f_prealloc __P((void)); 78 79 static struct glue * 80 moreglue(n) 81 register int n; 82 { 83 register struct glue *g; 84 register FILE *p; 85 static FILE empty; 86 87 g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE)); 88 if (g == NULL) 89 return (NULL); 90 p = (FILE *)ALIGN(g + 1); 91 g->next = NULL; 92 g->niobs = n; 93 g->iobs = p; 94 while (--n >= 0) 95 *p++ = empty; 96 return (g); 97 } 98 99 /* 100 * Find a free FILE for fopen et al. 101 */ 102 FILE * 103 __sfp() 104 { 105 register FILE *fp; 106 register int n; 107 register struct glue *g; 108 109 if (!__sdidinit) 110 __sinit(); 111 for (g = &__sglue;; g = g->next) { 112 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) 113 if (fp->_flags == 0) 114 goto found; 115 if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL) 116 break; 117 } 118 return (NULL); 119 found: 120 fp->_flags = 1; /* reserve this slot; caller sets real flags */ 121 fp->_p = NULL; /* no current pointer */ 122 fp->_w = 0; /* nothing to read or write */ 123 fp->_r = 0; 124 fp->_bf._base = NULL; /* no buffer */ 125 fp->_bf._size = 0; 126 fp->_lbfsize = 0; /* not line buffered */ 127 fp->_file = -1; /* no file */ 128 /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */ 129 fp->_ub._base = NULL; /* no ungetc buffer */ 130 fp->_ub._size = 0; 131 fp->_lb._base = NULL; /* no line buffer */ 132 fp->_lb._size = 0; 133 return (fp); 134 } 135 136 /* 137 * XXX. Force immediate allocation of internal memory. Not used by stdio, 138 * but documented historically for certain applications. Bad applications. 139 */ 140 void 141 f_prealloc() 142 { 143 register struct glue *g; 144 int n; 145 146 n = getdtablesize() - FOPEN_MAX + 20; /* 20 for slop. */ 147 for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next) 148 /* void */; 149 if (n > 0) 150 g->next = moreglue(n); 151 } 152 153 /* 154 * exit() calls _cleanup() through *__cleanup, set whenever we 155 * open or buffer a file. This chicanery is done so that programs 156 * that do not use stdio need not link it all in. 157 * 158 * The name `_cleanup' is, alas, fairly well known outside stdio. 159 */ 160 void 161 _cleanup() 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 */ 170 void 171 __sinit() 172 { 173 /* make sure we clean up on exit */ 174 __cleanup = _cleanup; /* conservative */ 175 __sdidinit = 1; 176 } 177