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.2.1 2004/03/09 09:17:30 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 setnetent(stay_open);
122 #ifdef NET_R_SET_RESULT
123 return (NET_R_SET_RESULT);
128 #ifdef NET_R_ENT_ARGS
129 endnetent_r(NET_R_ENT_ARGS)
135 NET_R_END_RESULT(NET_R_OK);
142 copy_netent(struct netent *ne, struct netent *nptr, NET_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; ne->n_aliases[i]; i++, numptr++) {
151 len += strlen(ne->n_aliases[i]) + 1;
153 len += strlen(ne->n_name) + 1;
154 len += numptr * sizeof(char*);
156 if (len > (int)buflen) {
161 /* copy net value and type */
162 nptr->n_addrtype = ne->n_addrtype;
163 nptr->n_net = ne->n_net;
165 cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
167 /* copy official name */
168 n = strlen(ne->n_name) + 1;
169 strcpy(cp, ne->n_name);
174 nptr->n_aliases = (char **)ALIGN(buf);
175 for (i = 0 ; ne->n_aliases[i]; i++) {
176 n = strlen(ne->n_aliases[i]) + 1;
177 strcpy(cp, ne->n_aliases[i]);
178 nptr->n_aliases[i] = cp;
181 nptr->n_aliases[i] = NULL;
185 #else /* !NETENT_DATA */
187 copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) {
191 /* copy net value and type */
192 nptr->n_addrtype = ne->n_addrtype;
193 nptr->n_net = ne->n_net;
195 /* copy official name */
197 eob = ndptr->line + sizeof(ndptr->line);
198 if ((n = strlen(ne->n_name) + 1) < (eob - cp)) {
199 strcpy(cp, ne->n_name);
208 nptr->n_aliases = ndptr->net_aliases;
209 while (ne->n_aliases[i] && i < (_MAXALIASES-1)) {
210 if ((n = strlen(ne->n_aliases[i]) + 1) < (eob - cp)) {
211 strcpy(cp, ne->n_aliases[i]);
212 nptr->n_aliases[i] = cp;
219 nptr->n_aliases[i] = NULL;
223 #endif /* !NETENT_DATA */
224 #else /* NET_R_RETURN */
225 static int getnetent_r_unknown_system = 0;
226 #endif /* NET_R_RETURN */
227 #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */