1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #if defined(LIBC_SCCS) && !defined(lint) 12 static char sccsid[] = "@(#)findfp.c 5.9 (Berkeley) 02/01/91"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <unistd.h> 18 #include <errno.h> 19 #include "local.h" 20 #include "glue.h" 21 22 int __sdidinit; 23 24 #define NSTATIC 20 /* stdin + stdout + stderr + the usual */ 25 #define NDYNAMIC 10 /* add ten more whenever necessary */ 26 27 #define std(flags, file) \ 28 {0,0,0,flags,file,{0},0,__sF+file,__sclose,__sread,__sseek,__swrite} 29 /* p r w flags file _bf z cookie close read seek write */ 30 31 static FILE usual[NSTATIC - 3]; /* the usual */ 32 static struct glue uglue = { 0, NSTATIC - 3, usual }; 33 34 FILE __sF[3] = { 35 std(__SRD, STDIN_FILENO), /* stdin */ 36 std(__SWR, STDOUT_FILENO), /* stdout */ 37 std(__SWR|__SNBF, STDERR_FILENO) /* stderr */ 38 }; 39 struct glue __sglue = { &uglue, 3, __sF }; 40 41 static struct glue * 42 moreglue(n) 43 register int n; 44 { 45 register struct glue *g; 46 register FILE *p; 47 static FILE empty; 48 49 g = (struct glue *)malloc(sizeof(*g) + n * sizeof(FILE)); 50 if (g == NULL) 51 return (NULL); 52 p = (FILE *)(g + 1); 53 g->next = NULL; 54 g->niobs = n; 55 g->iobs = p; 56 while (--n >= 0) 57 *p++ = empty; 58 return (g); 59 } 60 61 /* 62 * Find a free FILE for fopen et al. 63 */ 64 FILE * 65 __sfp() 66 { 67 register FILE *fp; 68 register int n; 69 register struct glue *g; 70 71 if (!__sdidinit) 72 __sinit(); 73 for (g = &__sglue;; g = g->next) { 74 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) 75 if (fp->_flags == 0) 76 goto found; 77 if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL) 78 break; 79 } 80 return (NULL); 81 found: 82 fp->_flags = 1; /* reserve this slot; caller sets real flags */ 83 fp->_p = NULL; /* no current pointer */ 84 fp->_w = 0; /* nothing to read or write */ 85 fp->_r = 0; 86 fp->_bf._base = NULL; /* no buffer */ 87 fp->_bf._size = 0; 88 fp->_lbfsize = 0; /* not line buffered */ 89 fp->_file = -1; /* no file */ 90 /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */ 91 fp->_ub._base = NULL; /* no ungetc buffer */ 92 fp->_ub._size = 0; 93 fp->_lb._base = NULL; /* no line buffer */ 94 fp->_lb._size = 0; 95 return (fp); 96 } 97 98 /* 99 * XXX. Force immediate allocation of internal memory. Not used by stdio, 100 * but documented historically for certain applications. Bad applications. 101 */ 102 f_prealloc() 103 { 104 int n = getdtablesize() - NSTATIC + 20; /* 20 for slop */ 105 register struct glue *g; 106 107 for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next) 108 /* void */; 109 if (n > 0) 110 g->next = moreglue(n); 111 } 112 113 /* 114 * exit() calls _cleanup() through *__cleanup, set whenever we 115 * open or buffer a file. This chicanery is done so that programs 116 * that do not use stdio need not link it all in. 117 * 118 * The name `_cleanup' is, alas, fairly well known outside stdio. 119 */ 120 void 121 _cleanup() 122 { 123 /* (void) _fwalk(fclose); */ 124 (void) _fwalk(__sflush); /* `cheating' */ 125 } 126 127 /* 128 * __sinit() is called whenever stdio's internal variables must be set up. 129 */ 130 void 131 __sinit() 132 { 133 /* make sure we clean up on exit */ 134 __cleanup = _cleanup; /* conservative */ 135 __sdidinit = 1; 136 } 137