diff options
Diffstat (limited to 'src/lib/libc/net/getservent.c')
| -rw-r--r-- | src/lib/libc/net/getservent.c | 147 |
1 files changed, 94 insertions, 53 deletions
diff --git a/src/lib/libc/net/getservent.c b/src/lib/libc/net/getservent.c index 316891450e..c81a4cf3e2 100644 --- a/src/lib/libc/net/getservent.c +++ b/src/lib/libc/net/getservent.c | |||
| @@ -1,5 +1,4 @@ | |||
| 1 | /* $NetBSD: getservent.c,v 1.4 1995/02/25 06:20:38 cgd Exp $ */ | 1 | /* $OpenBSD: getservent.c,v 1.12 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[] = "@(#)getservent.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: getservent.c,v 1.4 1995/02/25 06:20:38 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 <string.h> | 38 | #include <string.h> |
| 49 | #include <stdlib.h> | 39 | #include <stdlib.h> |
| 50 | 40 | ||
| 51 | #define MAXALIASES 35 | ||
| 52 | |||
| 53 | static FILE *servf = NULL; | ||
| 54 | static char line[BUFSIZ+1]; | ||
| 55 | static struct servent serv; | ||
| 56 | static char *serv_aliases[MAXALIASES]; | ||
| 57 | int _serv_stayopen; | ||
| 58 | |||
| 59 | void | 41 | void |
| 60 | setservent(f) | 42 | setservent_r(int f, struct servent_data *sd) |
| 61 | int f; | ||
| 62 | { | 43 | { |
| 63 | if (servf == NULL) | 44 | if (sd->fp == NULL) |
| 64 | servf = fopen(_PATH_SERVICES, "r" ); | 45 | sd->fp = fopen(_PATH_SERVICES, "r" ); |
| 65 | else | 46 | else |
| 66 | rewind(servf); | 47 | rewind(sd->fp); |
| 67 | _serv_stayopen |= f; | 48 | sd->stayopen |= f; |
| 68 | } | 49 | } |
| 69 | 50 | ||
| 70 | void | 51 | void |
| 71 | endservent() | 52 | endservent_r(struct servent_data *sd) |
| 72 | { | 53 | { |
| 73 | if (servf) { | 54 | if (sd->fp) { |
| 74 | fclose(servf); | 55 | fclose(sd->fp); |
| 75 | servf = NULL; | 56 | sd->fp = NULL; |
| 76 | } | 57 | } |
| 77 | _serv_stayopen = 0; | 58 | free(sd->aliases); |
| 59 | sd->aliases = NULL; | ||
| 60 | sd->maxaliases = 0; | ||
| 61 | free(sd->line); | ||
| 62 | sd->line = NULL; | ||
| 63 | sd->stayopen = 0; | ||
| 78 | } | 64 | } |
| 79 | 65 | ||
| 80 | struct servent * | 66 | int |
| 81 | getservent() | 67 | getservent_r(struct servent *se, struct servent_data *sd) |
| 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 (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) | 74 | if (sd->fp == NULL && (sd->fp = fopen(_PATH_SERVICES, "r" )) == NULL) |
| 87 | return (NULL); | 75 | return (-1); |
| 88 | again: | 76 | again: |
| 89 | if ((p = fgets(line, BUFSIZ, servf)) == NULL) | 77 | if ((p = fgetln(sd->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(sd->line, len + 1); | ||
| 94 | if (cp == NULL) | 86 | if (cp == NULL) |
| 95 | goto again; | 87 | return (-1); |
| 96 | *cp = '\0'; | 88 | sd->line = se->s_name = memcpy(cp, p, len); |
| 97 | serv.s_name = p; | 89 | cp[len] = '\0'; |
| 98 | p = strpbrk(p, " \t"); | 90 | p = strpbrk(cp, " \t"); |
| 99 | if (p == NULL) | 91 | if (p == NULL) |
| 100 | goto again; | 92 | goto again; |
| 101 | *p++ = '\0'; | 93 | *p++ = '\0'; |
| @@ -105,9 +97,22 @@ again: | |||
| 105 | if (cp == NULL) | 97 | if (cp == NULL) |
| 106 | goto again; | 98 | goto again; |
| 107 | *cp++ = '\0'; | 99 | *cp++ = '\0'; |
| 108 | serv.s_port = htons((u_short)atoi(p)); | 100 | l = strtol(p, &endp, 10); |
| 109 | serv.s_proto = cp; | 101 | if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX) |
| 110 | q = serv.s_aliases = serv_aliases; | 102 | goto again; |
| 103 | se->s_port = htons((in_port_t)l); | ||
| 104 | se->s_proto = cp; | ||
| 105 | if (sd->aliases == NULL) { | ||
| 106 | sd->maxaliases = 10; | ||
| 107 | sd->aliases = calloc(sd->maxaliases, sizeof(char *)); | ||
| 108 | if (sd->aliases == NULL) { | ||
| 109 | serrno = errno; | ||
| 110 | endservent_r(sd); | ||
| 111 | errno = serrno; | ||
| 112 | return (-1); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | q = se->s_aliases = sd->aliases; | ||
| 111 | cp = strpbrk(cp, " \t"); | 116 | cp = strpbrk(cp, " \t"); |
| 112 | if (cp != NULL) | 117 | if (cp != NULL) |
| 113 | *cp++ = '\0'; | 118 | *cp++ = '\0'; |
| @@ -116,12 +121,48 @@ again: | |||
| 116 | cp++; | 121 | cp++; |
| 117 | continue; | 122 | continue; |
| 118 | } | 123 | } |
| 119 | if (q < &serv_aliases[MAXALIASES - 1]) | 124 | if (q == &se->s_aliases[sd->maxaliases - 1]) { |
| 120 | *q++ = cp; | 125 | p = realloc(se->s_aliases, |
| 126 | 2 * sd->maxaliases * sizeof(char *)); | ||
| 127 | if (p == NULL) { | ||
| 128 | serrno = errno; | ||
| 129 | endservent_r(sd); | ||
| 130 | errno = serrno; | ||
| 131 | return (-1); | ||
| 132 | } | ||
| 133 | sd->maxaliases *= 2; | ||
| 134 | q = (char **)p + (q - se->s_aliases); | ||
| 135 | se->s_aliases = sd->aliases = (char **)p; | ||
| 136 | } | ||
| 137 | *q++ = cp; | ||
| 121 | cp = strpbrk(cp, " \t"); | 138 | cp = strpbrk(cp, " \t"); |
| 122 | if (cp != NULL) | 139 | if (cp != NULL) |
| 123 | *cp++ = '\0'; | 140 | *cp++ = '\0'; |
| 124 | } | 141 | } |
| 125 | *q = NULL; | 142 | *q = NULL; |
| 143 | return (0); | ||
| 144 | } | ||
| 145 | |||
| 146 | struct servent_data _servent_data; /* shared with getservby{name,port}.c */ | ||
| 147 | |||
| 148 | void | ||
| 149 | setservent(int f) | ||
| 150 | { | ||
| 151 | setservent_r(f, &_servent_data); | ||
| 152 | } | ||
| 153 | |||
| 154 | void | ||
| 155 | endservent(void) | ||
| 156 | { | ||
| 157 | endservent_r(&_servent_data); | ||
| 158 | } | ||
| 159 | |||
| 160 | struct servent * | ||
| 161 | getservent(void) | ||
| 162 | { | ||
| 163 | static struct servent serv; | ||
| 164 | |||
| 165 | if (getservent_r(&serv, &_servent_data) != 0) | ||
| 166 | return (NULL); | ||
| 126 | return (&serv); | 167 | return (&serv); |
| 127 | } | 168 | } |
