Commit | Line | Data |
---|---|---|
5e649950 | 1 | /* getnetent.c 4.2 82/10/05 */ |
b754e80b SL |
2 | |
3 | #include <stdio.h> | |
4 | #include <sys/socket.h> | |
5 | #include <netdb.h> | |
6 | #include <ctype.h> | |
7 | ||
8 | #define MAXALIASES 35 | |
9 | ||
10 | static char *NETDB = "/usr/lib/networks"; | |
11 | static FILE *netf = NULL; | |
12 | static char line[BUFSIZ+1]; | |
13 | static struct netent net; | |
14 | static char *net_aliases[MAXALIASES]; | |
15 | static int stayopen = 0; | |
b754e80b SL |
16 | static char *any(); |
17 | ||
18 | setnetent(f) | |
19 | int f; | |
20 | { | |
21 | if (netf == NULL) | |
22 | netf = fopen(NETDB, "r" ); | |
23 | else | |
24 | rewind(netf); | |
25 | stayopen |= f; | |
26 | } | |
27 | ||
28 | endnetent() | |
29 | { | |
30 | if (netf && !stayopen) { | |
31 | fclose(netf); | |
32 | netf = NULL; | |
33 | } | |
34 | } | |
35 | ||
36 | struct netent * | |
37 | getnetent() | |
38 | { | |
39 | char *p; | |
40 | register char *cp, **q; | |
41 | ||
42 | if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) | |
43 | return (NULL); | |
44 | again: | |
45 | p = fgets(line, BUFSIZ, netf); | |
46 | if (p == NULL) | |
47 | return (NULL); | |
48 | if (*p == '#') | |
49 | goto again; | |
50 | cp = any(p, "#\n"); | |
51 | if (cp == NULL) | |
52 | goto again; | |
53 | *cp = '\0'; | |
54 | net.n_name = p; | |
55 | cp = any(p, " \t"); | |
56 | if (cp == NULL) | |
57 | goto again; | |
58 | *cp++ = '\0'; | |
59 | while (*cp == ' ' || *cp == '\t') | |
60 | cp++; | |
61 | p = any(cp, " \t"); | |
62 | if (p != NULL) | |
63 | *p++ = '\0'; | |
5e649950 | 64 | net.n_net = inet_addr(cp); |
b754e80b SL |
65 | net.n_aliases = net_aliases; |
66 | q = net_aliases, cp = p; | |
67 | while (*cp) { | |
68 | if (*cp == ' ' || *cp == '\t') { | |
69 | cp++; | |
70 | continue; | |
71 | } | |
72 | if (q < &net_aliases[MAXALIASES - 1]) | |
73 | *q++ = cp; | |
74 | cp = any(cp, " \t"); | |
75 | if (*cp != NULL) | |
76 | *cp++ = '\0'; | |
77 | } | |
78 | *q = NULL; | |
79 | return (&net); | |
80 | } | |
81 | ||
b754e80b SL |
82 | static char * |
83 | any(cp, match) | |
84 | register char *cp; | |
85 | char *match; | |
86 | { | |
87 | register char *mp, c; | |
88 | ||
89 | while (c = *cp) { | |
90 | for (mp = match; *mp; mp++) | |
91 | if (*mp == c) | |
92 | return (cp); | |
93 | cp++; | |
94 | } | |
95 | return ((char *)0); | |
96 | } |