grrr...fix reverse chronological order
[dragonfly.git] / lib / libcr / rpc / getrpcent.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user or with the express written consent of
8  * Sun Microsystems, Inc.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  *
30  * @(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro
31  * $FreeBSD: src/lib/libc/rpc/getrpcent.c,v 1.10 1999/08/28 00:00:39 peter Exp $
32  * $DragonFly: src/lib/libcr/rpc/Attic/getrpcent.c,v 1.3 2004/10/25 19:38:25 drhodus Exp $
33  */
34
35 /*
36  * Copyright (c) 1984 by Sun Microsystems, Inc.
37  */
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <string.h>
43 #include <rpc/rpc.h>
44 #ifdef YP
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
47 #endif
48
49 /*
50  * Internet version.
51  */
52 struct rpcdata {
53         FILE    *rpcf;
54         int     stayopen;
55 #define MAXALIASES      35
56         char    *rpc_aliases[MAXALIASES];
57         struct  rpcent rpc;
58         char    line[BUFSIZ+1];
59 #ifdef  YP
60         char    *domain;
61         char    *current;
62         int     currentlen;
63 #endif
64 } *rpcdata;
65
66 #ifdef  YP
67 static int      __yp_nomap = 0;
68 extern int _yp_check(char **);
69 #endif  /* YP */
70
71 static  struct rpcent *interpret();
72 struct  hostent *gethostent();
73 char    *inet_ntoa();
74
75 static char RPCDB[] = "/etc/rpc";
76
77 static struct rpcdata *
78 _rpcdata()
79 {
80         struct rpcdata *d = rpcdata;
81
82         if (d == 0) {
83                 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
84                 rpcdata = d;
85         }
86         return (d);
87 }
88
89 struct rpcent *
90 getrpcbynumber(number)
91         int number;
92 {
93         struct rpcdata *d = _rpcdata();
94         struct rpcent *p;
95 #ifdef  YP
96         int reason;
97         char adrstr[16];
98 #endif
99
100         if (d == 0)
101                 return (0);
102 #ifdef  YP
103         if (!__yp_nomap && _yp_check(&d->domain)) {
104                 sprintf(adrstr, "%d", number);
105                 reason = yp_match(d->domain, "rpc.bynumber", adrstr, strlen(adrstr),
106                                   &d->current, &d->currentlen);
107                 switch(reason) {
108                 case 0:
109                         break;
110                 case YPERR_MAP:
111                         __yp_nomap = 1;
112                         goto no_yp;
113                         break;
114                 default:
115                         return(0);
116                         break;
117                 }
118                 d->current[d->currentlen] = '\0';
119                 p = interpret(d->current, d->currentlen);
120                 (void) free(d->current);
121                 return p;
122         }
123 no_yp:
124 #endif  /* YP */
125         setrpcent(0);
126         while ((p = getrpcent())) {
127                 if (p->r_number == number)
128                         break;
129         }
130         endrpcent();
131         return (p);
132 }
133
134 struct rpcent *
135 getrpcbyname(name)
136         char *name;
137 {
138         struct rpcent *rpc = NULL;
139         char **rp;
140
141         setrpcent(0);
142         while ((rpc = getrpcent())) {
143                 if (strcmp(rpc->r_name, name) == 0)
144                         goto done;
145                 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
146                         if (strcmp(*rp, name) == 0)
147                                 goto done;
148                 }
149         }
150 done:
151         endrpcent();
152         return (rpc);
153 }
154
155 void
156 setrpcent(f)
157         int f;
158 {
159         struct rpcdata *d = _rpcdata();
160
161         if (d == 0)
162                 return;
163 #ifdef  YP
164         if (!__yp_nomap && _yp_check(NULL)) {
165                 if (d->current)
166                         free(d->current);
167                 d->current = NULL;
168                 d->currentlen = 0;
169                 return;
170         }
171         __yp_nomap = 0;
172 #endif  /* YP */
173         if (d->rpcf == NULL)
174                 d->rpcf = fopen(RPCDB, "r");
175         else
176                 rewind(d->rpcf);
177         d->stayopen |= f;
178 }
179
180 void
181 endrpcent()
182 {
183         struct rpcdata *d = _rpcdata();
184
185         if (d == 0)
186                 return;
187 #ifdef  YP
188         if (!__yp_nomap && _yp_check(NULL)) {
189                 if (d->current && !d->stayopen)
190                         free(d->current);
191                 d->current = NULL;
192                 d->currentlen = 0;
193                 return;
194         }
195         __yp_nomap = 0;
196 #endif  /* YP */
197         if (d->rpcf && !d->stayopen) {
198                 fclose(d->rpcf);
199                 d->rpcf = NULL;
200         }
201 }
202
203 struct rpcent *
204 getrpcent()
205 {
206         struct rpcdata *d = _rpcdata();
207 #ifdef  YP
208         struct rpcent *hp;
209         int reason;
210         char *val = NULL;
211         int vallen;
212 #endif
213
214         if (d == 0)
215                 return(NULL);
216 #ifdef  YP
217         if (!__yp_nomap && _yp_check(&d->domain)) {
218                 if (d->current == NULL && d->currentlen == 0) {
219                         reason = yp_first(d->domain, "rpc.bynumber",
220                                           &d->current, &d->currentlen,
221                                           &val, &vallen);
222                 } else {
223                         reason = yp_next(d->domain, "rpc.bynumber",
224                                          d->current, d->currentlen,
225                                          &d->current, &d->currentlen,
226                                          &val, &vallen);
227                 }
228                 switch(reason) {
229                 case 0:
230                         break;
231                 case YPERR_MAP:
232                         __yp_nomap = 1;
233                         goto no_yp;
234                         break;
235                 default:
236                         return(0);
237                         break;
238                 }
239                 val[vallen] = '\0';
240                 hp = interpret(val, vallen);
241                 (void) free(val);
242                 return hp;
243         }
244 no_yp:
245 #endif  /* YP */
246         if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
247                 return (NULL);
248         /* -1 so there is room to append a \n below */
249         if (fgets(d->line, BUFSIZ - 1, d->rpcf) == NULL)
250                 return (NULL);
251         return (interpret(d->line, strlen(d->line)));
252 }
253
254 static struct rpcent *
255 interpret(val, len)
256         char *val;
257         int len;
258 {
259         struct rpcdata *d = _rpcdata();
260         char *p;
261         char *cp, **q;
262
263         if (d == 0)
264                 return (0);
265         (void) strncpy(d->line, val, BUFSIZ);
266         d->line[BUFSIZ] = '\0';
267         p = d->line;
268         p[len] = '\n';
269         if (*p == '#')
270                 return (getrpcent());
271         cp = strpbrk(p, "#\n");
272         if (cp == NULL)
273                 return (getrpcent());
274         *cp = '\0';
275         cp = strpbrk(p, " \t");
276         if (cp == NULL)
277                 return (getrpcent());
278         *cp++ = '\0';
279         /* THIS STUFF IS INTERNET SPECIFIC */
280         d->rpc.r_name = d->line;
281         while (*cp == ' ' || *cp == '\t')
282                 cp++;
283         d->rpc.r_number = atoi(cp);
284         q = d->rpc.r_aliases = d->rpc_aliases;
285         cp = strpbrk(cp, " \t");
286         if (cp != NULL)
287                 *cp++ = '\0';
288         while (cp && *cp) {
289                 if (*cp == ' ' || *cp == '\t') {
290                         cp++;
291                         continue;
292                 }
293                 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
294                         *q++ = cp;
295                 cp = strpbrk(cp, " \t");
296                 if (cp != NULL)
297                         *cp++ = '\0';
298         }
299         *q = NULL;
300         return (&d->rpc);
301 }
302