getrpcent.c revision 1.4.4.1 1 /* $NetBSD: getrpcent.c,v 1.4.4.1 1996/09/16 23:44:26 jtc 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 #if defined(LIBC_SCCS) && !defined(lint)
34 /*static char *sccsid = "from: @(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";*/
35 static char *rcsid = "$NetBSD: getrpcent.c,v 1.4.4.1 1996/09/16 23:44:26 jtc Exp $";
36 #endif
37
38 /*
39 * Copyright (c) 1984 by Sun Microsystems, Inc.
40 */
41
42 #include "namespace.h"
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <sys/types.h>
46 #include <string.h>
47 #include <rpc/rpc.h>
48
49 #ifdef __weak_alias
50 __weak_alias(endrpcent,_endrpcent);
51 __weak_alias(getrpcbyname,_getrpcbyname);
52 __weak_alias(getrpcbynumber,_getrpcbynumber);
53 __weak_alias(getrpcent,_getrpcent);
54 __weak_alias(setrpcent,_setrpcent);
55 #endif
56
57 /*
58 * Internet version.
59 */
60 struct rpcdata {
61 FILE *rpcf;
62 int stayopen;
63 #define MAXALIASES 35
64 char *rpc_aliases[MAXALIASES];
65 struct rpcent rpc;
66 char line[BUFSIZ+1];
67 } *rpcdata;
68
69 static struct rpcent *interpret();
70 struct hostent *gethostent();
71 char *inet_ntoa();
72
73 static char RPCDB[] = "/etc/rpc";
74
75 static struct rpcdata *
76 _rpcdata()
77 {
78 register struct rpcdata *d = rpcdata;
79
80 if (d == 0) {
81 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
82 rpcdata = d;
83 }
84 return (d);
85 }
86
87 struct rpcent *
88 getrpcbynumber(number)
89 register int number;
90 {
91 register struct rpcdata *d = _rpcdata();
92 register struct rpcent *p;
93
94 if (d == 0)
95 return (0);
96 setrpcent(0);
97 while (p = getrpcent()) {
98 if (p->r_number == number)
99 break;
100 }
101 endrpcent();
102 return (p);
103 }
104
105 struct rpcent *
106 getrpcbyname(name)
107 char *name;
108 {
109 struct rpcent *rpc;
110 char **rp;
111
112 setrpcent(0);
113 while (rpc = getrpcent()) {
114 if (strcmp(rpc->r_name, name) == 0)
115 return (rpc);
116 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
117 if (strcmp(*rp, name) == 0)
118 return (rpc);
119 }
120 }
121 endrpcent();
122 return (NULL);
123 }
124
125 void
126 setrpcent(f)
127 int f;
128 {
129 register struct rpcdata *d = _rpcdata();
130
131 if (d == 0)
132 return;
133 if (d->rpcf == NULL)
134 d->rpcf = fopen(RPCDB, "r");
135 else
136 rewind(d->rpcf);
137 d->stayopen |= f;
138 }
139
140 void
141 endrpcent()
142 {
143 register struct rpcdata *d = _rpcdata();
144
145 if (d == 0)
146 return;
147 if (d->rpcf && !d->stayopen) {
148 fclose(d->rpcf);
149 d->rpcf = NULL;
150 }
151 }
152
153 struct rpcent *
154 getrpcent()
155 {
156 struct rpcent *hp;
157 int reason;
158 register struct rpcdata *d = _rpcdata();
159
160 if (d == 0)
161 return(NULL);
162 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
163 return (NULL);
164 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
165 return (NULL);
166 return (interpret(d->line, strlen(d->line)));
167 }
168
169 static struct rpcent *
170 interpret(val, len)
171 char *val;
172 int len;
173 {
174 register struct rpcdata *d = _rpcdata();
175 char *p;
176 register char *cp, **q;
177
178 if (d == 0)
179 return (0);
180 (void) strncpy(d->line, val, len);
181 p = d->line;
182 d->line[len] = '\n';
183 if (*p == '#')
184 return (getrpcent());
185 cp = strpbrk(p, "#\n");
186 if (cp == NULL)
187 return (getrpcent());
188 *cp = '\0';
189 cp = strpbrk(p, " \t");
190 if (cp == NULL)
191 return (getrpcent());
192 *cp++ = '\0';
193 /* THIS STUFF IS INTERNET SPECIFIC */
194 d->rpc.r_name = d->line;
195 while (*cp == ' ' || *cp == '\t')
196 cp++;
197 d->rpc.r_number = atoi(cp);
198 q = d->rpc.r_aliases = d->rpc_aliases;
199 cp = strpbrk(cp, " \t");
200 if (cp != NULL)
201 *cp++ = '\0';
202 while (cp && *cp) {
203 if (*cp == ' ' || *cp == '\t') {
204 cp++;
205 continue;
206 }
207 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
208 *q++ = cp;
209 cp = strpbrk(cp, " \t");
210 if (cp != NULL)
211 *cp++ = '\0';
212 }
213 *q = NULL;
214 return (&d->rpc);
215 }
216
217