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