* Copyright (c) 1983, 1985 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)findfp.c 5.6 (Berkeley) %G%";
#endif LIBC_SCCS and not lint
#define active(iop) ((iop)->_flag & (_IOREAD|_IOWRT|_IORW))
#define NSTATIC 20 /* stdin + stdout + stderr + the usual */
{ 0, NULL
, NULL
, 0, _IOREAD
, 0 }, /* stdin */
{ 0, NULL
, NULL
, 0, _IOWRT
, 1 }, /* stdout */
{ 0, NULL
, NULL
, 0, _IOWRT
|_IONBF
, 2 }, /* stderr */
static char sbuf
[NSTATIC
];
* Find a free FILE for fopen et al.
* We have a fixed static array of entries, and in addition
* may allocate additional entries dynamically, up to the kernel
* limit on the number of open files.
* At first just check for a free slot in the fixed static array.
* If none are available, then we allocate a structure to glue together
* the old and new FILE entries, which are then no longer contiguous.
register FILE **iov
, *iop
;
for (iop
= _iob
; iop
< _iob
+ NSTATIC
; iop
++)
if (_f_morefiles() == 0) {
while (*iov
!= NULL
&& active(*iov
))
*iov
= (FILE *)calloc(1, sizeof **iov
);
nfiles
= getdtablesize();
iobglue
= (FILE **)calloc(nfiles
, sizeof *iobglue
);
endglue
= iobglue
+ nfiles
;
for (fp
= _iob
, iov
= iobglue
; fp
< &_iob
[NSTATIC
]; /* void */)
_smallbuf
= calloc(nfiles
, sizeof(*_smallbuf
));
if (iobglue
== NULL
&& _f_morefiles() == 0)
for (iov
= iobglue
; iov
< endglue
; iov
++)
*iov
= (FILE *)calloc(1, sizeof **iov
);
register int (*function
)();
for (fp
= _iob
; fp
< &_iob
[NSTATIC
]; fp
++)
for (iov
= iobglue
; iov
< endglue
; iov
++)
if (*iov
&& active(*iov
))