xref: /netbsd-src/lib/libc/net/getnetent.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: getnetent.c,v 1.14 2000/04/25 13:26:03 itojun Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
36  *    Dep. Matematica Universidade de Coimbra, Portugal, Europe
37  *
38  * Permission to use, copy, modify, and distribute this software for any
39  * purpose with or without fee is hereby granted, provided that the above
40  * copyright notice and this permission notice appear in all copies.
41  *
42  * from getnetent.c   1.1 (Coimbra) 93/06/02
43  */
44 
45 #include <sys/cdefs.h>
46 #if defined(LIBC_SCCS) && !defined(lint)
47 #if 0
48 static char sccsid[] = "@(#)getnetent.c	8.1 (Berkeley) 6/4/93";
49 static char rcsid[] = "Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp ";
50 #else
51 __RCSID("$NetBSD: getnetent.c,v 1.14 2000/04/25 13:26:03 itojun Exp $");
52 #endif
53 #endif /* LIBC_SCCS and not lint */
54 
55 #include "namespace.h"
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60 #include <netdb.h>
61 #include <stdio.h>
62 #include <string.h>
63 
64 #ifdef __weak_alias
65 __weak_alias(endnetent,_endnetent)
66 __weak_alias(getnetent,_getnetent)
67 __weak_alias(setnetent,_setnetent)
68 #endif
69 
70 #define	MAXALIASES	35
71 
72 static FILE *netf;
73 static char line[BUFSIZ+1];
74 static struct netent net;
75 static char *net_aliases[MAXALIASES];
76 int _net_stayopen;
77 
78 static void __setnetent __P((int));
79 static void __endnetent __P((void));
80 
81 void
82 setnetent(stayopen)
83 	int stayopen;
84 {
85 
86 	sethostent(stayopen);
87 	__setnetent(stayopen);
88 }
89 
90 void
91 endnetent()
92 {
93 
94 	endhostent();
95 	__endnetent();
96 }
97 
98 static void
99 __setnetent(f)
100 	int f;
101 {
102 
103 	if (netf == NULL)
104 		netf = fopen(_PATH_NETWORKS, "r" );
105 	else
106 		rewind(netf);
107 	_net_stayopen |= f;
108 }
109 
110 static void
111 __endnetent()
112 {
113 
114 	if (netf) {
115 		fclose(netf);
116 		netf = NULL;
117 	}
118 	_net_stayopen = 0;
119 }
120 
121 struct netent *
122 getnetent()
123 {
124 	char *p;
125 	register char *cp, **q;
126 
127 	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
128 		return (NULL);
129 again:
130 	p = fgets(line, sizeof line, netf);
131 	if (p == NULL)
132 		return (NULL);
133 	if (*p == '#')
134 		goto again;
135 	cp = strpbrk(p, "#\n");
136 	if (cp == NULL)
137 		goto again;
138 	*cp = '\0';
139 	net.n_name = p;
140 	cp = strpbrk(p, " \t");
141 	if (cp == NULL)
142 		goto again;
143 	*cp++ = '\0';
144 	while (*cp == ' ' || *cp == '\t')
145 		cp++;
146 	p = strpbrk(cp, " \t");
147 	if (p != NULL)
148 		*p++ = '\0';
149 	net.n_net = inet_network(cp);
150 	net.n_addrtype = AF_INET;
151 	q = net.n_aliases = net_aliases;
152 	if (p != NULL) {
153 		cp = p;
154 		while (cp && *cp) {
155 			if (*cp == ' ' || *cp == '\t') {
156 				cp++;
157 				continue;
158 			}
159 			if (q < &net_aliases[MAXALIASES - 1])
160 				*q++ = cp;
161 			cp = strpbrk(cp, " \t");
162 			if (cp != NULL)
163 				*cp++ = '\0';
164 		}
165 	}
166 	*q = NULL;
167 	return (&net);
168 }
169