4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1998-1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid
[] = "Id: getprotoent_r.c,v 1.6 2006/08/01 01:14:16 marka Exp";
22 #endif /* LIBC_SCCS and not lint */
24 #include <port_before.h>
25 #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
26 static int getprotoent_r_not_required
= 0;
31 #include <sys/types.h>
32 #include <netinet/in.h>
34 #include <port_after.h>
39 copy_protoent(struct protoent
*, struct protoent
*, PROTO_R_COPY_ARGS
);
42 getprotobyname_r(const char *name
, struct protoent
*pptr
, PROTO_R_ARGS
) {
43 struct protoent
*pe
= getprotobyname(name
);
44 #ifdef PROTO_R_SETANSWER
47 if (pe
== NULL
|| (n
= copy_protoent(pe
, pptr
, PROTO_R_COPY
)) != 0)
57 return (copy_protoent(pe
, pptr
, PROTO_R_COPY
));
62 getprotobynumber_r(int proto
, struct protoent
*pptr
, PROTO_R_ARGS
) {
63 struct protoent
*pe
= getprotobynumber(proto
);
64 #ifdef PROTO_R_SETANSWER
67 if (pe
== NULL
|| (n
= copy_protoent(pe
, pptr
, PROTO_R_COPY
)) != 0)
77 return (copy_protoent(pe
, pptr
, PROTO_R_COPY
));
82 * These assume a single context is in operation per thread.
83 * If this is not the case we will need to call irs directly
84 * rather than through the base functions.
88 getprotoent_r(struct protoent
*pptr
, PROTO_R_ARGS
) {
89 struct protoent
*pe
= getprotoent();
90 #ifdef PROTO_R_SETANSWER
93 if (pe
== NULL
|| (n
= copy_protoent(pe
, pptr
, PROTO_R_COPY
)) != 0)
101 return (PROTO_R_BAD
);
103 return (copy_protoent(pe
, pptr
, PROTO_R_COPY
));
108 #ifdef PROTO_R_ENT_ARGS
109 setprotoent_r(int stay_open
, PROTO_R_ENT_ARGS
)
111 setprotoent_r(int stay_open
)
114 #ifdef PROTO_R_ENT_UNUSED
117 setprotoent(stay_open
);
118 #ifdef PROTO_R_SET_RESULT
119 return (PROTO_R_SET_RESULT
);
124 #ifdef PROTO_R_ENT_ARGS
125 endprotoent_r(PROTO_R_ENT_ARGS
)
130 #ifdef PROTO_R_ENT_UNUSED
134 PROTO_R_END_RESULT(PROTO_R_OK
);
139 #ifndef PROTOENT_DATA
140 static PROTO_R_RETURN
141 copy_protoent(struct protoent
*pe
, struct protoent
*pptr
, PROTO_R_COPY_ARGS
) {
146 /* Find out the amount of space required to store the answer. */
147 numptr
= 1; /*%< NULL ptr */
148 len
= (char *)ALIGN(buf
) - buf
;
149 for (i
= 0; pe
->p_aliases
[i
]; i
++, numptr
++) {
150 len
+= strlen(pe
->p_aliases
[i
]) + 1;
152 len
+= strlen(pe
->p_name
) + 1;
153 len
+= numptr
* sizeof(char*);
155 if (len
> (int)buflen
) {
157 return (PROTO_R_BAD
);
160 /* copy protocol value*/
161 pptr
->p_proto
= pe
->p_proto
;
163 cp
= (char *)ALIGN(buf
) + numptr
* sizeof(char *);
165 /* copy official name */
166 n
= strlen(pe
->p_name
) + 1;
167 strcpy(cp
, pe
->p_name
);
172 pptr
->p_aliases
= (char **)ALIGN(buf
);
173 for (i
= 0 ; pe
->p_aliases
[i
]; i
++) {
174 n
= strlen(pe
->p_aliases
[i
]) + 1;
175 strcpy(cp
, pe
->p_aliases
[i
]);
176 pptr
->p_aliases
[i
] = cp
;
179 pptr
->p_aliases
[i
] = NULL
;
183 #else /* !PROTOENT_DATA */
185 copy_protoent(struct protoent
*pe
, struct protoent
*pptr
, PROTO_R_COPY_ARGS
) {
189 /* copy protocol value */
190 pptr
->p_proto
= pe
->p_proto
;
192 /* copy official name */
194 eob
= pdptr
->line
+ sizeof(pdptr
->line
);
195 if ((n
= strlen(pe
->p_name
) + 1) < (eob
- cp
)) {
196 strcpy(cp
, pe
->p_name
);
205 pptr
->p_aliases
= pdptr
->proto_aliases
;
206 while (pe
->p_aliases
[i
] && i
< (_MAXALIASES
-1)) {
207 if ((n
= strlen(pe
->p_aliases
[i
]) + 1) < (eob
- cp
)) {
208 strcpy(cp
, pe
->p_aliases
[i
]);
209 pptr
->p_aliases
[i
] = cp
;
216 pptr
->p_aliases
[i
] = NULL
;
220 #endif /* PROTOENT_DATA */
221 #else /* PROTO_R_RETURN */
222 static int getprotoent_r_unknown_system
= 0;
223 #endif /* PROTO_R_RETURN */
224 #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */