2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1998-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: getservent_r.c,v 1.3.206.1 2004/03/09 08:33:36 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 #include <port_before.h>
23 #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
24 static int getservent_r_not_required = 0;
29 #include <sys/types.h>
30 #include <netinet/in.h>
32 #include <sys/param.h>
33 #include <port_after.h>
38 copy_servent(struct servent *, struct servent *, SERV_R_COPY_ARGS);
41 getservbyname_r(const char *name, const char *proto,
42 struct servent *sptr, SERV_R_ARGS) {
43 struct servent *se = getservbyname(name, proto);
44 #ifdef SERV_R_SETANSWER
47 if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0)
57 return (copy_servent(se, sptr, SERV_R_COPY));
62 getservbyport_r(int port, const char *proto,
63 struct servent *sptr, SERV_R_ARGS) {
64 struct servent *se = getservbyport(port, proto);
65 #ifdef SERV_R_SETANSWER
68 if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0)
78 return (copy_servent(se, sptr, SERV_R_COPY));
83 * These assume a single context is in operation per thread.
84 * If this is not the case we will need to call irs directly
85 * rather than through the base functions.
89 getservent_r(struct servent *sptr, SERV_R_ARGS) {
90 struct servent *se = getservent();
91 #ifdef SERV_R_SETANSWER
94 if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0)
104 return (copy_servent(se, sptr, SERV_R_COPY));
109 #ifdef SERV_R_ENT_ARGS
110 setservent_r(int stay_open, SERV_R_ENT_ARGS)
112 setservent_r(int stay_open)
116 setservent(stay_open);
117 #ifdef SERV_R_SET_RESULT
118 return (SERV_R_SET_RESULT);
123 #ifdef SERV_R_ENT_ARGS
124 endservent_r(SERV_R_ENT_ARGS)
131 SERV_R_END_RESULT(SERV_R_OK);
138 copy_servent(struct servent *se, struct servent *sptr, SERV_R_COPY_ARGS) {
143 /* Find out the amount of space required to store the answer. */
144 numptr = 1; /* NULL ptr */
145 len = (char *)ALIGN(buf) - buf;
146 for (i = 0; se->s_aliases[i]; i++, numptr++) {
147 len += strlen(se->s_aliases[i]) + 1;
149 len += strlen(se->s_name) + 1;
150 len += strlen(se->s_proto) + 1;
151 len += numptr * sizeof(char*);
153 if (len > (int)buflen) {
158 /* copy port value */
159 sptr->s_port = se->s_port;
161 cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
163 /* copy official name */
164 n = strlen(se->s_name) + 1;
165 strcpy(cp, se->s_name);
170 sptr->s_aliases = (char **)ALIGN(buf);
171 for (i = 0 ; se->s_aliases[i]; i++) {
172 n = strlen(se->s_aliases[i]) + 1;
173 strcpy(cp, se->s_aliases[i]);
174 sptr->s_aliases[i] = cp;
177 sptr->s_aliases[i] = NULL;
180 n = strlen(se->s_proto) + 1;
181 strcpy(cp, se->s_proto);
187 #else /* !SERVENT_DATA */
189 copy_servent(struct servent *se, struct servent *sptr, SERV_R_COPY_ARGS) {
193 /* copy port value */
194 sptr->s_port = se->s_port;
196 /* copy official name */
198 eob = ndptr->line + sizeof(ndptr->line);
199 if ((n = strlen(se->s_name) + 1) < (eob - cp)) {
200 strcpy(cp, se->s_name);
209 sptr->s_aliases = ndptr->serv_aliases;
210 while (se->s_aliases[i] && i < (_MAXALIASES-1)) {
211 if ((n = strlen(se->s_aliases[i]) + 1) < (eob - cp)) {
212 strcpy(cp, se->s_aliases[i]);
213 sptr->s_aliases[i] = cp;
220 sptr->s_aliases[i] = NULL;
223 if ((n = strlen(se->s_proto) + 1) < (eob - cp)) {
224 strcpy(cp, se->s_proto);
233 #endif /* !SERVENT_DATA */
234 #else /*SERV_R_RETURN */
235 static int getservent_r_unknown_system = 0;
236 #endif /*SERV_R_RETURN */
237 #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */