Fri Jun 14 01:51:47 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / sunrpc / getrpcent.c
blob0fc7affdf9ed73693098e802b23d15120440ed8c
1 /* @(#)getrpcent.c 2.2 88/07/29 4.0 RPCSRC */
2 #if !defined(lint) && defined(SCCSIDS)
3 static char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11 Copyr 1984 Sun Micro";
4 #endif
6 /*
7 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
8 * unrestricted use provided that this legend is included on all tape
9 * media and as a part of the software program in whole or part. Users
10 * may copy or modify Sun RPC without charge, but are not authorized
11 * to license or distribute it to anyone else except as part of a product or
12 * program developed by the user.
14 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
15 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
18 * Sun RPC is provided with no support and without any obligation on the
19 * part of Sun Microsystems, Inc. to assist in its use, correction,
20 * modification or enhancement.
22 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
23 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
24 * OR ANY PART THEREOF.
26 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
27 * or profits or other special, indirect and consequential damages, even if
28 * Sun has been advised of the possibility of such damages.
30 * Sun Microsystems, Inc.
31 * 2550 Garcia Avenue
32 * Mountain View, California 94043
36 * Copyright (c) 1985 by Sun Microsystems, Inc.
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <rpc/rpc.h>
42 #include <netdb.h>
43 #include <sys/socket.h>
46 * Internet version.
48 struct rpcdata {
49 FILE *rpcf;
50 char *current;
51 int currentlen;
52 int stayopen;
53 #define MAXALIASES 35
54 char *rpc_aliases[MAXALIASES];
55 struct rpcent rpc;
56 char line[BUFSIZ+1];
57 char *domain;
58 } *rpcdata, *_rpcdata();
60 static struct rpcent *interpret();
61 struct hostent *gethostent();
62 char *inet_ntoa();
63 extern char *index(); /* Changed from static by roland@gnu */
65 static char RPCDB[] = _PATH_RPC; /* Changed from "/etc/rpc" by roland@gnu */
67 static struct rpcdata *
68 _rpcdata()
70 register struct rpcdata *d = rpcdata;
72 if (d == 0) {
73 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
74 rpcdata = d;
76 return (d);
79 struct rpcent *
80 getrpcbynumber(number)
81 register int number;
83 register struct rpcdata *d = _rpcdata();
84 register struct rpcent *p;
85 int reason;
86 char adrstr[16], *val = NULL;
87 int vallen;
89 if (d == 0)
90 return (0);
91 setrpcent(0);
92 while (p = getrpcent()) {
93 if (p->r_number == number)
94 break;
96 endrpcent();
97 return (p);
100 struct rpcent *
101 getrpcbyname(name)
102 const char *name; /* const added by roland@gnu */
104 struct rpcent *rpc;
105 char **rp;
107 setrpcent(0);
108 while(rpc = getrpcent()) {
109 if (strcmp(rpc->r_name, name) == 0)
110 return (rpc);
111 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
112 if (strcmp(*rp, name) == 0)
113 return (rpc);
116 endrpcent();
117 return (NULL);
120 setrpcent(f)
121 int f;
123 register struct rpcdata *d = _rpcdata();
125 if (d == 0)
126 return;
127 if (d->rpcf == NULL)
128 d->rpcf = fopen(RPCDB, "r");
129 else
130 rewind(d->rpcf);
131 if (d->current)
132 free(d->current);
133 d->current = NULL;
134 d->stayopen |= f;
137 endrpcent()
139 register struct rpcdata *d = _rpcdata();
141 if (d == 0)
142 return;
143 if (d->current && !d->stayopen) {
144 free(d->current);
145 d->current = NULL;
147 if (d->rpcf && !d->stayopen) {
148 fclose(d->rpcf);
149 d->rpcf = NULL;
153 struct rpcent *
154 getrpcent()
156 struct rpcent *hp;
157 int reason;
158 char *key = NULL, *val = NULL;
159 int keylen, vallen;
160 register struct rpcdata *d = _rpcdata();
162 if (d == 0)
163 return(NULL);
164 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
165 return (NULL);
166 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
167 return (NULL);
168 return interpret(d->line, strlen(d->line));
171 static struct rpcent *
172 interpret(val, len)
173 char * val;
174 size_t len;
176 register struct rpcdata *d = _rpcdata();
177 char *p;
178 register char *cp, **q;
180 if (d == 0)
181 return;
182 strncpy(d->line, val, len);
183 p = d->line;
184 d->line[len] = '\n';
185 if (*p == '#')
186 return (getrpcent());
187 cp = strpbrk(p, "#\n");
188 if (cp == NULL)
189 return (getrpcent());
190 *cp = '\0';
191 cp = strpbrk(p, " \t");
192 if (cp == NULL)
193 return (getrpcent());
194 *cp++ = '\0';
195 /* THIS STUFF IS INTERNET SPECIFIC */
196 d->rpc.r_name = d->line;
197 while (*cp == ' ' || *cp == '\t')
198 cp++;
199 d->rpc.r_number = atoi(cp);
200 q = d->rpc.r_aliases = d->rpc_aliases;
201 cp = strpbrk(p, " \t");
202 if (cp != NULL)
203 *cp++ = '\0';
204 while (cp && *cp) {
205 if (*cp == ' ' || *cp == '\t') {
206 cp++;
207 continue;
209 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
210 *q++ = cp;
211 cp = strpbrk(p, " \t");
212 if (cp != NULL)
213 *cp++ = '\0';
215 *q = NULL;
216 return (&d->rpc);