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