Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_play / rpc / getrpcent.c
1 #ifndef lint
2 static char sfs_getrpcent_id[] = "@(#)getrpcent.c     2.1     97/10/23";
3 #endif
4 /* @(#)getrpcent.c      2.2 88/07/29 4.0 RPCSRC */
5 #if !defined(lint) && defined(SCCSIDS)
6 static  char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11  Copyr 1984 Sun Micro";
7 #endif
8 /*
9  *  Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
10  *      All rights reserved.
11  *              Standard Performance Evaluation Corporation (SPEC)
12  *              6585 Merchant Place, Suite 100
13  *              Warrenton, VA 20187
14  *
15  *      This product contains benchmarks acquired from several sources who
16  *      understand and agree with SPEC's goal of creating fair and objective
17  *      benchmarks to measure computer performance.
18  *
19  *      This copyright notice is placed here only to protect SPEC in the
20  *      event the source is misused in any manner that is contrary to the
21  *      spirit, the goals and the intent of SPEC.
22  *
23  *      The source code is provided to the user or company under the license
24  *      agreement for the SPEC Benchmark Suite for this product.
25  */
26
27 /*
28  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
29  * unrestricted use provided that this legend is included on all tape
30  * media and as a part of the software program in whole or part.  Users
31  * may copy or modify Sun RPC without charge, but are not authorized
32  * to license or distribute it to anyone else except as part of a product or
33  * program developed by the user.
34  * 
35  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
36  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
37  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
38  * 
39  * Sun RPC is provided with no support and without any obligation on the
40  * part of Sun Microsystems, Inc. to assist in its use, correction,
41  * modification or enhancement.
42  * 
43  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
44  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
45  * OR ANY PART THEREOF.
46  * 
47  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
48  * or profits or other special, indirect and consequential damages, even if
49  * Sun has been advised of the possibility of such damages.
50  * 
51  * Sun Microsystems, Inc.
52  * 2550 Garcia Avenue
53  * Mountain View, California  94043
54  */
55
56 /*
57  * Copyright (c) 1985 by Sun Microsystems, Inc.
58  */
59
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <sys/types.h>
64 #include "rpc/rpc.h"
65 #include "rpc/netdb.h"
66 #include "rpc/osdep.h"
67
68 /*
69  * Internet version.
70  */
71 struct rpcdata {
72         FILE    *rpcf;
73         char    *current;
74         int     currentlen;
75         int     stayopen;
76 #define MAXALIASES      35
77         char    *rpc_aliases[MAXALIASES];
78         struct  rpcent rpc;
79         char    line[BUFSIZ+1];
80         char    *domain;
81 } *rpcdata;
82
83 extern void setrpcent(int);
84 extern void endrpcent(void);
85 static struct rpcent *interpret(char *, int);
86
87 static char RPCDB[] = "/etc/rpc";
88
89 static struct rpcdata *
90 _rpcdata(void)
91 {
92         register struct rpcdata *d = rpcdata;
93
94         if (d == 0) {
95                 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
96                 rpcdata = d;
97         }
98         return (d);
99 }
100
101 struct rpcent *
102 getrpcbynumber(
103         int number)
104 {
105         register struct rpcdata *d = _rpcdata();
106         register struct rpcent *p;
107
108         if (d == 0)
109                 return (0);
110         setrpcent(0);
111         while ((p = getrpcent()) != NULL) {
112                 if (p->r_number == number)
113                         break;
114         }
115         endrpcent();
116         return (p);
117 }
118
119 struct rpcent *
120 getrpcbyname(
121         char *name)
122 {
123         struct rpcent *rpc;
124         char **rp;
125
126         setrpcent(0);
127         while((rpc = getrpcent()) != NULL) {
128                 if (strcmp(rpc->r_name, name) == 0)
129                         return (rpc);
130                 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
131                         if (strcmp(*rp, name) == 0)
132                                 return (rpc);
133                 }
134         }
135         endrpcent();
136         return (NULL);
137 }
138
139 void
140 setrpcent(
141         int f)
142 {
143         register struct rpcdata *d = _rpcdata();
144
145         if (d == 0)
146                 return;
147         if (d->rpcf == NULL)
148                 d->rpcf = fopen(RPCDB, "r");
149         else
150                 rewind(d->rpcf);
151         if (d->current)
152                 free(d->current);
153         d->current = NULL;
154         d->stayopen |= f;
155 }
156
157 void
158 endrpcent(void)
159 {
160         register struct rpcdata *d = _rpcdata();
161
162         if (d == 0)
163                 return;
164         if (d->current && !d->stayopen) {
165                 free(d->current);
166                 d->current = NULL;
167         }
168         if (d->rpcf && !d->stayopen) {
169                 fclose(d->rpcf);
170                 d->rpcf = NULL;
171         }
172 }
173
174 struct rpcent *
175 getrpcent(void)
176 {
177         register struct rpcdata *d = _rpcdata();
178
179         if (d == 0)
180                 return(NULL);
181         if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
182                 return (NULL);
183         if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
184                 return (NULL);
185         return (interpret(d->line, strlen(d->line)));
186 }
187
188 static struct rpcent *
189 interpret(
190         char *val,
191         int len)
192 {
193         register struct rpcdata *d = _rpcdata();
194         char *p;
195         register char *cp, **q;
196
197         if (d == 0)
198                 return (NULL);
199         strncpy(d->line, val, len);
200         p = d->line;
201         d->line[len] = '\n';
202         if (*p == '#')
203                 return (getrpcent());
204         cp = strchr(p, '#');
205         if (cp == NULL) {
206                 cp = strchr(p, '\n');
207                 if (cp == NULL)
208                         return (getrpcent());
209         }
210         *cp = '\0';
211         cp = strchr(p, ' ');
212         if (cp == NULL) {
213                 cp = strchr(p, '\t');
214                 if (cp == NULL)
215                         return (getrpcent());
216         }
217         *cp++ = '\0';
218         /* THIS STUFF IS INTERNET SPECIFIC */
219         d->rpc.r_name = d->line;
220         while (*cp == ' ' || *cp == '\t')
221                 cp++;
222         d->rpc.r_number = atoi(cp);
223         q = d->rpc.r_aliases = d->rpc_aliases;
224         cp = strchr(p, ' ');
225         if (cp != NULL)
226                 *cp++ = '\0';
227         else {
228                 cp = strchr(p, '\t');
229                 if (cp != NULL)
230                         *cp++ = '\0';
231         }
232         while (cp && *cp) {
233                 if (*cp == ' ' || *cp == '\t') {
234                         cp++;
235                         continue;
236                 }
237                 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
238                         *q++ = cp;
239                 cp = strchr(p, ' ');
240                 if (cp != NULL)
241                         *cp++ = '\0';
242                 else {
243                         cp = strchr(p, '\t');
244                         if (cp != NULL)
245                                 *cp++ = '\0';
246                 }
247         }
248         *q = NULL;
249         return (&d->rpc);
250 }