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