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