xref: /original-bsd/lib/librpc/rpc/getrpcent.c (revision 9e0ce84d)
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
5 
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.
13  *
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.
17  *
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.
21  *
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.
25  *
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.
29  *
30  * Sun Microsystems, Inc.
31  * 2550 Garcia Avenue
32  * Mountain View, California  94043
33  */
34 
35 /*
36  * Copyright (c) 1985 by Sun Microsystems, Inc.
37  */
38 
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <rpc/rpc.h>
42 #include <netdb.h>
43 #include <sys/socket.h>
44 
45 /*
46  * Internet version.
47  */
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();
59 
60 static	struct rpcent *interpret();
61 struct	hostent *gethostent();
62 char	*inet_ntoa();
63 char	*index();
64 
65 static char RPCDB[] = "/etc/rpc";
66 
67 static struct rpcdata *
68 _rpcdata()
69 {
70 	register struct rpcdata *d = rpcdata;
71 
72 	if (d == 0) {
73 		d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
74 		rpcdata = d;
75 	}
76 	return (d);
77 }
78 
79 struct rpcent *
80 getrpcbynumber(number)
81 	register int number;
82 {
83 	register struct rpcdata *d = _rpcdata();
84 	register struct rpcent *p;
85 	int reason;
86 	char adrstr[16], *val = NULL;
87 	int vallen;
88 
89 	if (d == 0)
90 		return (0);
91 	setrpcent(0);
92 	while (p = getrpcent()) {
93 		if (p->r_number == number)
94 			break;
95 	}
96 	endrpcent();
97 	return (p);
98 }
99 
100 struct rpcent *
101 getrpcbyname(name)
102 	char *name;
103 {
104 	struct rpcent *rpc;
105 	char **rp;
106 
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);
114 		}
115 	}
116 	endrpcent();
117 	return (NULL);
118 }
119 
120 setrpcent(f)
121 	int f;
122 {
123 	register struct rpcdata *d = _rpcdata();
124 
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;
135 }
136 
137 endrpcent()
138 {
139 	register struct rpcdata *d = _rpcdata();
140 
141 	if (d == 0)
142 		return;
143 	if (d->current && !d->stayopen) {
144 		free(d->current);
145 		d->current = NULL;
146 	}
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 	char *key = NULL, *val = NULL;
159 	int keylen, vallen;
160 	register struct rpcdata *d = _rpcdata();
161 
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));
169 }
170 
171 static struct rpcent *
172 interpret(val, len)
173 {
174 	register struct rpcdata *d = _rpcdata();
175 	char *p;
176 	register char *cp, **q;
177 
178 	if (d == 0)
179 		return;
180 	strncpy(d->line, val, len);
181 	p = d->line;
182 	d->line[len] = '\n';
183 	if (*p == '#')
184 		return (getrpcent());
185 	cp = index(p, '#');
186 	if (cp == NULL)
187     {
188 		cp = index(p, '\n');
189 		if (cp == NULL)
190 			return (getrpcent());
191 	}
192 	*cp = '\0';
193 	cp = index(p, ' ');
194 	if (cp == NULL)
195     {
196 		cp = index(p, '\t');
197 		if (cp == NULL)
198 			return (getrpcent());
199 	}
200 	*cp++ = '\0';
201 	/* THIS STUFF IS INTERNET SPECIFIC */
202 	d->rpc.r_name = d->line;
203 	while (*cp == ' ' || *cp == '\t')
204 		cp++;
205 	d->rpc.r_number = atoi(cp);
206 	q = d->rpc.r_aliases = d->rpc_aliases;
207 	cp = index(p, ' ');
208 	if (cp != NULL)
209 		*cp++ = '\0';
210 	else
211     {
212 		cp = index(p, '\t');
213 		if (cp != NULL)
214 			*cp++ = '\0';
215 	}
216 	while (cp && *cp) {
217 		if (*cp == ' ' || *cp == '\t') {
218 			cp++;
219 			continue;
220 		}
221 		if (q < &(d->rpc_aliases[MAXALIASES - 1]))
222 			*q++ = cp;
223 		cp = index(p, ' ');
224 		if (cp != NULL)
225 			*cp++ = '\0';
226 		else
227 	    {
228 			cp = index(p, '\t');
229 			if (cp != NULL)
230 				*cp++ = '\0';
231 		}
232 	}
233 	*q = NULL;
234 	return (&d->rpc);
235 }
236