source: rtems/cpukit/librpc/src/rpc/getrpcent.c @ 142025c

4.104.114.95
Last change on this file since 142025c was 9c873b1d, checked in by Ralf Corsepius <ralf.corsepius@…>, on 08/01/08 at 16:04:25

Remove stray local decls. Misc. ansifications.

  • Property mode set to 100644
File size: 7.1 KB
Line 
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
31#if defined(LIBC_SCCS) && !defined(lint)
32/*static char *sccsid = "from: @(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";*/
33static char *rcsid = "$FreeBSD: src/lib/libc/rpc/getrpcent.c,v 1.10 1999/08/28 00:00:39 peter Exp $";
34#endif
35
36/*
37 * Copyright (c) 1984 by Sun Microsystems, Inc.
38 */
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <sys/types.h>
43#include <string.h>
44#include <rpc/rpc.h>
45#ifdef YP
46#include <rpcsvc/yp_prot.h>
47#include <rpcsvc/ypclnt.h>
48#endif
49
50/*
51 * Internet version.
52 */
53struct rpcdata {
54        FILE    *rpcf;
55        int     stayopen;
56#define MAXALIASES      35
57        char    *rpc_aliases[MAXALIASES];
58        struct  rpcent rpc;
59        char    line[BUFSIZ+1];
60#ifdef  YP
61        char    *domain;
62        char    *current;
63        int     currentlen;
64#endif
65} *rpcdata;
66
67#ifdef  YP
68static int      __yp_nomap = 0;
69extern int _yp_check(char **);
70#endif  /* YP */
71
72static  struct rpcent *interpret(char *val, int len);
73
74static char RPCDB[] = "/etc/rpc";
75
76static struct rpcdata *
77_rpcdata(void)
78{
79        register struct rpcdata *d = rpcdata;
80
81        if (d == 0) {
82                d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
83                rpcdata = d;
84        }
85        return (d);
86}
87
88struct rpcent *
89getrpcbynumber(number)
90        register int number;
91{
92        register struct rpcdata *d = _rpcdata();
93        register struct rpcent *p;
94#ifdef  YP
95        int reason;
96        char adrstr[16];
97#endif
98
99        if (d == 0)
100                return (0);
101#ifdef  YP
102        if (!__yp_nomap && _yp_check(&d->domain)) {
103                sprintf(adrstr, "%d", number);
104                reason = yp_match(d->domain, "rpc.bynumber", adrstr, strlen(adrstr),
105                                  &d->current, &d->currentlen);
106                switch(reason) {
107                case 0:
108                        break;
109                case YPERR_MAP:
110                        __yp_nomap = 1;
111                        goto no_yp;
112                        break;
113                default:
114                        return(0);
115                        break;
116                }
117                d->current[d->currentlen] = '\0';
118                p = interpret(d->current, d->currentlen);
119                (void) free(d->current);
120                return p;
121        }
122no_yp:
123#endif  /* YP */
124        setrpcent(0);
125        while ((p = getrpcent())) {
126                if (p->r_number == number)
127                        break;
128        }
129        endrpcent();
130        return (p);
131}
132
133struct rpcent *
134getrpcbyname(name)
135        char *name;
136{
137        struct rpcent *rpc = NULL;
138        char **rp;
139
140        setrpcent(0);
141        while ((rpc = getrpcent())) {
142                if (strcmp(rpc->r_name, name) == 0)
143                        goto done;
144                for (rp = rpc->r_aliases; *rp != NULL; rp++) {
145                        if (strcmp(*rp, name) == 0)
146                                goto done;
147                }
148        }
149done:
150        endrpcent();
151        return (rpc);
152}
153
154void
155setrpcent(f)
156        int f;
157{
158        register struct rpcdata *d = _rpcdata();
159
160        if (d == 0)
161                return;
162#ifdef  YP
163        if (!__yp_nomap && _yp_check(NULL)) {
164                if (d->current)
165                        free(d->current);
166                d->current = NULL;
167                d->currentlen = 0;
168                return;
169        }
170        __yp_nomap = 0;
171#endif  /* YP */
172        if (d->rpcf == NULL)
173                d->rpcf = fopen(RPCDB, "r");
174        else
175                rewind(d->rpcf);
176        d->stayopen |= f;
177}
178
179void
180endrpcent()
181{
182        register struct rpcdata *d = _rpcdata();
183
184        if (d == 0)
185                return;
186#ifdef  YP
187        if (!__yp_nomap && _yp_check(NULL)) {
188                if (d->current && !d->stayopen)
189                        free(d->current);
190                d->current = NULL;
191                d->currentlen = 0;
192                return;
193        }
194        __yp_nomap = 0;
195#endif  /* YP */
196        if (d->rpcf && !d->stayopen) {
197                fclose(d->rpcf);
198                d->rpcf = NULL;
199        }
200}
201
202struct rpcent *
203getrpcent()
204{
205        register struct rpcdata *d = _rpcdata();
206#ifdef  YP
207        struct rpcent *hp;
208        int reason;
209        char *val = NULL;
210        int vallen;
211#endif
212
213        if (d == 0)
214                return(NULL);
215#ifdef  YP
216        if (!__yp_nomap && _yp_check(&d->domain)) {
217                if (d->current == NULL && d->currentlen == 0) {
218                        reason = yp_first(d->domain, "rpc.bynumber",
219                                          &d->current, &d->currentlen,
220                                          &val, &vallen);
221                } else {
222                        reason = yp_next(d->domain, "rpc.bynumber",
223                                         d->current, d->currentlen,
224                                         &d->current, &d->currentlen,
225                                         &val, &vallen);
226                }
227                switch(reason) {
228                case 0:
229                        break;
230                case YPERR_MAP:
231                        __yp_nomap = 1;
232                        goto no_yp;
233                        break;
234                default:
235                        return(0);
236                        break;
237                }
238                val[vallen] = '\0';
239                hp = interpret(val, vallen);
240                (void) free(val);
241                return hp;
242        }
243no_yp:
244#endif  /* YP */
245        if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
246                return (NULL);
247        /* -1 so there is room to append a \n below */
248        if (fgets(d->line, BUFSIZ - 1, d->rpcf) == NULL)
249                return (NULL);
250        return (interpret(d->line, strlen(d->line)));
251}
252
253static struct rpcent *
254interpret(
255        char *val,
256        int len)
257{
258        register struct rpcdata *d = _rpcdata();
259        char *p;
260        register char *cp, **q;
261
262        if (d == 0)
263                return (0);
264        (void) strncpy(d->line, val, BUFSIZ);
265        d->line[BUFSIZ] = '\0';
266        p = d->line;
267        p[len] = '\n';
268        if (*p == '#')
269                return (getrpcent());
270        cp = strpbrk(p, "#\n");
271        if (cp == NULL)
272                return (getrpcent());
273        *cp = '\0';
274        cp = strpbrk(p, " \t");
275        if (cp == NULL)
276                return (getrpcent());
277        *cp++ = '\0';
278        /* THIS STUFF IS INTERNET SPECIFIC */
279        d->rpc.r_name = d->line;
280        while (*cp == ' ' || *cp == '\t')
281                cp++;
282        d->rpc.r_number = atoi(cp);
283        q = d->rpc.r_aliases = d->rpc_aliases;
284        cp = strpbrk(cp, " \t");
285        if (cp != NULL)
286                *cp++ = '\0';
287        while (cp && *cp) {
288                if (*cp == ' ' || *cp == '\t') {
289                        cp++;
290                        continue;
291                }
292                if (q < &(d->rpc_aliases[MAXALIASES - 1]))
293                        *q++ = cp;
294                cp = strpbrk(cp, " \t");
295                if (cp != NULL)
296                        *cp++ = '\0';
297        }
298        *q = NULL;
299        return (&d->rpc);
300}
Note: See TracBrowser for help on using the repository browser.