1 /* $NetBSD: findfp.c,v 1.25 2010/09/06 14:52:55 christos Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94";
40 __RCSID("$NetBSD: findfp.c,v 1.25 2010/09/06 14:52:55 christos Exp $");
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
45 #include <sys/param.h>
51 #include "reentrant.h"
57 #define NDYNAMIC 10 /* add ten more whenever necessary */
59 #define std(flags, file) \
60 /* p r w flags file bf lfbsize cookie close */ \
61 { NULL, 0, 0, flags, file, { NULL, 0 }, 0, __sF + file, __sclose, \
62 /* read seek write ext up */ \
63 __sread, __sseek, __swrite, { (void *)(__sFext + file), 0 }, NULL, \
64 /* ur ubuf, nbuf lb blksize offset */ \
65 0, { '\0', '\0', '\0' }, { '\0' }, { NULL, 0 }, 0, (fpos_t)0 }
67 /* the usual - (stdin + stdout + stderr) */
68 static FILE usual
[FOPEN_MAX
- 3];
69 static struct __sfileext usualext
[FOPEN_MAX
- 3];
70 static struct glue uglue
= { 0, FOPEN_MAX
- 3, usual
};
72 #if defined(_REENTRANT) && !defined(__lint__) /* XXX lint is busted */
73 #define STDEXT { ._lock = MUTEX_INITIALIZER, ._lockcond = COND_INITIALIZER }
74 struct __sfileext __sFext
[3] = { STDEXT
,
78 struct __sfileext __sFext
[3];
82 std(__SRD
, STDIN_FILENO
), /* stdin */
83 std(__SWR
, STDOUT_FILENO
), /* stdout */
84 std(__SWR
|__SNBF
, STDERR_FILENO
) /* stderr */
86 struct glue __sglue
= { &uglue
, 3, __sF
};
88 static struct glue
*moreglue
__P((int));
89 void f_prealloc
__P((void));
92 rwlock_t __sfp_lock
= RWLOCK_INITIALIZER
;
101 struct __sfileext
*pext
;
104 g
= (struct glue
*)malloc(sizeof(*g
) + ALIGNBYTES
+ n
* sizeof(FILE)
105 + n
* sizeof(struct __sfileext
));
108 p
= (FILE *)ALIGN((u_long
)(g
+ 1));
112 pext
= (void *)(p
+ n
);
115 _FILEEXT_SETUP(p
, pext
);
125 fp
->_flags
= 1; /* reserve this slot; caller sets real flags */
126 fp
->_p
= NULL
; /* no current pointer */
127 fp
->_w
= 0; /* nothing to read or write */
129 fp
->_bf
._base
= NULL
; /* no buffer */
131 fp
->_lbfsize
= 0; /* not line buffered */
132 fp
->_file
= -1; /* no file */
133 /* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
134 _UB(fp
)._base
= NULL
; /* no ungetc buffer */
136 memset(WCIO_GET(fp
), 0, sizeof(struct wchar_io_data
));
140 * Find a free FILE for fopen et al.
152 rwlock_wrlock(&__sfp_lock
);
153 for (g
= &__sglue
;; g
= g
->next
) {
154 for (fp
= g
->iobs
, n
= g
->niobs
; --n
>= 0; fp
++)
157 if (g
->next
== NULL
&& (g
->next
= moreglue(NDYNAMIC
)) == NULL
)
160 rwlock_unlock(&__sfp_lock
);
164 rwlock_unlock(&__sfp_lock
);
169 * XXX. Force immediate allocation of internal memory. Not used by stdio,
170 * but documented historically for certain applications. Bad applications.
178 n
= (int)sysconf(_SC_OPEN_MAX
) - FOPEN_MAX
+ 20; /* 20 for slop. */
179 for (g
= &__sglue
; (n
-= g
->niobs
) > 0 && g
->next
; g
= g
->next
)
182 g
->next
= moreglue(n
);
186 * exit() calls _cleanup() through *__cleanup, set whenever we
187 * open or buffer a file. This chicanery is done so that programs
188 * that do not use stdio need not link it all in.
190 * The name `_cleanup' is, alas, fairly well known outside stdio.
195 /* (void) _fwalk(fclose); */
196 (void) fflush(NULL
); /* `cheating' */
200 * __sinit() is called whenever stdio's internal variables must be set up.
207 for (i
= 0; i
< FOPEN_MAX
- 3; i
++)
208 _FILEEXT_SETUP(&usual
[i
], &usualext
[i
]);
210 /* make sure we clean up on exit */
211 __cleanup
= _cleanup
; /* conservative */