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.6 2006/08/01 01:14:16 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 #include <port_before.h>
23 #if !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
)
115 #ifdef SERV_R_ENT_UNUSED
118 setservent(stay_open
);
119 #ifdef SERV_R_SET_RESULT
120 return (SERV_R_SET_RESULT
);
125 #ifdef SERV_R_ENT_ARGS
126 endservent_r(SERV_R_ENT_ARGS
)
131 #ifdef SERV_R_ENT_UNUSED
135 SERV_R_END_RESULT(SERV_R_OK
);
142 copy_servent(struct servent
*se
, struct servent
*sptr
, SERV_R_COPY_ARGS
) {
147 /* Find out the amount of space required to store the answer. */
148 numptr
= 1; /*%< NULL ptr */
149 len
= (char *)ALIGN(buf
) - buf
;
150 for (i
= 0; se
->s_aliases
[i
]; i
++, numptr
++) {
151 len
+= strlen(se
->s_aliases
[i
]) + 1;
153 len
+= strlen(se
->s_name
) + 1;
154 len
+= strlen(se
->s_proto
) + 1;
155 len
+= numptr
* sizeof(char*);
157 if (len
> (int)buflen
) {
162 /* copy port value */
163 sptr
->s_port
= se
->s_port
;
165 cp
= (char *)ALIGN(buf
) + numptr
* sizeof(char *);
167 /* copy official name */
168 n
= strlen(se
->s_name
) + 1;
169 strcpy(cp
, se
->s_name
);
174 sptr
->s_aliases
= (char **)ALIGN(buf
);
175 for (i
= 0 ; se
->s_aliases
[i
]; i
++) {
176 n
= strlen(se
->s_aliases
[i
]) + 1;
177 strcpy(cp
, se
->s_aliases
[i
]);
178 sptr
->s_aliases
[i
] = cp
;
181 sptr
->s_aliases
[i
] = NULL
;
184 n
= strlen(se
->s_proto
) + 1;
185 strcpy(cp
, se
->s_proto
);
191 #else /* !SERVENT_DATA */
193 copy_servent(struct servent
*se
, struct servent
*sptr
, SERV_R_COPY_ARGS
) {
197 /* copy port value */
198 sptr
->s_port
= se
->s_port
;
200 /* copy official name */
202 eob
= sdptr
->line
+ sizeof(sdptr
->line
);
203 if ((n
= strlen(se
->s_name
) + 1) < (eob
- cp
)) {
204 strcpy(cp
, se
->s_name
);
213 sptr
->s_aliases
= sdptr
->serv_aliases
;
214 while (se
->s_aliases
[i
] && i
< (_MAXALIASES
-1)) {
215 if ((n
= strlen(se
->s_aliases
[i
]) + 1) < (eob
- cp
)) {
216 strcpy(cp
, se
->s_aliases
[i
]);
217 sptr
->s_aliases
[i
] = cp
;
224 sptr
->s_aliases
[i
] = NULL
;
227 if ((n
= strlen(se
->s_proto
) + 1) < (eob
- cp
)) {
228 strcpy(cp
, se
->s_proto
);
237 #endif /* !SERVENT_DATA */
238 #else /*SERV_R_RETURN */
239 static int getservent_r_unknown_system
= 0;
240 #endif /*SERV_R_RETURN */
241 #endif /* !defined(DO_PTHREADS) */