summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/getservent.c
diff options
context:
space:
mode:
authormillert <>2004-10-17 20:24:23 +0000
committermillert <>2004-10-17 20:24:23 +0000
commiteae90a29f226809527585d7ba688d0af8627db58 (patch)
treebeb43deef803247f4e915dfbd6ef663407e7dd2b /src/lib/libc/net/getservent.c
parent91294475ad30114d23728b4a7599c81a0e1a99a3 (diff)
downloadopenbsd-eae90a29f226809527585d7ba688d0af8627db58.tar.gz
openbsd-eae90a29f226809527585d7ba688d0af8627db58.tar.bz2
openbsd-eae90a29f226809527585d7ba688d0af8627db58.zip
Reentrant versions of getprotoent(3) and getservent(3). Adapted from
changes in NetBSD by Christos. OK otto@
Diffstat (limited to 'src/lib/libc/net/getservent.c')
-rw-r--r--src/lib/libc/net/getservent.c122
1 files changed, 83 insertions, 39 deletions
diff --git a/src/lib/libc/net/getservent.c b/src/lib/libc/net/getservent.c
index 475e25a1f8..9cf53774d6 100644
--- a/src/lib/libc/net/getservent.c
+++ b/src/lib/libc/net/getservent.c
@@ -28,68 +28,68 @@
28 */ 28 */
29 29
30#if defined(LIBC_SCCS) && !defined(lint) 30#if defined(LIBC_SCCS) && !defined(lint)
31static char rcsid[] = "$OpenBSD: getservent.c,v 1.7 2004/09/16 03:13:22 deraadt Exp $"; 31static char rcsid[] = "$OpenBSD: getservent.c,v 1.8 2004/10/17 20:24:23 millert Exp $";
32#endif /* LIBC_SCCS and not lint */ 32#endif /* LIBC_SCCS and not lint */
33 33
34#include <sys/types.h> 34#include <sys/types.h>
35#include <sys/socket.h> 35#include <sys/socket.h>
36
37#include <errno.h>
36#include <netdb.h> 38#include <netdb.h>
37#include <stdio.h> 39#include <stdio.h>
38#include <string.h> 40#include <string.h>
39#include <stdlib.h> 41#include <stdlib.h>
40 42
41#define MAXALIASES 35
42
43static FILE *servf = NULL;
44static char line[BUFSIZ+1];
45static struct servent serv;
46static char *serv_aliases[MAXALIASES];
47int _serv_stayopen;
48
49void 43void
50setservent(int f) 44setservent_r(int f, struct servent_data *sd)
51{ 45{
52 if (servf == NULL) 46 if (sd->fp == NULL)
53 servf = fopen(_PATH_SERVICES, "r" ); 47 sd->fp = fopen(_PATH_SERVICES, "r" );
54 else 48 else
55 rewind(servf); 49 rewind(sd->fp);
56 _serv_stayopen |= f; 50 sd->stayopen |= f;
57} 51}
58 52
59void 53void
60endservent(void) 54endservent_r(struct servent_data *sd)
61{ 55{
62 if (servf) { 56 if (sd->fp) {
63 fclose(servf); 57 fclose(sd->fp);
64 servf = NULL; 58 sd->fp = NULL;
65 } 59 }
66 _serv_stayopen = 0; 60 free(sd->aliases);
61 sd->aliases = NULL;
62 sd->maxaliases = 0;
63 free(sd->line);
64 sd->line = NULL;
65 sd->stayopen = 0;
67} 66}
68 67
69struct servent * 68struct servent *
70getservent(void) 69getservent_r(struct servent *se, struct servent_data *sd)
71{ 70{
72 char *p, *cp, **q, *endp; 71 char *p, *cp, **q, *endp;
73 long l;
74 size_t len; 72 size_t len;
73 long l;
74 int serrno;
75 75
76 if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) 76 if (sd->fp == NULL && (sd->fp = fopen(_PATH_SERVICES, "r" )) == NULL)
77 return (NULL); 77 return (NULL);
78again: 78again:
79 if ((p = fgetln(servf, &len)) == NULL) 79 if ((p = fgetln(sd->fp, &len)) == NULL)
80 return (NULL); 80 return (NULL);
81 if (len == 0 || *p == '#' || *p == '\n')
82 goto again;
81 if (p[len-1] == '\n') 83 if (p[len-1] == '\n')
82 len--; 84 len--;
83 if (len >= sizeof(line) || len == 0) 85 if ((cp = memchr(p, '#', len)) != NULL)
84 goto again; 86 len = cp - p;
85 p = memcpy(line, p, len); 87 cp = realloc(sd->line, len + 1);
86 line[len] = '\0'; 88 if (cp == NULL)
87 if (*p == '#') 89 return (NULL);
88 goto again; 90 sd->line = se->s_name = memcpy(cp, p, len);
89 if ((cp = strchr(p, '#')) != NULL) 91 cp[len] = '\0';
90 *cp = '\0'; 92 p = strpbrk(cp, " \t");
91 serv.s_name = p;
92 p = strpbrk(p, " \t");
93 if (p == NULL) 93 if (p == NULL)
94 goto again; 94 goto again;
95 *p++ = '\0'; 95 *p++ = '\0';
@@ -102,9 +102,19 @@ again:
102 l = strtol(p, &endp, 10); 102 l = strtol(p, &endp, 10);
103 if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX) 103 if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX)
104 goto again; 104 goto again;
105 serv.s_port = htons((in_port_t)l); 105 se->s_port = htons((in_port_t)l);
106 serv.s_proto = cp; 106 se->s_proto = cp;
107 q = serv.s_aliases = serv_aliases; 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 (NULL);
115 }
116 }
117 q = se->s_aliases = sd->aliases;
108 cp = strpbrk(cp, " \t"); 118 cp = strpbrk(cp, " \t");
109 if (cp != NULL) 119 if (cp != NULL)
110 *cp++ = '\0'; 120 *cp++ = '\0';
@@ -113,12 +123,46 @@ again:
113 cp++; 123 cp++;
114 continue; 124 continue;
115 } 125 }
116 if (q < &serv_aliases[MAXALIASES - 1]) 126 if (q == &se->s_aliases[sd->maxaliases - 1]) {
117 *q++ = cp; 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 (NULL);
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;
118 cp = strpbrk(cp, " \t"); 140 cp = strpbrk(cp, " \t");
119 if (cp != NULL) 141 if (cp != NULL)
120 *cp++ = '\0'; 142 *cp++ = '\0';
121 } 143 }
122 *q = NULL; 144 *q = NULL;
123 return (&serv); 145 return (se);
146}
147
148struct servent_data _servent_data; /* shared with getservby{name,port}.c */
149
150void
151setservent(int f)
152{
153 setservent_r(f, &_servent_data);
154}
155
156void
157endservent(void)
158{
159 endservent_r(&_servent_data);
160}
161
162struct servent *
163getservent(void)
164{
165 static struct servent serv;
166
167 return getservent_r(&serv, &_servent_data);
124} 168}