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 * Redistribution and use in source and binary forms, with or without --- 25 unchanged lines hidden (view full) --- 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38#if 0 39static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94"; 40#endif 41static const char rcsid[] = |
42 "$FreeBSD: head/lib/libc/stdio/findfp.c 72529 2001-02-16 06:11:22Z imp $"; |
43#endif /* LIBC_SCCS and not lint */ 44 45#include <sys/param.h> 46#include <machine/atomic.h> 47#include <unistd.h> 48#include <stdio.h> 49#include <stdlib.h> 50#include <string.h> 51 52#include <spinlock.h> 53 54#include "libc_private.h" 55#include "local.h" 56#include "glue.h" 57 58int __sdidinit; 59 60#define NDYNAMIC 10 /* add ten more whenever necessary */ 61 |
62#define std(flags, file) \ 63 {0,0,0,flags,file,{0},0,__sF+file,__sclose,__sread,__sseek,__swrite, \ 64 {0}, __sFX + file} 65 /* p r w flags file _bf z cookie close read seek write */ 66 /* _ub _extra */ |
67 /* the usual - (stdin + stdout + stderr) */ 68static FILE usual[FOPEN_MAX - 3]; 69static struct glue uglue = { NULL, FOPEN_MAX - 3, usual }; 70 |
71static struct __sFILEX __sFX[3]; |
72 |
73FILE __sF[3] = { 74 std(__SRD, STDIN_FILENO), 75 std(__SWR, STDOUT_FILENO), 76 std(__SWR|__SNBF, STDERR_FILENO) 77}; 78 79struct glue __sglue = { &uglue, 3, __sF }; |
80static struct glue *lastglue = &uglue; 81 82static struct glue * moreglue __P((int)); 83 84static spinlock_t thread_lock = _SPINLOCK_INITIALIZER; 85#define THREAD_LOCK() if (__isthreaded) _SPINLOCK(&thread_lock) 86#define THREAD_UNLOCK() if (__isthreaded) _SPINUNLOCK(&thread_lock) 87 88#if NOT_YET 89#define SET_GLUE_PTR(ptr, val) atomic_set_rel_ptr(&(ptr), (uintptr_t)(val)) 90#else 91#define SET_GLUE_PTR(ptr, val) ptr = val 92#endif 93 94static struct glue * 95moreglue(n) 96 int n; 97{ 98 struct glue *g; |
99 static FILE empty; |
100 static struct __sFILEX emptyx; 101 FILE *p; 102 struct __sFILEX *fx; |
103 |
104 g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE) + 105 n * sizeof(struct __sFILEX)); |
106 if (g == NULL) 107 return (NULL); 108 p = (FILE *)ALIGN(g + 1); |
109 fx = (struct __sFILEX *)&p[n]; |
110 g->next = NULL; 111 g->niobs = n; 112 g->iobs = p; |
113 while (--n >= 0) { 114 *p = empty; 115 p->_extra = fx; 116 *p->_extra = emptyx; 117 p++, fx++; 118 } |
119 return (g); 120} 121 122/* 123 * Find a free FILE for fopen et al. 124 */ 125FILE * 126__sfp() --- 95 unchanged lines hidden --- |