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: getprotoent_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(_REENTRANT) || !defined(DO_PTHREADS)
24 static int getprotoent_r_not_required
= 0;
29 #include <sys/types.h>
30 #include <netinet/in.h>
32 #include <port_after.h>
37 copy_protoent(struct protoent
*, struct protoent
*, PROTO_R_COPY_ARGS
);
40 getprotobyname_r(const char *name
, struct protoent
*pptr
, PROTO_R_ARGS
) {
41 struct protoent
*pe
= getprotobyname(name
);
42 #ifdef PROTO_R_SETANSWER
45 if (pe
== NULL
|| (n
= copy_protoent(pe
, pptr
, PROTO_R_COPY
)) != 0)
55 return (copy_protoent(pe
, pptr
, PROTO_R_COPY
));
60 getprotobynumber_r(int proto
, struct protoent
*pptr
, PROTO_R_ARGS
) {
61 struct protoent
*pe
= getprotobynumber(proto
);
62 #ifdef PROTO_R_SETANSWER
65 if (pe
== NULL
|| (n
= copy_protoent(pe
, pptr
, PROTO_R_COPY
)) != 0)
75 return (copy_protoent(pe
, pptr
, PROTO_R_COPY
));
80 * These assume a single context is in operation per thread.
81 * If this is not the case we will need to call irs directly
82 * rather than through the base functions.
86 getprotoent_r(struct protoent
*pptr
, PROTO_R_ARGS
) {
87 struct protoent
*pe
= getprotoent();
88 #ifdef PROTO_R_SETANSWER
91 if (pe
== NULL
|| (n
= copy_protoent(pe
, pptr
, PROTO_R_COPY
)) != 0)
101 return (copy_protoent(pe
, pptr
, PROTO_R_COPY
));
106 #ifdef PROTO_R_ENT_ARGS
107 setprotoent_r(int stay_open
, PROTO_R_ENT_ARGS
)
109 setprotoent_r(int stay_open
)
112 #ifdef PROTO_R_ENT_UNUSED
115 setprotoent(stay_open
);
116 #ifdef PROTO_R_SET_RESULT
117 return (PROTO_R_SET_RESULT
);
122 #ifdef PROTO_R_ENT_ARGS
123 endprotoent_r(PROTO_R_ENT_ARGS
)
128 #ifdef PROTO_R_ENT_UNUSED
132 PROTO_R_END_RESULT(PROTO_R_OK
);
137 #ifndef PROTOENT_DATA
138 static PROTO_R_RETURN
139 copy_protoent(struct protoent
*pe
, struct protoent
*pptr
, PROTO_R_COPY_ARGS
) {
144 /* Find out the amount of space required to store the answer. */
145 numptr
= 1; /*%< NULL ptr */
146 len
= (char *)ALIGN(buf
) - buf
;
147 for (i
= 0; pe
->p_aliases
[i
]; i
++, numptr
++) {
148 len
+= strlen(pe
->p_aliases
[i
]) + 1;
150 len
+= strlen(pe
->p_name
) + 1;
151 len
+= numptr
* sizeof(char*);
153 if (len
> (int)buflen
) {
155 return (PROTO_R_BAD
);
158 /* copy protocol value*/
159 pptr
->p_proto
= pe
->p_proto
;
161 cp
= (char *)ALIGN(buf
) + numptr
* sizeof(char *);
163 /* copy official name */
164 n
= strlen(pe
->p_name
) + 1;
165 strcpy(cp
, pe
->p_name
);
170 pptr
->p_aliases
= (char **)ALIGN(buf
);
171 for (i
= 0 ; pe
->p_aliases
[i
]; i
++) {
172 n
= strlen(pe
->p_aliases
[i
]) + 1;
173 strcpy(cp
, pe
->p_aliases
[i
]);
174 pptr
->p_aliases
[i
] = cp
;
177 pptr
->p_aliases
[i
] = NULL
;
181 #else /* !PROTOENT_DATA */
183 copy_protoent(struct protoent
*pe
, struct protoent
*pptr
, PROTO_R_COPY_ARGS
) {
187 /* copy protocol value */
188 pptr
->p_proto
= pe
->p_proto
;
190 /* copy official name */
192 eob
= pdptr
->line
+ sizeof(pdptr
->line
);
193 if ((n
= strlen(pe
->p_name
) + 1) < (eob
- cp
)) {
194 strcpy(cp
, pe
->p_name
);
203 pptr
->p_aliases
= pdptr
->proto_aliases
;
204 while (pe
->p_aliases
[i
] && i
< (_MAXALIASES
-1)) {
205 if ((n
= strlen(pe
->p_aliases
[i
]) + 1) < (eob
- cp
)) {
206 strcpy(cp
, pe
->p_aliases
[i
]);
207 pptr
->p_aliases
[i
] = cp
;
214 pptr
->p_aliases
[i
] = NULL
;
218 #endif /* PROTOENT_DATA */
219 #else /* PROTO_R_RETURN */
220 static int getprotoent_r_unknown_system
= 0;
221 #endif /* PROTO_R_RETURN */
222 #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */