Merge from vendor branch TCSH:
[dragonfly.git] / contrib / bind-9.3 / lib / bind / irs / getnetgrent_r.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1998-1999 by Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: getnetgrent_r.c,v 1.5.2.1.4.4 2005/09/03 12:47:38 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
21
22 #include <port_before.h>
23 #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
24         static int getnetgrent_r_not_required = 0;
25 #else
26 #include <errno.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <stdlib.h>
33 #include <port_after.h>
34
35 #ifdef NGR_R_RETURN
36
37 static NGR_R_RETURN 
38 copy_protoent(char **, char **, char **, const char *, const char *,
39               const char *, NGR_R_COPY_ARGS);
40
41 NGR_R_RETURN
42 innetgr_r(const char *netgroup, const char *host, const char *user,
43           const char *domain) {
44         char *ng, *ho, *us, *dom;
45
46         DE_CONST(netgroup, ng);
47         DE_CONST(host, ho);
48         DE_CONST(user, us);
49         DE_CONST(domain, dom);
50
51         return (innetgr(ng, ho, us, dom));
52 }
53
54 /*
55  *      These assume a single context is in operation per thread.
56  *      If this is not the case we will need to call irs directly
57  *      rather than through the base functions.
58  */
59
60 NGR_R_RETURN
61 getnetgrent_r(char **machinep, char **userp, char **domainp, NGR_R_ARGS) {
62         char *mp, *up, *dp;
63         int res = getnetgrent(&mp, &up, &dp);
64
65         if (res != 1) 
66                 return (res);
67
68         return (copy_protoent(machinep, userp, domainp,
69                                 mp, up, dp, NGR_R_COPY));
70 }
71
72 NGR_R_SET_RETURN
73 #ifdef NGR_R_ENT_ARGS
74 setnetgrent_r(const char *netgroup, NGR_R_ENT_ARGS)
75 #else
76 setnetgrent_r(const char *netgroup)
77 #endif
78 {
79         char *tmp;
80 #if defined(NGR_R_ENT_ARGS) && !defined(NGR_R_PRIVATE)
81         UNUSED(buf);
82         UNUSED(buflen);
83 #endif
84
85         DE_CONST(netgroup, tmp);
86         setnetgrent(tmp);
87
88 #ifdef NGR_R_PRIVATE
89         *buf = NULL;
90 #endif
91 #ifdef NGR_R_SET_RESULT
92         return (NGR_R_SET_RESULT);
93 #endif
94 }
95
96 NGR_R_END_RETURN
97 #ifdef NGR_R_ENT_ARGS
98 endnetgrent_r(NGR_R_ENT_ARGS)
99 #else
100 endnetgrent_r(void)
101 #endif
102 {
103 #if defined(NGR_R_ENT_ARGS) && !defined(NGR_R_PRIVATE)
104         UNUSED(buf);
105         UNUSED(buflen);
106 #endif
107
108         endnetgrent();
109 #ifdef NGR_R_PRIVATE
110         if (*buf != NULL)
111                 free(*buf);
112         *buf = NULL;
113 #endif
114         NGR_R_END_RESULT(NGR_R_OK);
115 }
116
117 /* Private */
118
119 static int
120 copy_protoent(char **machinep, char **userp, char **domainp,
121               const char *mp, const char *up, const char *dp,
122               NGR_R_COPY_ARGS) {
123         char *cp;
124         int n;
125         int len;
126
127         /* Find out the amount of space required to store the answer. */
128         len = 0;
129         if (mp != NULL) len += strlen(mp) + 1;
130         if (up != NULL) len += strlen(up) + 1;
131         if (dp != NULL) len += strlen(dp) + 1;
132         
133 #ifdef NGR_R_PRIVATE
134         free(*buf);
135         *buf = malloc(len);
136         if (*buf == NULL)
137                 return(NGR_R_BAD);
138         cp = *buf;
139 #else
140         if (len > (int)buflen) {
141                 errno = ERANGE;
142                 return (NGR_R_BAD);
143         }
144         cp = buf;
145 #endif
146
147
148         if (mp != NULL) {
149                 n = strlen(mp) + 1;
150                 strcpy(cp, mp);
151                 *machinep = cp;
152                 cp += n;
153         } else
154                 *machinep = NULL;
155
156         if (up != NULL) {
157                 n = strlen(up) + 1;
158                 strcpy(cp, up);
159                 *userp = cp;
160                 cp += n;
161         } else
162                 *userp = NULL;
163
164         if (dp != NULL) {
165                 n = strlen(dp) + 1;
166                 strcpy(cp, dp);
167                 *domainp = cp;
168                 cp += n;
169         } else
170                 *domainp = NULL;
171
172         return (NGR_R_OK);
173 }
174 #else /* NGR_R_RETURN */
175         static int getnetgrent_r_unknown_system = 0;
176 #endif /* NGR_R_RETURN */
177 #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */