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