Commit | Line | Data |
---|---|---|
8ea4199d DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
6b2f9dd0 KB |
3 | * All rights reserved. |
4 | * | |
269a7923 | 5 | * %sccs.include.redist.c% |
8ea4199d DF |
6 | */ |
7 | ||
2ce81398 | 8 | #if defined(LIBC_SCCS) && !defined(lint) |
24fac7d8 | 9 | static char sccsid[] = "@(#)getnetent.c 5.8 (Berkeley) %G%"; |
6b2f9dd0 | 10 | #endif /* LIBC_SCCS and not lint */ |
b754e80b | 11 | |
1a58f155 | 12 | #include <sys/types.h> |
b754e80b | 13 | #include <sys/socket.h> |
24fac7d8 KB |
14 | #include <netinet/in.h> |
15 | #include <arpa/inet.h> | |
b754e80b | 16 | #include <netdb.h> |
24fac7d8 KB |
17 | #include <stdio.h> |
18 | #include <string.h> | |
b754e80b SL |
19 | |
20 | #define MAXALIASES 35 | |
21 | ||
24fac7d8 | 22 | static FILE *netf; |
b754e80b SL |
23 | static char line[BUFSIZ+1]; |
24 | static struct netent net; | |
25 | static char *net_aliases[MAXALIASES]; | |
5442f2c7 | 26 | int _net_stayopen; |
b754e80b | 27 | |
24fac7d8 | 28 | void |
b754e80b SL |
29 | setnetent(f) |
30 | int f; | |
31 | { | |
32 | if (netf == NULL) | |
29eb0a7d | 33 | netf = fopen(_PATH_NETWORKS, "r" ); |
b754e80b SL |
34 | else |
35 | rewind(netf); | |
5442f2c7 | 36 | _net_stayopen |= f; |
b754e80b SL |
37 | } |
38 | ||
24fac7d8 | 39 | void |
b754e80b SL |
40 | endnetent() |
41 | { | |
5442f2c7 | 42 | if (netf) { |
b754e80b SL |
43 | fclose(netf); |
44 | netf = NULL; | |
45 | } | |
5442f2c7 | 46 | _net_stayopen = 0; |
b754e80b SL |
47 | } |
48 | ||
49 | struct netent * | |
50 | getnetent() | |
51 | { | |
52 | char *p; | |
53 | register char *cp, **q; | |
54 | ||
29eb0a7d | 55 | if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) |
b754e80b SL |
56 | return (NULL); |
57 | again: | |
58 | p = fgets(line, BUFSIZ, netf); | |
59 | if (p == NULL) | |
60 | return (NULL); | |
61 | if (*p == '#') | |
62 | goto again; | |
24fac7d8 | 63 | cp = strpbrk(p, "#\n"); |
b754e80b SL |
64 | if (cp == NULL) |
65 | goto again; | |
66 | *cp = '\0'; | |
67 | net.n_name = p; | |
24fac7d8 | 68 | cp = strpbrk(p, " \t"); |
b754e80b SL |
69 | if (cp == NULL) |
70 | goto again; | |
71 | *cp++ = '\0'; | |
72 | while (*cp == ' ' || *cp == '\t') | |
73 | cp++; | |
24fac7d8 | 74 | p = strpbrk(cp, " \t"); |
b754e80b SL |
75 | if (p != NULL) |
76 | *p++ = '\0'; | |
047cb5c4 | 77 | net.n_net = inet_network(cp); |
86755108 | 78 | net.n_addrtype = AF_INET; |
9243aabf | 79 | q = net.n_aliases = net_aliases; |
463dee20 | 80 | if (p != NULL) |
9243aabf | 81 | cp = p; |
463dee20 SL |
82 | while (cp && *cp) { |
83 | if (*cp == ' ' || *cp == '\t') { | |
84 | cp++; | |
85 | continue; | |
b754e80b | 86 | } |
463dee20 SL |
87 | if (q < &net_aliases[MAXALIASES - 1]) |
88 | *q++ = cp; | |
24fac7d8 | 89 | cp = strpbrk(cp, " \t"); |
463dee20 SL |
90 | if (cp != NULL) |
91 | *cp++ = '\0'; | |
b754e80b SL |
92 | } |
93 | *q = NULL; | |
94 | return (&net); | |
95 | } |