getrpcent.c revision 1.9 1 /* $NetBSD: getrpcent.c,v 1.9 1998/02/10 04:54:34 lukem Exp $ */
2
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user or with the express written consent of
10 * Sun Microsystems, Inc.
11 *
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 *
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
19 *
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
23 *
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
27 *
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California 94043
31 */
32
33 #include <sys/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 #if 0
36 static char *sccsid = "@(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";
37 #else
38 __RCSID("$NetBSD: getrpcent.c,v 1.9 1998/02/10 04:54:34 lukem Exp $");
39 #endif
40 #endif
41
42 /*
43 * Copyright (c) 1984 by Sun Microsystems, Inc.
44 */
45
46 #include "namespace.h"
47
48 #include <sys/types.h>
49
50 #include <netdb.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include <rpc/rpc.h>
56
57 #ifdef __weak_alias
58 __weak_alias(endrpcent,_endrpcent);
59 __weak_alias(getrpcbyname,_getrpcbyname);
60 __weak_alias(getrpcbynumber,_getrpcbynumber);
61 __weak_alias(getrpcent,_getrpcent);
62 __weak_alias(setrpcent,_setrpcent);
63 #endif
64
65 /*
66 * Internet version.
67 */
68 struct rpcdata {
69 FILE *rpcf;
70 int stayopen;
71 #define MAXALIASES 35
72 char *rpc_aliases[MAXALIASES];
73 struct rpcent rpc;
74 char line[BUFSIZ+1];
75 } *rpcdata;
76
77 static struct rpcent *interpret __P((char *val, int len));
78
79 static char RPCDB[] = "/etc/rpc";
80
81 static struct rpcdata *_rpcdata __P((void));
82
83 static struct rpcdata *
84 _rpcdata()
85 {
86 struct rpcdata *d = rpcdata;
87
88 if (d == NULL) {
89 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
90 rpcdata = d;
91 }
92 return (d);
93 }
94
95 struct rpcent *
96 getrpcbynumber(number)
97 u_int32_t number;
98 {
99 struct rpcent *rpc;
100
101 setrpcent(0);
102 while ((rpc = getrpcent()) != NULL) {
103 if (rpc->r_number == number)
104 break;
105 }
106 endrpcent();
107 return (rpc);
108 }
109
110 struct rpcent *
111 getrpcbyname(name)
112 const char *name;
113 {
114 struct rpcent *rpc;
115 char **rp;
116
117 setrpcent(0);
118 while ((rpc = getrpcent()) != NULL) {
119 if (strcmp(rpc->r_name, name) == 0)
120 break;
121 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
122 if (strcmp(*rp, name) == 0)
123 break;
124 }
125 }
126 endrpcent();
127 return (rpc);
128 }
129
130 void
131 setrpcent(f)
132 int f;
133 {
134 struct rpcdata *d = _rpcdata();
135
136 if (d == NULL)
137 return;
138 if (d->rpcf == NULL)
139 d->rpcf = fopen(RPCDB, "r");
140 else
141 rewind(d->rpcf);
142 d->stayopen |= f;
143 }
144
145 void
146 endrpcent()
147 {
148 struct rpcdata *d = _rpcdata();
149
150 if (d == NULL)
151 return;
152 if (d->rpcf && !d->stayopen) {
153 fclose(d->rpcf);
154 d->rpcf = NULL;
155 }
156 }
157
158 struct rpcent *
159 getrpcent()
160 {
161 struct rpcdata *d = _rpcdata();
162
163 if (d == NULL)
164 return(NULL);
165 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
166 return (NULL);
167 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
168 return (NULL);
169 return (interpret(d->line, strlen(d->line)));
170 }
171
172 static struct rpcent *
173 interpret(val, len)
174 char *val;
175 int len;
176 {
177 struct rpcdata *d = _rpcdata();
178 char *p;
179 char *cp, **q;
180
181 if (d == NULL)
182 return (0);
183 (void) strncpy(d->line, val, len);
184 p = d->line;
185 d->line[len] = '\n';
186 if (*p == '#')
187 return (getrpcent());
188 cp = strpbrk(p, "#\n");
189 if (cp == NULL)
190 return (getrpcent());
191 *cp = '\0';
192 cp = strpbrk(p, " \t");
193 if (cp == NULL)
194 return (getrpcent());
195 *cp++ = '\0';
196 /* THIS STUFF IS INTERNET SPECIFIC */
197 d->rpc.r_name = d->line;
198 while (*cp == ' ' || *cp == '\t')
199 cp++;
200 d->rpc.r_number = atoi(cp);
201 q = d->rpc.r_aliases = d->rpc_aliases;
202 cp = strpbrk(cp, " \t");
203 if (cp != NULL)
204 *cp++ = '\0';
205 while (cp && *cp) {
206 if (*cp == ' ' || *cp == '\t') {
207 cp++;
208 continue;
209 }
210 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
211 *q++ = cp;
212 cp = strpbrk(cp, " \t");
213 if (cp != NULL)
214 *cp++ = '\0';
215 }
216 *q = NULL;
217 return (&d->rpc);
218 }
219