xref: /csrg-svn/lib/librpc/rpc/getrpcent.c (revision 54677)
145081Smckusick /* @(#)getrpcent.c	2.2 88/07/29 4.0 RPCSRC */
245081Smckusick #if !defined(lint) && defined(SCCSIDS)
345081Smckusick static  char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11  Copyr 1984 Sun Micro";
445081Smckusick #endif
545081Smckusick 
645081Smckusick /*
745081Smckusick  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
845081Smckusick  * unrestricted use provided that this legend is included on all tape
945081Smckusick  * media and as a part of the software program in whole or part.  Users
1045081Smckusick  * may copy or modify Sun RPC without charge, but are not authorized
1145081Smckusick  * to license or distribute it to anyone else except as part of a product or
1245081Smckusick  * program developed by the user.
1345081Smckusick  *
1445081Smckusick  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1545081Smckusick  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1645081Smckusick  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1745081Smckusick  *
1845081Smckusick  * Sun RPC is provided with no support and without any obligation on the
1945081Smckusick  * part of Sun Microsystems, Inc. to assist in its use, correction,
2045081Smckusick  * modification or enhancement.
2145081Smckusick  *
2245081Smckusick  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
2345081Smckusick  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2445081Smckusick  * OR ANY PART THEREOF.
2545081Smckusick  *
2645081Smckusick  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2745081Smckusick  * or profits or other special, indirect and consequential damages, even if
2845081Smckusick  * Sun has been advised of the possibility of such damages.
2945081Smckusick  *
3045081Smckusick  * Sun Microsystems, Inc.
3145081Smckusick  * 2550 Garcia Avenue
3245081Smckusick  * Mountain View, California  94043
3345081Smckusick  */
3445081Smckusick 
3545081Smckusick /*
3645081Smckusick  * Copyright (c) 1985 by Sun Microsystems, Inc.
3745081Smckusick  */
3845081Smckusick 
3945081Smckusick #include <stdio.h>
4045081Smckusick #include <sys/types.h>
4145081Smckusick #include <rpc/rpc.h>
4245081Smckusick #include <netdb.h>
4345081Smckusick #include <sys/socket.h>
4445081Smckusick 
4545081Smckusick /*
4645081Smckusick  * Internet version.
4745081Smckusick  */
4845081Smckusick struct rpcdata {
4945081Smckusick 	FILE	*rpcf;
5045081Smckusick 	char	*current;
5145081Smckusick 	int	currentlen;
5245081Smckusick 	int	stayopen;
5345081Smckusick #define	MAXALIASES	35
5445081Smckusick 	char	*rpc_aliases[MAXALIASES];
5545081Smckusick 	struct	rpcent rpc;
5645081Smckusick 	char	line[BUFSIZ+1];
5745081Smckusick 	char	*domain;
58*54677Storek } *rpcdata;
5945081Smckusick 
6045081Smckusick static	struct rpcent *interpret();
6145081Smckusick struct	hostent *gethostent();
6245081Smckusick char	*inet_ntoa();
6351029Sralph char	*index();
6445081Smckusick 
6545081Smckusick static char RPCDB[] = "/etc/rpc";
6645081Smckusick 
6745081Smckusick static struct rpcdata *
_rpcdata()6845081Smckusick _rpcdata()
6945081Smckusick {
7045081Smckusick 	register struct rpcdata *d = rpcdata;
7145081Smckusick 
7245081Smckusick 	if (d == 0) {
7345081Smckusick 		d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
7445081Smckusick 		rpcdata = d;
7545081Smckusick 	}
7645081Smckusick 	return (d);
7745081Smckusick }
7845081Smckusick 
7945081Smckusick struct rpcent *
getrpcbynumber(number)8045081Smckusick getrpcbynumber(number)
8145081Smckusick 	register int number;
8245081Smckusick {
8345081Smckusick 	register struct rpcdata *d = _rpcdata();
8445081Smckusick 	register struct rpcent *p;
8545081Smckusick 	int reason;
8645081Smckusick 	char adrstr[16], *val = NULL;
8745081Smckusick 	int vallen;
8845081Smckusick 
8945081Smckusick 	if (d == 0)
9045081Smckusick 		return (0);
9145081Smckusick 	setrpcent(0);
9245081Smckusick 	while (p = getrpcent()) {
9345081Smckusick 		if (p->r_number == number)
9445081Smckusick 			break;
9545081Smckusick 	}
9645081Smckusick 	endrpcent();
9745081Smckusick 	return (p);
9845081Smckusick }
9945081Smckusick 
10045081Smckusick struct rpcent *
getrpcbyname(name)10145081Smckusick getrpcbyname(name)
10245081Smckusick 	char *name;
10345081Smckusick {
10445081Smckusick 	struct rpcent *rpc;
10545081Smckusick 	char **rp;
10645081Smckusick 
10745081Smckusick 	setrpcent(0);
10845081Smckusick 	while(rpc = getrpcent()) {
10945081Smckusick 		if (strcmp(rpc->r_name, name) == 0)
11045081Smckusick 			return (rpc);
11145081Smckusick 		for (rp = rpc->r_aliases; *rp != NULL; rp++) {
11245081Smckusick 			if (strcmp(*rp, name) == 0)
11345081Smckusick 				return (rpc);
11445081Smckusick 		}
11545081Smckusick 	}
11645081Smckusick 	endrpcent();
11745081Smckusick 	return (NULL);
11845081Smckusick }
11945081Smckusick 
setrpcent(f)12045081Smckusick setrpcent(f)
12145081Smckusick 	int f;
12245081Smckusick {
12345081Smckusick 	register struct rpcdata *d = _rpcdata();
12445081Smckusick 
12545081Smckusick 	if (d == 0)
12645081Smckusick 		return;
12745081Smckusick 	if (d->rpcf == NULL)
12845081Smckusick 		d->rpcf = fopen(RPCDB, "r");
12945081Smckusick 	else
13045081Smckusick 		rewind(d->rpcf);
13145081Smckusick 	if (d->current)
13245081Smckusick 		free(d->current);
13345081Smckusick 	d->current = NULL;
13445081Smckusick 	d->stayopen |= f;
13545081Smckusick }
13645081Smckusick 
endrpcent()13745081Smckusick endrpcent()
13845081Smckusick {
13945081Smckusick 	register struct rpcdata *d = _rpcdata();
14045081Smckusick 
14145081Smckusick 	if (d == 0)
14245081Smckusick 		return;
14345081Smckusick 	if (d->current && !d->stayopen) {
14445081Smckusick 		free(d->current);
14545081Smckusick 		d->current = NULL;
14645081Smckusick 	}
14745081Smckusick 	if (d->rpcf && !d->stayopen) {
14845081Smckusick 		fclose(d->rpcf);
14945081Smckusick 		d->rpcf = NULL;
15045081Smckusick 	}
15145081Smckusick }
15245081Smckusick 
15345081Smckusick struct rpcent *
getrpcent()15445081Smckusick getrpcent()
15545081Smckusick {
15645081Smckusick 	struct rpcent *hp;
15745081Smckusick 	int reason;
15845081Smckusick 	char *key = NULL, *val = NULL;
15945081Smckusick 	int keylen, vallen;
16045081Smckusick 	register struct rpcdata *d = _rpcdata();
16145081Smckusick 
16245081Smckusick 	if (d == 0)
16345081Smckusick 		return(NULL);
16445081Smckusick 	if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
16545081Smckusick 		return (NULL);
16645081Smckusick     if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
16745081Smckusick 		return (NULL);
16845081Smckusick 	return interpret(d->line, strlen(d->line));
16945081Smckusick }
17045081Smckusick 
17145081Smckusick static struct rpcent *
interpret(val,len)17245081Smckusick interpret(val, len)
17345081Smckusick {
17445081Smckusick 	register struct rpcdata *d = _rpcdata();
17545081Smckusick 	char *p;
17645081Smckusick 	register char *cp, **q;
17745081Smckusick 
17845081Smckusick 	if (d == 0)
17945081Smckusick 		return;
18045081Smckusick 	strncpy(d->line, val, len);
18145081Smckusick 	p = d->line;
18245081Smckusick 	d->line[len] = '\n';
18345081Smckusick 	if (*p == '#')
18445081Smckusick 		return (getrpcent());
18545081Smckusick 	cp = index(p, '#');
18645081Smckusick 	if (cp == NULL)
18745081Smckusick     {
18845081Smckusick 		cp = index(p, '\n');
18945081Smckusick 		if (cp == NULL)
19045081Smckusick 			return (getrpcent());
19145081Smckusick 	}
19245081Smckusick 	*cp = '\0';
19345081Smckusick 	cp = index(p, ' ');
19445081Smckusick 	if (cp == NULL)
19545081Smckusick     {
19645081Smckusick 		cp = index(p, '\t');
19745081Smckusick 		if (cp == NULL)
19845081Smckusick 			return (getrpcent());
19945081Smckusick 	}
20045081Smckusick 	*cp++ = '\0';
20145081Smckusick 	/* THIS STUFF IS INTERNET SPECIFIC */
20245081Smckusick 	d->rpc.r_name = d->line;
20345081Smckusick 	while (*cp == ' ' || *cp == '\t')
20445081Smckusick 		cp++;
20545081Smckusick 	d->rpc.r_number = atoi(cp);
20645081Smckusick 	q = d->rpc.r_aliases = d->rpc_aliases;
20745081Smckusick 	cp = index(p, ' ');
20845081Smckusick 	if (cp != NULL)
20945081Smckusick 		*cp++ = '\0';
21045081Smckusick 	else
21145081Smckusick     {
21245081Smckusick 		cp = index(p, '\t');
21345081Smckusick 		if (cp != NULL)
21445081Smckusick 			*cp++ = '\0';
21545081Smckusick 	}
21645081Smckusick 	while (cp && *cp) {
21745081Smckusick 		if (*cp == ' ' || *cp == '\t') {
21845081Smckusick 			cp++;
21945081Smckusick 			continue;
22045081Smckusick 		}
22145081Smckusick 		if (q < &(d->rpc_aliases[MAXALIASES - 1]))
22245081Smckusick 			*q++ = cp;
22345081Smckusick 		cp = index(p, ' ');
22445081Smckusick 		if (cp != NULL)
22545081Smckusick 			*cp++ = '\0';
22645081Smckusick 		else
22745081Smckusick 	    {
22845081Smckusick 			cp = index(p, '\t');
22945081Smckusick 			if (cp != NULL)
23045081Smckusick 				*cp++ = '\0';
23145081Smckusick 		}
23245081Smckusick 	}
23345081Smckusick 	*q = NULL;
23445081Smckusick 	return (&d->rpc);
23545081Smckusick }
236