diff options
Diffstat (limited to 'src/lib/libc/net/getnetent.c')
-rw-r--r-- | src/lib/libc/net/getnetent.c | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/lib/libc/net/getnetent.c b/src/lib/libc/net/getnetent.c new file mode 100644 index 0000000000..1bec6fb98c --- /dev/null +++ b/src/lib/libc/net/getnetent.c | |||
@@ -0,0 +1,121 @@ | |||
1 | /* $OpenBSD: getnetent.c,v 1.12 2006/01/17 15:37:58 millert Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/param.h> | ||
32 | #include <sys/socket.h> | ||
33 | #include <netinet/in.h> | ||
34 | #include <arpa/inet.h> | ||
35 | #include <netdb.h> | ||
36 | #include <stdio.h> | ||
37 | #include <string.h> | ||
38 | |||
39 | #define MAXALIASES 35 | ||
40 | |||
41 | static FILE *netf; | ||
42 | static char line[BUFSIZ+1]; | ||
43 | static struct netent net; | ||
44 | static char *net_aliases[MAXALIASES]; | ||
45 | int _net_stayopen; | ||
46 | |||
47 | void | ||
48 | setnetent(int f) | ||
49 | { | ||
50 | if (netf == NULL) | ||
51 | netf = fopen(_PATH_NETWORKS, "r" ); | ||
52 | else | ||
53 | rewind(netf); | ||
54 | _net_stayopen |= f; | ||
55 | } | ||
56 | |||
57 | void | ||
58 | endnetent(void) | ||
59 | { | ||
60 | if (netf) { | ||
61 | fclose(netf); | ||
62 | netf = NULL; | ||
63 | } | ||
64 | _net_stayopen = 0; | ||
65 | } | ||
66 | |||
67 | struct netent * | ||
68 | getnetent(void) | ||
69 | { | ||
70 | char *p, *cp, **q; | ||
71 | size_t len; | ||
72 | |||
73 | if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) | ||
74 | return (NULL); | ||
75 | again: | ||
76 | if ((p = fgetln(netf, &len)) == NULL) | ||
77 | return (NULL); | ||
78 | if (p[len-1] == '\n') | ||
79 | len--; | ||
80 | if (len >= sizeof(line) || len == 0) | ||
81 | goto again; | ||
82 | p = memcpy(line, p, len); | ||
83 | line[len] = '\0'; | ||
84 | if (*p == '#') | ||
85 | goto again; | ||
86 | if ((cp = strchr(p, '#')) != NULL) | ||
87 | *cp = '\0'; | ||
88 | net.n_name = p; | ||
89 | if (strlen(net.n_name) >= MAXHOSTNAMELEN-1) | ||
90 | net.n_name[MAXHOSTNAMELEN-1] = '\0'; | ||
91 | cp = strpbrk(p, " \t"); | ||
92 | if (cp == NULL) | ||
93 | goto again; | ||
94 | *cp++ = '\0'; | ||
95 | while (*cp == ' ' || *cp == '\t') | ||
96 | cp++; | ||
97 | p = strpbrk(cp, " \t"); | ||
98 | if (p != NULL) | ||
99 | *p++ = '\0'; | ||
100 | net.n_net = inet_network(cp); | ||
101 | net.n_addrtype = AF_INET; | ||
102 | q = net.n_aliases = net_aliases; | ||
103 | if (p != NULL) | ||
104 | cp = p; | ||
105 | while (cp && *cp) { | ||
106 | if (*cp == ' ' || *cp == '\t') { | ||
107 | cp++; | ||
108 | continue; | ||
109 | } | ||
110 | if (q < &net_aliases[MAXALIASES - 1]) { | ||
111 | *q++ = cp; | ||
112 | if (strlen(cp) >= MAXHOSTNAMELEN-1) | ||
113 | cp[MAXHOSTNAMELEN-1] = '\0'; | ||
114 | } | ||
115 | cp = strpbrk(cp, " \t"); | ||
116 | if (cp != NULL) | ||
117 | *cp++ = '\0'; | ||
118 | } | ||
119 | *q = NULL; | ||
120 | return (&net); | ||
121 | } | ||