xref: /titanic_41/usr/src/lib/libbc/libc/sys/common/fdlist.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1990 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 
31 #define NUM_FD	16
32 
33 struct fd_lst {
34         int     fd[NUM_FD];                 /* list of 16 descriptors */
35 	int 	fds[NUM_FD];
36         struct fd_lst *next;
37 };
38 
39 
40 static struct fd_lst *fdlist = NULL;
41 static struct fd_lst *fdtail = NULL;
42 
43 void
fd_init(struct fd_lst * lst)44 fd_init(struct fd_lst *lst)
45 {
46 	int i;
47 
48 	for (i=0; i<NUM_FD; i++) {
49 		lst->fd[i] = -1;
50 		lst->fds[i] = -1;
51 	}
52 	lst->next = NULL;
53 }
54 
55 
56 
57 int
fd_add(int fd,int fds)58 fd_add(int fd, int fds)
59 {
60 	int i;
61 	struct fd_lst *fdc, *fdnew;
62 
63 	fdc = fdlist;
64 
65 	while (fdc != NULL) {
66 		for (i=0; i<NUM_FD; i++) {
67 			if (fdc->fd[i] == -1) {
68 				fdc->fd[i] = fd;
69 				fdc->fds[i] = fds;
70 				return(0);
71 			}
72 		}
73 		fdc = fdc->next;
74 	}
75 
76 	if ((fdnew = (struct fd_lst *)malloc(sizeof(struct fd_lst))) == NULL) {
77 		fprintf(stderr,"fd_add: malloc failed\n");
78 		exit(1);
79 	}
80 
81 	fd_init(fdnew);
82 
83 	if (fdlist == NULL)
84 		fdlist = fdnew;
85 	else
86 		fdtail->next = fdnew;
87 
88 	fdtail = fdnew;
89 	fdtail->fd[0] = fd;
90 	fdtail->fds[0] = fds;
91 	return (0);
92 }
93 
94 
95 int
fd_rem(int fd)96 fd_rem(int fd)
97 {
98 	int i;
99 	struct fd_lst *fdc = fdlist;
100 
101 	while (fdc != NULL) {
102 		for (i=0; i<NUM_FD; i++) {
103 			if (fdc->fd[i] == fd) {
104 				fdc->fd[i] = -1;
105 				fdc->fds[i] = -1;
106 				return (0);
107 			}
108 		}
109 		fdc = fdc->next;
110 	}
111 	return (0);
112 }
113 
114 
115 int
fd_get(int fd)116 fd_get(int fd)
117 {
118 	int i;
119 	struct fd_lst *fdc = fdlist;
120 
121 	while (fdc != NULL) {
122 		for (i=0; i<NUM_FD; i++) {
123 			if (fdc->fd[i] == fd) {
124 				return (fdc->fds[i]);
125 			}
126 		}
127 		fdc = fdc->next;
128 	}
129 	return (-1);
130 }
131