diff options
Diffstat (limited to 'src/lib/libc/net/getprotoent.c')
-rw-r--r-- | src/lib/libc/net/getprotoent.c | 145 |
1 files changed, 93 insertions, 52 deletions
diff --git a/src/lib/libc/net/getprotoent.c b/src/lib/libc/net/getprotoent.c index 1179b9029b..f0705e0765 100644 --- a/src/lib/libc/net/getprotoent.c +++ b/src/lib/libc/net/getprotoent.c | |||
@@ -1,5 +1,4 @@ | |||
1 | /* $NetBSD: getprotoent.c,v 1.4 1995/02/25 06:20:35 cgd Exp $ */ | 1 | /* $OpenBSD: getprotoent.c,v 1.10 2007/09/02 15:19:17 deraadt Exp $ */ |
2 | |||
3 | /* | 2 | /* |
4 | * Copyright (c) 1983, 1993 | 3 | * Copyright (c) 1983, 1993 |
5 | * The Regents of the University of California. All rights reserved. | 4 | * The Regents of the University of California. All rights reserved. |
@@ -12,11 +11,7 @@ | |||
12 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
15 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | 15 | * may be used to endorse or promote products derived from this software |
21 | * without specific prior written permission. | 16 | * without specific prior written permission. |
22 | * | 17 | * |
@@ -33,69 +28,66 @@ | |||
33 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
34 | */ | 29 | */ |
35 | 30 | ||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: getprotoent.c,v 1.4 1995/02/25 06:20:35 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/types.h> | 31 | #include <sys/types.h> |
45 | #include <sys/socket.h> | 32 | #include <sys/socket.h> |
33 | |||
34 | #include <errno.h> | ||
35 | #include <limits.h> | ||
46 | #include <netdb.h> | 36 | #include <netdb.h> |
47 | #include <stdio.h> | 37 | #include <stdio.h> |
48 | #include <stdlib.h> | 38 | #include <stdlib.h> |
49 | #include <string.h> | 39 | #include <string.h> |
50 | 40 | ||
51 | #define MAXALIASES 35 | ||
52 | |||
53 | static FILE *protof = NULL; | ||
54 | static char line[BUFSIZ+1]; | ||
55 | static struct protoent proto; | ||
56 | static char *proto_aliases[MAXALIASES]; | ||
57 | int _proto_stayopen; | ||
58 | |||
59 | void | 41 | void |
60 | setprotoent(f) | 42 | setprotoent_r(int f, struct protoent_data *pd) |
61 | int f; | ||
62 | { | 43 | { |
63 | if (protof == NULL) | 44 | if (pd->fp == NULL) |
64 | protof = fopen(_PATH_PROTOCOLS, "r" ); | 45 | pd->fp = fopen(_PATH_PROTOCOLS, "r" ); |
65 | else | 46 | else |
66 | rewind(protof); | 47 | rewind(pd->fp); |
67 | _proto_stayopen |= f; | 48 | pd->stayopen |= f; |
68 | } | 49 | } |
69 | 50 | ||
70 | void | 51 | void |
71 | endprotoent() | 52 | endprotoent_r(struct protoent_data *pd) |
72 | { | 53 | { |
73 | if (protof) { | 54 | if (pd->fp) { |
74 | fclose(protof); | 55 | fclose(pd->fp); |
75 | protof = NULL; | 56 | pd->fp = NULL; |
76 | } | 57 | } |
77 | _proto_stayopen = 0; | 58 | free(pd->aliases); |
59 | pd->aliases = NULL; | ||
60 | pd->maxaliases = 0; | ||
61 | free(pd->line); | ||
62 | pd->line = NULL; | ||
63 | pd->stayopen = 0; | ||
78 | } | 64 | } |
79 | 65 | ||
80 | struct protoent * | 66 | int |
81 | getprotoent() | 67 | getprotoent_r(struct protoent *pe, struct protoent_data *pd) |
82 | { | 68 | { |
83 | char *p; | 69 | char *p, *cp, **q, *endp; |
84 | register char *cp, **q; | 70 | size_t len; |
71 | long l; | ||
72 | int serrno; | ||
85 | 73 | ||
86 | if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) | 74 | if (pd->fp == NULL && (pd->fp = fopen(_PATH_PROTOCOLS, "r" )) == NULL) |
87 | return (NULL); | 75 | return (-1); |
88 | again: | 76 | again: |
89 | if ((p = fgets(line, BUFSIZ, protof)) == NULL) | 77 | if ((p = fgetln(pd->fp, &len)) == NULL) |
90 | return (NULL); | 78 | return (-1); |
91 | if (*p == '#') | 79 | if (len == 0 || *p == '#' || *p == '\n') |
92 | goto again; | 80 | goto again; |
93 | cp = strpbrk(p, "#\n"); | 81 | if (p[len-1] == '\n') |
82 | len--; | ||
83 | if ((cp = memchr(p, '#', len)) != NULL) | ||
84 | len = cp - p; | ||
85 | cp = realloc(pd->line, len + 1); | ||
94 | if (cp == NULL) | 86 | if (cp == NULL) |
95 | goto again; | 87 | return (-1); |
96 | *cp = '\0'; | 88 | pd->line = pe->p_name = memcpy(cp, p, len); |
97 | proto.p_name = p; | 89 | cp[len] = '\0'; |
98 | cp = strpbrk(p, " \t"); | 90 | cp = strpbrk(cp, " \t"); |
99 | if (cp == NULL) | 91 | if (cp == NULL) |
100 | goto again; | 92 | goto again; |
101 | *cp++ = '\0'; | 93 | *cp++ = '\0'; |
@@ -104,8 +96,21 @@ again: | |||
104 | p = strpbrk(cp, " \t"); | 96 | p = strpbrk(cp, " \t"); |
105 | if (p != NULL) | 97 | if (p != NULL) |
106 | *p++ = '\0'; | 98 | *p++ = '\0'; |
107 | proto.p_proto = atoi(cp); | 99 | l = strtol(cp, &endp, 10); |
108 | q = proto.p_aliases = proto_aliases; | 100 | if (endp == cp || *endp != '\0' || l < 0 || l >= INT_MAX) |
101 | goto again; | ||
102 | pe->p_proto = l; | ||
103 | if (pd->aliases == NULL) { | ||
104 | pd->maxaliases = 5; | ||
105 | pd->aliases = calloc(pd->maxaliases, sizeof(char *)); | ||
106 | if (pd->aliases == NULL) { | ||
107 | serrno = errno; | ||
108 | endprotoent_r(pd); | ||
109 | errno = serrno; | ||
110 | return (-1); | ||
111 | } | ||
112 | } | ||
113 | q = pe->p_aliases = pd->aliases; | ||
109 | if (p != NULL) { | 114 | if (p != NULL) { |
110 | cp = p; | 115 | cp = p; |
111 | while (cp && *cp) { | 116 | while (cp && *cp) { |
@@ -113,13 +118,49 @@ again: | |||
113 | cp++; | 118 | cp++; |
114 | continue; | 119 | continue; |
115 | } | 120 | } |
116 | if (q < &proto_aliases[MAXALIASES - 1]) | 121 | if (q == &pe->p_aliases[pd->maxaliases - 1]) { |
117 | *q++ = cp; | 122 | p = realloc(pe->p_aliases, |
123 | 2 * pd->maxaliases * sizeof(char *)); | ||
124 | if (p == NULL) { | ||
125 | serrno = errno; | ||
126 | endprotoent_r(pd); | ||
127 | errno = serrno; | ||
128 | return (-1); | ||
129 | } | ||
130 | pd->maxaliases *= 2; | ||
131 | q = (char **)p + (q - pe->p_aliases); | ||
132 | pe->p_aliases = pd->aliases = (char **)p; | ||
133 | } | ||
134 | *q++ = cp; | ||
118 | cp = strpbrk(cp, " \t"); | 135 | cp = strpbrk(cp, " \t"); |
119 | if (cp != NULL) | 136 | if (cp != NULL) |
120 | *cp++ = '\0'; | 137 | *cp++ = '\0'; |
121 | } | 138 | } |
122 | } | 139 | } |
123 | *q = NULL; | 140 | *q = NULL; |
141 | return (0); | ||
142 | } | ||
143 | |||
144 | struct protoent_data _protoent_data; /* shared with getproto{,name}.c */ | ||
145 | |||
146 | void | ||
147 | setprotoent(int f) | ||
148 | { | ||
149 | setprotoent_r(f, &_protoent_data); | ||
150 | } | ||
151 | |||
152 | void | ||
153 | endprotoent(void) | ||
154 | { | ||
155 | endprotoent_r(&_protoent_data); | ||
156 | } | ||
157 | |||
158 | struct protoent * | ||
159 | getprotoent(void) | ||
160 | { | ||
161 | static struct protoent proto; | ||
162 | |||
163 | if (getprotoent_r(&proto, &_protoent_data) != 0) | ||
164 | return (NULL); | ||
124 | return (&proto); | 165 | return (&proto); |
125 | } | 166 | } |