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