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