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