xref: /original-bsd/lib/libc/net/getnetent.c (revision 7dc4431a)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char sccsid[] = "@(#)getnetent.c	5.6 (Berkeley) 05/21/90";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netdb.h>
26 #include <ctype.h>
27 
28 #define	MAXALIASES	35
29 
30 static FILE *netf = NULL;
31 static char line[BUFSIZ+1];
32 static struct netent net;
33 static char *net_aliases[MAXALIASES];
34 int _net_stayopen;
35 static char *any();
36 
37 setnetent(f)
38 	int f;
39 {
40 	if (netf == NULL)
41 		netf = fopen(_PATH_NETWORKS, "r" );
42 	else
43 		rewind(netf);
44 	_net_stayopen |= f;
45 }
46 
47 endnetent()
48 {
49 	if (netf) {
50 		fclose(netf);
51 		netf = NULL;
52 	}
53 	_net_stayopen = 0;
54 }
55 
56 struct netent *
57 getnetent()
58 {
59 	char *p;
60 	register char *cp, **q;
61 
62 	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
63 		return (NULL);
64 again:
65 	p = fgets(line, BUFSIZ, netf);
66 	if (p == NULL)
67 		return (NULL);
68 	if (*p == '#')
69 		goto again;
70 	cp = any(p, "#\n");
71 	if (cp == NULL)
72 		goto again;
73 	*cp = '\0';
74 	net.n_name = p;
75 	cp = any(p, " \t");
76 	if (cp == NULL)
77 		goto again;
78 	*cp++ = '\0';
79 	while (*cp == ' ' || *cp == '\t')
80 		cp++;
81 	p = any(cp, " \t");
82 	if (p != NULL)
83 		*p++ = '\0';
84 	net.n_net = inet_network(cp);
85 	net.n_addrtype = AF_INET;
86 	q = net.n_aliases = net_aliases;
87 	if (p != NULL)
88 		cp = p;
89 	while (cp && *cp) {
90 		if (*cp == ' ' || *cp == '\t') {
91 			cp++;
92 			continue;
93 		}
94 		if (q < &net_aliases[MAXALIASES - 1])
95 			*q++ = cp;
96 		cp = any(cp, " \t");
97 		if (cp != NULL)
98 			*cp++ = '\0';
99 	}
100 	*q = NULL;
101 	return (&net);
102 }
103 
104 static char *
105 any(cp, match)
106 	register char *cp;
107 	char *match;
108 {
109 	register char *mp, c;
110 
111 	while (c = *cp) {
112 		for (mp = match; *mp; mp++)
113 			if (*mp == c)
114 				return (cp);
115 		cp++;
116 	}
117 	return ((char *)0);
118 }
119