diff options
Diffstat (limited to 'src/lib/libc/net/getservent.c')
-rw-r--r-- | src/lib/libc/net/getservent.c | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/src/lib/libc/net/getservent.c b/src/lib/libc/net/getservent.c new file mode 100644 index 0000000000..3eaf2a4adc --- /dev/null +++ b/src/lib/libc/net/getservent.c | |||
@@ -0,0 +1,170 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: getservent.c,v 1.9 2004/10/25 03:09:01 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <sys/socket.h> | ||
36 | |||
37 | #include <errno.h> | ||
38 | #include <netdb.h> | ||
39 | #include <stdio.h> | ||
40 | #include <string.h> | ||
41 | #include <stdlib.h> | ||
42 | |||
43 | void | ||
44 | setservent_r(int f, struct servent_data *sd) | ||
45 | { | ||
46 | if (sd->fp == NULL) | ||
47 | sd->fp = fopen(_PATH_SERVICES, "r" ); | ||
48 | else | ||
49 | rewind(sd->fp); | ||
50 | sd->stayopen |= f; | ||
51 | } | ||
52 | |||
53 | void | ||
54 | endservent_r(struct servent_data *sd) | ||
55 | { | ||
56 | if (sd->fp) { | ||
57 | fclose(sd->fp); | ||
58 | sd->fp = NULL; | ||
59 | } | ||
60 | free(sd->aliases); | ||
61 | sd->aliases = NULL; | ||
62 | sd->maxaliases = 0; | ||
63 | free(sd->line); | ||
64 | sd->line = NULL; | ||
65 | sd->stayopen = 0; | ||
66 | } | ||
67 | |||
68 | int | ||
69 | getservent_r(struct servent *se, struct servent_data *sd) | ||
70 | { | ||
71 | char *p, *cp, **q, *endp; | ||
72 | size_t len; | ||
73 | long l; | ||
74 | int serrno; | ||
75 | |||
76 | if (sd->fp == NULL && (sd->fp = fopen(_PATH_SERVICES, "r" )) == NULL) | ||
77 | return (-1); | ||
78 | again: | ||
79 | if ((p = fgetln(sd->fp, &len)) == NULL) | ||
80 | return (-1); | ||
81 | if (len == 0 || *p == '#' || *p == '\n') | ||
82 | goto again; | ||
83 | if (p[len-1] == '\n') | ||
84 | len--; | ||
85 | if ((cp = memchr(p, '#', len)) != NULL) | ||
86 | len = cp - p; | ||
87 | cp = realloc(sd->line, len + 1); | ||
88 | if (cp == NULL) | ||
89 | return (-1); | ||
90 | sd->line = se->s_name = memcpy(cp, p, len); | ||
91 | cp[len] = '\0'; | ||
92 | p = strpbrk(cp, " \t"); | ||
93 | if (p == NULL) | ||
94 | goto again; | ||
95 | *p++ = '\0'; | ||
96 | while (*p == ' ' || *p == '\t') | ||
97 | p++; | ||
98 | cp = strpbrk(p, ",/"); | ||
99 | if (cp == NULL) | ||
100 | goto again; | ||
101 | *cp++ = '\0'; | ||
102 | l = strtol(p, &endp, 10); | ||
103 | if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX) | ||
104 | goto again; | ||
105 | se->s_port = htons((in_port_t)l); | ||
106 | se->s_proto = cp; | ||
107 | if (sd->aliases == NULL) { | ||
108 | sd->maxaliases = 10; | ||
109 | sd->aliases = malloc(sd->maxaliases * sizeof(char *)); | ||
110 | if (sd->aliases == NULL) { | ||
111 | serrno = errno; | ||
112 | endservent_r(sd); | ||
113 | errno = serrno; | ||
114 | return (-1); | ||
115 | } | ||
116 | } | ||
117 | q = se->s_aliases = sd->aliases; | ||
118 | cp = strpbrk(cp, " \t"); | ||
119 | if (cp != NULL) | ||
120 | *cp++ = '\0'; | ||
121 | while (cp && *cp) { | ||
122 | if (*cp == ' ' || *cp == '\t') { | ||
123 | cp++; | ||
124 | continue; | ||
125 | } | ||
126 | if (q == &se->s_aliases[sd->maxaliases - 1]) { | ||
127 | p = realloc(se->s_aliases, | ||
128 | 2 * sd->maxaliases * sizeof(char *)); | ||
129 | if (p == NULL) { | ||
130 | serrno = errno; | ||
131 | endservent_r(sd); | ||
132 | errno = serrno; | ||
133 | return (-1); | ||
134 | } | ||
135 | sd->maxaliases *= 2; | ||
136 | q = (char **)p + (q - se->s_aliases); | ||
137 | se->s_aliases = sd->aliases = (char **)p; | ||
138 | } | ||
139 | *q++ = cp; | ||
140 | cp = strpbrk(cp, " \t"); | ||
141 | if (cp != NULL) | ||
142 | *cp++ = '\0'; | ||
143 | } | ||
144 | *q = NULL; | ||
145 | return (0); | ||
146 | } | ||
147 | |||
148 | struct servent_data _servent_data; /* shared with getservby{name,port}.c */ | ||
149 | |||
150 | void | ||
151 | setservent(int f) | ||
152 | { | ||
153 | setservent_r(f, &_servent_data); | ||
154 | } | ||
155 | |||
156 | void | ||
157 | endservent(void) | ||
158 | { | ||
159 | endservent_r(&_servent_data); | ||
160 | } | ||
161 | |||
162 | struct servent * | ||
163 | getservent(void) | ||
164 | { | ||
165 | static struct servent serv; | ||
166 | |||
167 | if (getservent_r(&serv, &_servent_data) != 0) | ||
168 | return (NULL); | ||
169 | return (&serv); | ||
170 | } | ||