1 1.28 christos /* $NetBSD: findfp.c,v 1.28 2012/03/27 15:05:42 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.20 agc * 3. Neither the name of the University nor the names of its contributors 19 1.1 cgd * may be used to endorse or promote products derived from this software 20 1.1 cgd * without specific prior written permission. 21 1.1 cgd * 22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 1.1 cgd * SUCH DAMAGE. 33 1.1 cgd */ 34 1.1 cgd 35 1.7 christos #include <sys/cdefs.h> 36 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint) 37 1.6 jtc #if 0 38 1.6 jtc static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94"; 39 1.7 christos #else 40 1.28 christos __RCSID("$NetBSD: findfp.c,v 1.28 2012/03/27 15:05:42 christos Exp $"); 41 1.6 jtc #endif 42 1.1 cgd #endif /* LIBC_SCCS and not lint */ 43 1.1 cgd 44 1.10 kleink #include "namespace.h" 45 1.5 cgd #include <sys/param.h> 46 1.1 cgd #include <unistd.h> 47 1.1 cgd #include <stdio.h> 48 1.1 cgd #include <errno.h> 49 1.1 cgd #include <stdlib.h> 50 1.1 cgd #include <string.h> 51 1.16 thorpej #include "reentrant.h" 52 1.1 cgd #include "local.h" 53 1.1 cgd #include "glue.h" 54 1.1 cgd 55 1.1 cgd int __sdidinit; 56 1.1 cgd 57 1.6 jtc #define NDYNAMIC 10 /* add ten more whenever necessary */ 58 1.1 cgd 59 1.28 christos #define std(flags, file) { \ 60 1.28 christos ._p = NULL, \ 61 1.28 christos ._r = 0, \ 62 1.28 christos ._w = 0, \ 63 1.28 christos ._flags = (flags), \ 64 1.28 christos ._file = (file), \ 65 1.28 christos ._bf = { ._base = NULL, ._size = 0 }, \ 66 1.28 christos ._lbfsize = 0, \ 67 1.28 christos ._cookie = __sF + (file), \ 68 1.28 christos ._close = __sclose, \ 69 1.28 christos ._read = __sread, \ 70 1.28 christos ._seek = __sseek, \ 71 1.28 christos ._write = __swrite, \ 72 1.28 christos ._ext = { ._base = (void *)(__sFext + (file)), ._size = 0 }, \ 73 1.28 christos ._up = NULL, \ 74 1.28 christos ._ur = 0, \ 75 1.28 christos ._ubuf = { [0] = '\0', [1] = '\0', [2] = '\0' }, \ 76 1.28 christos ._nbuf = { [0] = '\0' }, \ 77 1.28 christos ._flush = NULL, \ 78 1.28 christos ._lb_unused = { '\0' }, \ 79 1.28 christos ._blksize = 0, \ 80 1.28 christos ._offset = (off_t)0, \ 81 1.28 christos } 82 1.1 cgd 83 1.6 jtc /* the usual - (stdin + stdout + stderr) */ 84 1.6 jtc static FILE usual[FOPEN_MAX - 3]; 85 1.14 yamt static struct __sfileext usualext[FOPEN_MAX - 3]; 86 1.6 jtc static struct glue uglue = { 0, FOPEN_MAX - 3, usual }; 87 1.1 cgd 88 1.23 thorpej #if defined(_REENTRANT) && !defined(__lint__) /* XXX lint is busted */ 89 1.22 thorpej #define STDEXT { ._lock = MUTEX_INITIALIZER, ._lockcond = COND_INITIALIZER } 90 1.18 nathanw struct __sfileext __sFext[3] = { STDEXT, 91 1.18 nathanw STDEXT, 92 1.18 nathanw STDEXT}; 93 1.18 nathanw #else 94 1.14 yamt struct __sfileext __sFext[3]; 95 1.18 nathanw #endif 96 1.14 yamt 97 1.1 cgd FILE __sF[3] = { 98 1.1 cgd std(__SRD, STDIN_FILENO), /* stdin */ 99 1.1 cgd std(__SWR, STDOUT_FILENO), /* stdout */ 100 1.1 cgd std(__SWR|__SNBF, STDERR_FILENO) /* stderr */ 101 1.1 cgd }; 102 1.1 cgd struct glue __sglue = { &uglue, 3, __sF }; 103 1.7 christos 104 1.27 christos void f_prealloc(void); 105 1.1 cgd 106 1.16 thorpej #ifdef _REENTRANT 107 1.8 jtc rwlock_t __sfp_lock = RWLOCK_INITIALIZER; 108 1.8 jtc #endif 109 1.8 jtc 110 1.1 cgd static struct glue * 111 1.27 christos moreglue(int n) 112 1.1 cgd { 113 1.9 perry struct glue *g; 114 1.9 perry FILE *p; 115 1.14 yamt struct __sfileext *pext; 116 1.1 cgd static FILE empty; 117 1.1 cgd 118 1.28 christos g = malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE) 119 1.28 christos + n * sizeof(struct __sfileext)); 120 1.1 cgd if (g == NULL) 121 1.27 christos return NULL; 122 1.12 christos p = (FILE *)ALIGN((u_long)(g + 1)); 123 1.1 cgd g->next = NULL; 124 1.1 cgd g->niobs = n; 125 1.1 cgd g->iobs = p; 126 1.14 yamt pext = (void *)(p + n); 127 1.14 yamt while (--n >= 0) { 128 1.14 yamt *p = empty; 129 1.14 yamt _FILEEXT_SETUP(p, pext); 130 1.14 yamt p++; 131 1.14 yamt pext++; 132 1.14 yamt } 133 1.27 christos return g; 134 1.1 cgd } 135 1.1 cgd 136 1.25 christos void 137 1.25 christos __sfpinit(FILE *fp) 138 1.25 christos { 139 1.25 christos fp->_flags = 1; /* reserve this slot; caller sets real flags */ 140 1.25 christos fp->_p = NULL; /* no current pointer */ 141 1.25 christos fp->_w = 0; /* nothing to read or write */ 142 1.25 christos fp->_r = 0; 143 1.25 christos fp->_bf._base = NULL; /* no buffer */ 144 1.25 christos fp->_bf._size = 0; 145 1.25 christos fp->_lbfsize = 0; /* not line buffered */ 146 1.25 christos fp->_file = -1; /* no file */ 147 1.25 christos /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */ 148 1.28 christos fp->_flush = NULL; /* default flush */ 149 1.25 christos _UB(fp)._base = NULL; /* no ungetc buffer */ 150 1.25 christos _UB(fp)._size = 0; 151 1.25 christos memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data)); 152 1.25 christos } 153 1.25 christos 154 1.1 cgd /* 155 1.1 cgd * Find a free FILE for fopen et al. 156 1.1 cgd */ 157 1.1 cgd FILE * 158 1.27 christos __sfp(void) 159 1.1 cgd { 160 1.9 perry FILE *fp; 161 1.9 perry int n; 162 1.9 perry struct glue *g; 163 1.1 cgd 164 1.1 cgd if (!__sdidinit) 165 1.1 cgd __sinit(); 166 1.8 jtc 167 1.8 jtc rwlock_wrlock(&__sfp_lock); 168 1.1 cgd for (g = &__sglue;; g = g->next) { 169 1.1 cgd for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) 170 1.1 cgd if (fp->_flags == 0) 171 1.1 cgd goto found; 172 1.1 cgd if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL) 173 1.1 cgd break; 174 1.1 cgd } 175 1.8 jtc rwlock_unlock(&__sfp_lock); 176 1.27 christos return NULL; 177 1.1 cgd found: 178 1.25 christos __sfpinit(fp); 179 1.8 jtc rwlock_unlock(&__sfp_lock); 180 1.27 christos return fp; 181 1.1 cgd } 182 1.1 cgd 183 1.1 cgd /* 184 1.1 cgd * XXX. Force immediate allocation of internal memory. Not used by stdio, 185 1.1 cgd * but documented historically for certain applications. Bad applications. 186 1.1 cgd */ 187 1.4 jtc void 188 1.27 christos f_prealloc(void) 189 1.1 cgd { 190 1.9 perry struct glue *g; 191 1.6 jtc int n; 192 1.1 cgd 193 1.11 kleink n = (int)sysconf(_SC_OPEN_MAX) - FOPEN_MAX + 20; /* 20 for slop. */ 194 1.1 cgd for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next) 195 1.27 christos continue; 196 1.1 cgd if (n > 0) 197 1.1 cgd g->next = moreglue(n); 198 1.1 cgd } 199 1.1 cgd 200 1.1 cgd /* 201 1.1 cgd * exit() calls _cleanup() through *__cleanup, set whenever we 202 1.1 cgd * open or buffer a file. This chicanery is done so that programs 203 1.1 cgd * that do not use stdio need not link it all in. 204 1.1 cgd * 205 1.1 cgd * The name `_cleanup' is, alas, fairly well known outside stdio. 206 1.1 cgd */ 207 1.1 cgd void 208 1.27 christos _cleanup(void) 209 1.1 cgd { 210 1.1 cgd /* (void) _fwalk(fclose); */ 211 1.13 mycroft (void) fflush(NULL); /* `cheating' */ 212 1.1 cgd } 213 1.1 cgd 214 1.1 cgd /* 215 1.1 cgd * __sinit() is called whenever stdio's internal variables must be set up. 216 1.1 cgd */ 217 1.1 cgd void 218 1.27 christos __sinit(void) 219 1.1 cgd { 220 1.14 yamt int i; 221 1.14 yamt 222 1.14 yamt for (i = 0; i < FOPEN_MAX - 3; i++) 223 1.14 yamt _FILEEXT_SETUP(&usual[i], &usualext[i]); 224 1.14 yamt 225 1.1 cgd /* make sure we clean up on exit */ 226 1.1 cgd __cleanup = _cleanup; /* conservative */ 227 1.1 cgd __sdidinit = 1; 228 1.1 cgd } 229