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: getnetent_r.c,v 1.3.206.2 2005/09/03 12:47:38 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 #include <port_before.h>
23 #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
24 static int getnetent_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_netent(struct netent
*, struct netent
*, NET_R_COPY_ARGS
);
41 getnetbyname_r(const char *name
, struct netent
*nptr
, NET_R_ARGS
) {
42 struct netent
*ne
= getnetbyname(name
);
43 #ifdef NET_R_SETANSWER
46 if (ne
== NULL
|| (n
= copy_netent(ne
, nptr
, NET_R_COPY
)) != 0)
57 return (copy_netent(ne
, nptr
, NET_R_COPY
));
61 #ifndef GETNETBYADDR_ADDR_T
62 #define GETNETBYADDR_ADDR_T long
65 getnetbyaddr_r(GETNETBYADDR_ADDR_T addr
, int type
, struct netent
*nptr
, NET_R_ARGS
) {
66 struct netent
*ne
= getnetbyaddr(addr
, type
);
67 #ifdef NET_R_SETANSWER
70 if (ne
== NULL
|| (n
= copy_netent(ne
, nptr
, NET_R_COPY
)) != 0)
82 return (copy_netent(ne
, nptr
, NET_R_COPY
));
87 * These assume a single context is in operation per thread.
88 * If this is not the case we will need to call irs directly
89 * rather than through the base functions.
93 getnetent_r(struct netent
*nptr
, NET_R_ARGS
) {
94 struct netent
*ne
= getnetent();
95 #ifdef NET_R_SETANSWER
98 if (ne
== NULL
|| (n
= copy_netent(ne
, nptr
, NET_R_COPY
)) != 0)
110 return (copy_netent(ne
, nptr
, NET_R_COPY
));
115 #ifdef NET_R_ENT_ARGS
116 setnetent_r(int stay_open
, NET_R_ENT_ARGS
)
118 setnetent_r(int stay_open
)
121 #ifdef NET_R_ENT_ARGS
124 setnetent(stay_open
);
125 #ifdef NET_R_SET_RESULT
126 return (NET_R_SET_RESULT
);
131 #ifdef NET_R_ENT_ARGS
132 endnetent_r(NET_R_ENT_ARGS
)
137 #ifdef NET_R_ENT_ARGS
141 NET_R_END_RESULT(NET_R_OK
);
148 copy_netent(struct netent
*ne
, struct netent
*nptr
, NET_R_COPY_ARGS
) {
153 /* Find out the amount of space required to store the answer. */
154 numptr
= 1; /* NULL ptr */
155 len
= (char *)ALIGN(buf
) - buf
;
156 for (i
= 0; ne
->n_aliases
[i
]; i
++, numptr
++) {
157 len
+= strlen(ne
->n_aliases
[i
]) + 1;
159 len
+= strlen(ne
->n_name
) + 1;
160 len
+= numptr
* sizeof(char*);
162 if (len
> (int)buflen
) {
167 /* copy net value and type */
168 nptr
->n_addrtype
= ne
->n_addrtype
;
169 nptr
->n_net
= ne
->n_net
;
171 cp
= (char *)ALIGN(buf
) + numptr
* sizeof(char *);
173 /* copy official name */
174 n
= strlen(ne
->n_name
) + 1;
175 strcpy(cp
, ne
->n_name
);
180 nptr
->n_aliases
= (char **)ALIGN(buf
);
181 for (i
= 0 ; ne
->n_aliases
[i
]; i
++) {
182 n
= strlen(ne
->n_aliases
[i
]) + 1;
183 strcpy(cp
, ne
->n_aliases
[i
]);
184 nptr
->n_aliases
[i
] = cp
;
187 nptr
->n_aliases
[i
] = NULL
;
191 #else /* !NETENT_DATA */
193 copy_netent(struct netent
*ne
, struct netent
*nptr
, NET_R_COPY_ARGS
) {
197 /* copy net value and type */
198 nptr
->n_addrtype
= ne
->n_addrtype
;
199 nptr
->n_net
= ne
->n_net
;
201 /* copy official name */
203 eob
= ndptr
->line
+ sizeof(ndptr
->line
);
204 if ((n
= strlen(ne
->n_name
) + 1) < (eob
- cp
)) {
205 strcpy(cp
, ne
->n_name
);
214 nptr
->n_aliases
= ndptr
->net_aliases
;
215 while (ne
->n_aliases
[i
] && i
< (_MAXALIASES
-1)) {
216 if ((n
= strlen(ne
->n_aliases
[i
]) + 1) < (eob
- cp
)) {
217 strcpy(cp
, ne
->n_aliases
[i
]);
218 nptr
->n_aliases
[i
] = cp
;
225 nptr
->n_aliases
[i
] = NULL
;
229 #endif /* !NETENT_DATA */
230 #else /* NET_R_RETURN */
231 static int getnetent_r_unknown_system
= 0;
232 #endif /* NET_R_RETURN */
233 #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */