xref: /onnv-gate/usr/src/lib/libbc/libc/sys/common/fdlist.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 1990 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #define NUM_FD	16
320Sstevel@tonic-gate 
33*722Smuffin struct fd_lst {
340Sstevel@tonic-gate         int     fd[NUM_FD];                 /* list of 16 descriptors */
350Sstevel@tonic-gate 	int 	fds[NUM_FD];
360Sstevel@tonic-gate         struct fd_lst *next;
370Sstevel@tonic-gate };
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static struct fd_lst *fdlist = NULL;
410Sstevel@tonic-gate static struct fd_lst *fdtail = NULL;
420Sstevel@tonic-gate 
43*722Smuffin void
fd_init(struct fd_lst * lst)44*722Smuffin fd_init(struct fd_lst *lst)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 	int i;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	for (i=0; i<NUM_FD; i++) {
490Sstevel@tonic-gate 		lst->fd[i] = -1;
500Sstevel@tonic-gate 		lst->fds[i] = -1;
510Sstevel@tonic-gate 	}
520Sstevel@tonic-gate 	lst->next = NULL;
530Sstevel@tonic-gate }
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 
57*722Smuffin int
fd_add(int fd,int fds)58*722Smuffin fd_add(int fd, int fds)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	int i;
610Sstevel@tonic-gate 	struct fd_lst *fdc, *fdnew;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	fdc = fdlist;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	while (fdc != NULL) {
660Sstevel@tonic-gate 		for (i=0; i<NUM_FD; i++) {
670Sstevel@tonic-gate 			if (fdc->fd[i] == -1) {
680Sstevel@tonic-gate 				fdc->fd[i] = fd;
690Sstevel@tonic-gate 				fdc->fds[i] = fds;
700Sstevel@tonic-gate 				return(0);
710Sstevel@tonic-gate 			}
720Sstevel@tonic-gate 		}
730Sstevel@tonic-gate 		fdc = fdc->next;
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if ((fdnew = (struct fd_lst *)malloc(sizeof(struct fd_lst))) == NULL) {
770Sstevel@tonic-gate 		fprintf(stderr,"fd_add: malloc failed\n");
780Sstevel@tonic-gate 		exit(1);
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	fd_init(fdnew);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	if (fdlist == NULL)
840Sstevel@tonic-gate 		fdlist = fdnew;
850Sstevel@tonic-gate 	else
860Sstevel@tonic-gate 		fdtail->next = fdnew;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	fdtail = fdnew;
890Sstevel@tonic-gate 	fdtail->fd[0] = fd;
900Sstevel@tonic-gate 	fdtail->fds[0] = fds;
91*722Smuffin 	return (0);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 
95*722Smuffin int
fd_rem(int fd)96*722Smuffin fd_rem(int fd)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	int i;
990Sstevel@tonic-gate 	struct fd_lst *fdc = fdlist;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	while (fdc != NULL) {
1020Sstevel@tonic-gate 		for (i=0; i<NUM_FD; i++) {
1030Sstevel@tonic-gate 			if (fdc->fd[i] == fd) {
1040Sstevel@tonic-gate 				fdc->fd[i] = -1;
1050Sstevel@tonic-gate 				fdc->fds[i] = -1;
106*722Smuffin 				return (0);
1070Sstevel@tonic-gate 			}
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 		fdc = fdc->next;
1100Sstevel@tonic-gate 	}
111*722Smuffin 	return (0);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 
115*722Smuffin int
fd_get(int fd)116*722Smuffin fd_get(int fd)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	int i;
1190Sstevel@tonic-gate 	struct fd_lst *fdc = fdlist;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	while (fdc != NULL) {
1220Sstevel@tonic-gate 		for (i=0; i<NUM_FD; i++) {
1230Sstevel@tonic-gate 			if (fdc->fd[i] == fd) {
124*722Smuffin 				return (fdc->fds[i]);
1250Sstevel@tonic-gate 			}
1260Sstevel@tonic-gate 		}
1270Sstevel@tonic-gate 		fdc = fdc->next;
1280Sstevel@tonic-gate 	}
129*722Smuffin 	return (-1);
1300Sstevel@tonic-gate }
131