getnetent.c revision 1.8
1/*	$NetBSD: getnetent.c,v 1.8 1997/04/13 10:30:37 mrg 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#if defined(LIBC_SCCS) && !defined(lint)
46#if 0
47static char sccsid[] = "@(#)getnetent.c	8.1 (Berkeley) 6/4/93";
48static char rcsid[] = "Id: getnetent.c,v 8.3 1996/08/05 08:31:35 vixie Exp";
49#else
50static char rcsid[] = "$NetBSD: getnetent.c,v 1.8 1997/04/13 10:30:37 mrg Exp $";
51#endif
52#endif /* LIBC_SCCS and not lint */
53
54#include <sys/types.h>
55#include <sys/socket.h>
56#include <netinet/in.h>
57#include <arpa/inet.h>
58#include <netdb.h>
59#include <stdio.h>
60#include <string.h>
61
62#define	MAXALIASES	35
63
64static FILE *netf;
65static char line[BUFSIZ+1];
66static struct netent net;
67static char *net_aliases[MAXALIASES];
68int _net_stayopen;
69
70void _setnetent __P((int));
71void _endnetent __P((void));
72
73void
74setnetent(stayopen)
75	int stayopen;
76{
77
78	sethostent(stayopen);
79	_setnetent(stayopen);
80}
81
82void
83endnetent()
84{
85
86	endhostent();
87	_endnetent();
88}
89
90void
91_setnetent(f)
92	int f;
93{
94
95	if (netf == NULL)
96		netf = fopen(_PATH_NETWORKS, "r" );
97	else
98		rewind(netf);
99	_net_stayopen |= f;
100}
101
102void
103_endnetent()
104{
105
106	if (netf) {
107		fclose(netf);
108		netf = NULL;
109	}
110	_net_stayopen = 0;
111}
112
113struct netent *
114getnetent()
115{
116	char *p;
117	register char *cp, **q;
118
119	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
120		return (NULL);
121again:
122	p = fgets(line, BUFSIZ, netf);
123	if (p == NULL)
124		return (NULL);
125	if (*p == '#')
126		goto again;
127	cp = strpbrk(p, "#\n");
128	if (cp == NULL)
129		goto again;
130	*cp = '\0';
131	net.n_name = p;
132	cp = strpbrk(p, " \t");
133	if (cp == NULL)
134		goto again;
135	*cp++ = '\0';
136	while (*cp == ' ' || *cp == '\t')
137		cp++;
138	p = strpbrk(cp, " \t");
139	if (p != NULL)
140		*p++ = '\0';
141	net.n_net = inet_network(cp);
142	net.n_addrtype = AF_INET;
143	q = net.n_aliases = net_aliases;
144	if (p != NULL)  {
145		cp = p;
146		while (cp && *cp) {
147			if (*cp == ' ' || *cp == '\t') {
148				cp++;
149				continue;
150			}
151			if (q < &net_aliases[MAXALIASES - 1])
152				*q++ = cp;
153			cp = strpbrk(cp, " \t");
154			if (cp != NULL)
155				*cp++ = '\0';
156		}
157	}
158	*q = NULL;
159	return (&net);
160}
161