1 /* $NetBSD: findfp.c,v 1.14 2001/12/07 11:47:41 yamt 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.14 2001/12/07 11:47:41 yamt Exp $"); 45 #endif 46 #endif /* LIBC_SCCS and not lint */ 47 48 #include "namespace.h" 49 #include <sys/param.h> 50 #include <unistd.h> 51 #include <stdio.h> 52 #include <errno.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include "local.h" 56 #include "glue.h" 57 #include "reentrant.h" 58 59 int __sdidinit; 60 61 #define NDYNAMIC 10 /* add ten more whenever necessary */ 62 63 #define std(flags, file) \ 64 {0,0,0,flags,file,{0},0,__sF+file,__sclose,__sread,__sseek,__swrite, \ 65 {(void*)(__sFext+file)}} 66 /* p r w flags file _bf z cookie close read seek write, 67 _ext */ 68 69 /* the usual - (stdin + stdout + stderr) */ 70 static FILE usual[FOPEN_MAX - 3]; 71 static struct __sfileext usualext[FOPEN_MAX - 3]; 72 static struct glue uglue = { 0, FOPEN_MAX - 3, usual }; 73 74 struct __sfileext __sFext[3]; 75 76 FILE __sF[3] = { 77 std(__SRD, STDIN_FILENO), /* stdin */ 78 std(__SWR, STDOUT_FILENO), /* stdout */ 79 std(__SWR|__SNBF, STDERR_FILENO) /* stderr */ 80 }; 81 struct glue __sglue = { &uglue, 3, __sF }; 82 83 static struct glue *moreglue __P((int)); 84 void f_prealloc __P((void)); 85 86 #ifdef _REENT 87 rwlock_t __sfp_lock = RWLOCK_INITIALIZER; 88 #endif 89 90 static struct glue * 91 moreglue(n) 92 int n; 93 { 94 struct glue *g; 95 FILE *p; 96 struct __sfileext *pext; 97 static FILE empty; 98 99 g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE) 100 + n * sizeof(struct __sfileext)); 101 if (g == NULL) 102 return (NULL); 103 p = (FILE *)ALIGN((u_long)(g + 1)); 104 g->next = NULL; 105 g->niobs = n; 106 g->iobs = p; 107 pext = (void *)(p + n); 108 while (--n >= 0) { 109 *p = empty; 110 _FILEEXT_SETUP(p, pext); 111 p++; 112 pext++; 113 } 114 return (g); 115 } 116 117 /* 118 * Find a free FILE for fopen et al. 119 */ 120 FILE * 121 __sfp() 122 { 123 FILE *fp; 124 int n; 125 struct glue *g; 126 127 if (!__sdidinit) 128 __sinit(); 129 130 rwlock_wrlock(&__sfp_lock); 131 for (g = &__sglue;; g = g->next) { 132 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) 133 if (fp->_flags == 0) 134 goto found; 135 if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL) 136 break; 137 } 138 rwlock_unlock(&__sfp_lock); 139 return (NULL); 140 found: 141 fp->_flags = 1; /* reserve this slot; caller sets real flags */ 142 fp->_p = NULL; /* no current pointer */ 143 fp->_w = 0; /* nothing to read or write */ 144 fp->_r = 0; 145 fp->_bf._base = NULL; /* no buffer */ 146 fp->_bf._size = 0; 147 fp->_lbfsize = 0; /* not line buffered */ 148 fp->_file = -1; /* no file */ 149 /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */ 150 _UB(fp)._base = NULL; /* no ungetc buffer */ 151 _UB(fp)._size = 0; 152 fp->_lb._base = NULL; /* no line buffer */ 153 fp->_lb._size = 0; 154 memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data)); 155 rwlock_unlock(&__sfp_lock); 156 return (fp); 157 } 158 159 /* 160 * XXX. Force immediate allocation of internal memory. Not used by stdio, 161 * but documented historically for certain applications. Bad applications. 162 */ 163 void 164 f_prealloc() 165 { 166 struct glue *g; 167 int n; 168 169 n = (int)sysconf(_SC_OPEN_MAX) - FOPEN_MAX + 20; /* 20 for slop. */ 170 for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next) 171 /* void */; 172 if (n > 0) 173 g->next = moreglue(n); 174 } 175 176 /* 177 * exit() calls _cleanup() through *__cleanup, set whenever we 178 * open or buffer a file. This chicanery is done so that programs 179 * that do not use stdio need not link it all in. 180 * 181 * The name `_cleanup' is, alas, fairly well known outside stdio. 182 */ 183 void 184 _cleanup() 185 { 186 /* (void) _fwalk(fclose); */ 187 (void) fflush(NULL); /* `cheating' */ 188 } 189 190 /* 191 * __sinit() is called whenever stdio's internal variables must be set up. 192 */ 193 void 194 __sinit() 195 { 196 int i; 197 198 for (i = 0; i < FOPEN_MAX - 3; i++) 199 _FILEEXT_SETUP(&usual[i], &usualext[i]); 200 201 /* make sure we clean up on exit */ 202 __cleanup = _cleanup; /* conservative */ 203 __sdidinit = 1; 204 } 205