Merge from vendor branch GCC:
[dragonfly.git] / contrib / bind-9.3 / lib / bind / irs / getservent_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: getservent_r.c,v 1.3.206.1 2004/03/09 08:33:36 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 getservent_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 <sys/param.h>
33 #include <port_after.h>
34
35 #ifdef SERV_R_RETURN
36
37 static SERV_R_RETURN 
38 copy_servent(struct servent *, struct servent *, SERV_R_COPY_ARGS);
39
40 SERV_R_RETURN
41 getservbyname_r(const char *name, const char *proto,
42                 struct servent *sptr, SERV_R_ARGS) {
43         struct servent *se = getservbyname(name, proto);
44 #ifdef SERV_R_SETANSWER
45         int n = 0;
46         
47         if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0)
48                 *answerp = NULL;
49         else
50                 *answerp = sptr;
51
52         return (n);
53 #else
54         if (se == NULL)
55                 return (SERV_R_BAD);
56
57         return (copy_servent(se, sptr, SERV_R_COPY));
58 #endif
59 }
60
61 SERV_R_RETURN
62 getservbyport_r(int port, const char *proto,
63                 struct servent *sptr, SERV_R_ARGS) {
64         struct servent *se = getservbyport(port, proto);
65 #ifdef SERV_R_SETANSWER
66         int n = 0;
67         
68         if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0)
69                 *answerp = NULL;
70         else
71                 *answerp = sptr;
72
73         return (n);
74 #else
75         if (se == NULL)
76                 return (SERV_R_BAD);
77
78         return (copy_servent(se, sptr, SERV_R_COPY));
79 #endif
80 }
81
82 /*
83  *      These assume a single context is in operation per thread.
84  *      If this is not the case we will need to call irs directly
85  *      rather than through the base functions.
86  */
87
88 SERV_R_RETURN
89 getservent_r(struct servent *sptr, SERV_R_ARGS) {
90         struct servent *se = getservent();
91 #ifdef SERV_R_SETANSWER
92         int n = 0;
93         
94         if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0)
95                 *answerp = NULL;
96         else
97                 *answerp = sptr;
98
99         return (n);
100 #else
101         if (se == NULL)
102                 return (SERV_R_BAD);
103
104         return (copy_servent(se, sptr, SERV_R_COPY));
105 #endif
106 }
107
108 SERV_R_SET_RETURN
109 #ifdef SERV_R_ENT_ARGS
110 setservent_r(int stay_open, SERV_R_ENT_ARGS)
111 #else
112 setservent_r(int stay_open)
113 #endif
114 {
115
116         setservent(stay_open);
117 #ifdef SERV_R_SET_RESULT
118         return (SERV_R_SET_RESULT);
119 #endif
120 }
121
122 SERV_R_END_RETURN
123 #ifdef SERV_R_ENT_ARGS
124 endservent_r(SERV_R_ENT_ARGS)
125 #else
126 endservent_r()
127 #endif
128 {
129
130         endservent();
131         SERV_R_END_RESULT(SERV_R_OK);
132 }
133
134 /* Private */
135
136 #ifndef SERVENT_DATA
137 static SERV_R_RETURN
138 copy_servent(struct servent *se, struct servent *sptr, SERV_R_COPY_ARGS) {
139         char *cp;
140         int i, n;
141         int numptr, len;
142
143         /* Find out the amount of space required to store the answer. */
144         numptr = 1; /* NULL ptr */
145         len = (char *)ALIGN(buf) - buf;
146         for (i = 0; se->s_aliases[i]; i++, numptr++) {
147                 len += strlen(se->s_aliases[i]) + 1;
148         }
149         len += strlen(se->s_name) + 1;
150         len += strlen(se->s_proto) + 1;
151         len += numptr * sizeof(char*);
152         
153         if (len > (int)buflen) {
154                 errno = ERANGE;
155                 return (SERV_R_BAD);
156         }
157
158         /* copy port value */
159         sptr->s_port = se->s_port;
160
161         cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
162
163         /* copy official name */
164         n = strlen(se->s_name) + 1;
165         strcpy(cp, se->s_name);
166         sptr->s_name = cp;
167         cp += n;
168
169         /* copy aliases */
170         sptr->s_aliases = (char **)ALIGN(buf);
171         for (i = 0 ; se->s_aliases[i]; i++) {
172                 n = strlen(se->s_aliases[i]) + 1;
173                 strcpy(cp, se->s_aliases[i]);
174                 sptr->s_aliases[i] = cp;
175                 cp += n;
176         }
177         sptr->s_aliases[i] = NULL;
178
179         /* copy proto */
180         n = strlen(se->s_proto) + 1;
181         strcpy(cp, se->s_proto);
182         sptr->s_proto = cp;
183         cp += n;
184
185         return (SERV_R_OK);
186 }
187 #else /* !SERVENT_DATA */
188 static int
189 copy_servent(struct servent *se, struct servent *sptr, SERV_R_COPY_ARGS) {
190         char *cp, *eob;
191         int i, n;
192
193         /* copy port value */
194         sptr->s_port = se->s_port;
195
196         /* copy official name */
197         cp = ndptr->line;
198         eob = ndptr->line + sizeof(ndptr->line);
199         if ((n = strlen(se->s_name) + 1) < (eob - cp)) {
200                 strcpy(cp, se->s_name);
201                 sptr->s_name = cp;
202                 cp += n;
203         } else {
204                 return (-1);
205         }
206
207         /* copy aliases */
208         i = 0;
209         sptr->s_aliases = ndptr->serv_aliases;
210         while (se->s_aliases[i] && i < (_MAXALIASES-1)) {
211                 if ((n = strlen(se->s_aliases[i]) + 1) < (eob - cp)) {
212                         strcpy(cp, se->s_aliases[i]);
213                         sptr->s_aliases[i] = cp;
214                         cp += n;
215                 } else {
216                         break;
217                 }
218                 i++;
219         }
220         sptr->s_aliases[i] = NULL;
221
222         /* copy proto */
223         if ((n = strlen(se->s_proto) + 1) < (eob - cp)) {
224                 strcpy(cp, se->s_proto);
225                 sptr->s_proto = cp;
226                 cp += n;
227         } else {
228                 return (-1);
229         }
230
231         return (SERV_R_OK);
232 }
233 #endif /* !SERVENT_DATA */
234 #else /*SERV_R_RETURN */
235         static int getservent_r_unknown_system = 0;
236 #endif /*SERV_R_RETURN */
237 #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */