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