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