source: rtems-libbsd/services/librpc/src/rpc/getrpcent.c @ 20ec9e6

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 20ec9e6 was 20ec9e6, checked in by Joel Sherrill <joel.sherrill@…>, on 08/03/12 at 19:19:49

librpc: Initial addition

This does not currently compile. There is an include file
issue and int32_t is not defined even though stdint.h is included
before its use.

  • 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#ifdef HAVE_CONFIG_H
41#include "config.h"
42#endif
43
44#include <stdio.h>
45#include <stdlib.h>
46#include <sys/types.h>
47#include <string.h>
48#include <rpc/rpc.h>
49#ifdef YP
50#include <rpcsvc/yp_prot.h>
51#include <rpcsvc/ypclnt.h>
52#endif
53
54/*
55 * Internet version.
56 */
57struct rpcdata {
58        FILE    *rpcf;
59        int     stayopen;
60#define MAXALIASES      35
61        char    *rpc_aliases[MAXALIASES];
62        struct  rpcent rpc;
63        char    line[BUFSIZ+1];
64#ifdef  YP
65        char    *domain;
66        char    *current;
67        int     currentlen;
68#endif
69} *rpcdata;
70
71#ifdef  YP
72static int      __yp_nomap = 0;
73extern int _yp_check(char **);
74#endif  /* YP */
75
76static  struct rpcent *interpret(char *val, int len);
77
78static char RPCDB[] = "/etc/rpc";
79
80static struct rpcdata *
81_rpcdata(void)
82{
83        register struct rpcdata *d = rpcdata;
84
85        if (d == 0) {
86                d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
87                rpcdata = d;
88        }
89        return (d);
90}
91
92struct rpcent *
93getrpcbynumber(int number)
94{
95        register struct rpcdata *d = _rpcdata();
96        register struct rpcent *p;
97#ifdef  YP
98        int reason;
99        char adrstr[16];
100#endif
101
102        if (d == 0)
103                return (0);
104#ifdef  YP
105        if (!__yp_nomap && _yp_check(&d->domain)) {
106                sprintf(adrstr, "%d", number);
107                reason = yp_match(d->domain, "rpc.bynumber", adrstr, strlen(adrstr),
108                                  &d->current, &d->currentlen);
109                switch(reason) {
110                case 0:
111                        break;
112                case YPERR_MAP:
113                        __yp_nomap = 1;
114                        goto no_yp;
115                        break;
116                default:
117                        return(0);
118                        break;
119                }
120                d->current[d->currentlen] = '\0';
121                p = interpret(d->current, d->currentlen);
122                (void) free(d->current);
123                return p;
124        }
125no_yp:
126#endif  /* YP */
127        setrpcent(0);
128        while ((p = getrpcent())) {
129                if (p->r_number == number)
130                        break;
131        }
132        endrpcent();
133        return (p);
134}
135
136struct rpcent *
137getrpcbyname(char *name)
138{
139        struct rpcent *rpc = NULL;
140        char **rp;
141
142        setrpcent(0);
143        while ((rpc = getrpcent())) {
144                if (strcmp(rpc->r_name, name) == 0)
145                        goto done;
146                for (rp = rpc->r_aliases; *rp != NULL; rp++) {
147                        if (strcmp(*rp, name) == 0)
148                                goto done;
149                }
150        }
151done:
152        endrpcent();
153        return (rpc);
154}
155
156void
157setrpcent(int f)
158{
159        register 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
180void
181endrpcent(void)
182{
183        register 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
203struct rpcent *
204getrpcent(void)
205{
206        register 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        }
244no_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
254static struct rpcent *
255interpret(
256        char *val,
257        int len)
258{
259        register struct rpcdata *d = _rpcdata();
260        char *p;
261        register 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}
Note: See TracBrowser for help on using the repository browser.