xref: /onnv-gate/usr/src/lib/libbc/libc/stdio/common/findiop.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 1987 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*      Copyright (c) 1984 AT&T */
280Sstevel@tonic-gate /*        All Rights Reserved   */
290Sstevel@tonic-gate 
30*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*LINTLIBRARY*/
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <errno.h>
35*722Smuffin #include <malloc.h>
360Sstevel@tonic-gate #include "iob.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define active(iop)	((iop)->_flag & (_IOREAD|_IOWRT|_IORW))
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static unsigned char sbuf[NSTATIC][_SBFSIZ];
410Sstevel@tonic-gate unsigned char (*_smbuf)[_SBFSIZ] = sbuf;
420Sstevel@tonic-gate static	FILE	**iobglue;
430Sstevel@tonic-gate static	FILE	**endglue;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Find a free FILE for fopen et al.
470Sstevel@tonic-gate  * We have a fixed static array of entries, and in addition
480Sstevel@tonic-gate  * may allocate additional entries dynamically, up to the kernel
490Sstevel@tonic-gate  * limit on the number of open files.
500Sstevel@tonic-gate  * At first just check for a free slot in the fixed static array.
510Sstevel@tonic-gate  * If none are available, then we allocate a structure to glue together
520Sstevel@tonic-gate  * the old and new FILE entries, which are then no longer contiguous.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate FILE *
_findiop(void)55*722Smuffin _findiop(void)
560Sstevel@tonic-gate {
57*722Smuffin 	FILE **iov, *iop;
58*722Smuffin 	FILE *fp;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	if(iobglue == NULL) {
610Sstevel@tonic-gate 		for(iop = _iob; iop < _iob + NSTATIC; iop++)
620Sstevel@tonic-gate 			if(!active(iop))
630Sstevel@tonic-gate 				return(iop);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 		if(_f_morefiles() == 0) {
660Sstevel@tonic-gate 			errno = ENOMEM;
670Sstevel@tonic-gate 			return(NULL);
680Sstevel@tonic-gate 		}
690Sstevel@tonic-gate 	}
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	iov = iobglue;
720Sstevel@tonic-gate 	while(*iov != NULL && active(*iov))
730Sstevel@tonic-gate 		if (++iov >= endglue) {
740Sstevel@tonic-gate 			errno = EMFILE;
750Sstevel@tonic-gate 			return(NULL);
760Sstevel@tonic-gate 		}
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	if(*iov == NULL)
790Sstevel@tonic-gate 		*iov = (FILE *)calloc(1, sizeof **iov);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	return(*iov);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
84*722Smuffin int
_f_morefiles(void)85*722Smuffin _f_morefiles(void)
860Sstevel@tonic-gate {
87*722Smuffin 	FILE **iov;
88*722Smuffin 	FILE *fp;
89*722Smuffin 	unsigned char *cp;
900Sstevel@tonic-gate 	int nfiles;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	nfiles = getdtablesize();
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	iobglue = (FILE **)calloc(nfiles, sizeof *iobglue);
950Sstevel@tonic-gate 	if(iobglue == NULL)
960Sstevel@tonic-gate 		return(0);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if((_smbuf = (unsigned char (*)[_SBFSIZ])malloc(nfiles * sizeof *_smbuf)) == NULL) {
990Sstevel@tonic-gate 		free((char *)iobglue);
1000Sstevel@tonic-gate 		iobglue = NULL;
1010Sstevel@tonic-gate 		return(0);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	endglue = iobglue + nfiles;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	for(fp = _iob, iov = iobglue; fp < &_iob[NSTATIC]; /* void */)
1070Sstevel@tonic-gate 		*iov++ = fp++;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	return(1);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
112*722Smuffin void
f_prealloc(void)113*722Smuffin f_prealloc(void)
1140Sstevel@tonic-gate {
115*722Smuffin 	FILE **iov;
116*722Smuffin 	FILE *fp;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if(iobglue == NULL && _f_morefiles() == 0)
1190Sstevel@tonic-gate 		return;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	for(iov = iobglue; iov < endglue; iov++)
1220Sstevel@tonic-gate 		if(*iov == NULL)
1230Sstevel@tonic-gate 			*iov = (FILE *)calloc(1, sizeof **iov);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate void
_fwalk(int (* function)(FILE *))127*722Smuffin _fwalk(int (*function)(FILE *))
1280Sstevel@tonic-gate {
129*722Smuffin 	FILE **iov;
130*722Smuffin 	FILE *fp;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if(function == NULL)
1330Sstevel@tonic-gate 		return;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if(iobglue == NULL) {
1360Sstevel@tonic-gate 		for(fp = _iob; fp < &_iob[NSTATIC]; fp++)
1370Sstevel@tonic-gate 			if(active(fp))
1380Sstevel@tonic-gate 				(*function)(fp);
1390Sstevel@tonic-gate 	} else {
1400Sstevel@tonic-gate 		for(iov = iobglue; iov < endglue; iov++)
1410Sstevel@tonic-gate 			if(*iov && active(*iov))
1420Sstevel@tonic-gate 				(*function)(*iov);
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate }
145