1 /* @(#)getrpcent.c 2.2 88/07/29 4.0 RPCSRC */
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.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
33 * Copyright (c) 1985 by Sun Microsystems, Inc.
40 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <arpa/inet.h>
47 libc_hidden_proto(memcpy)
48 libc_hidden_proto(memset)
49 libc_hidden_proto(strchr)
50 libc_hidden_proto(strcmp)
51 libc_hidden_proto(strlen)
52 libc_hidden_proto(fopen)
53 libc_hidden_proto(fclose)
54 libc_hidden_proto(atoi)
55 libc_hidden_proto(rewind)
56 libc_hidden_proto(fgets)
61 static struct rpcdata {
67 char *rpc_aliases[MAXALIASES];
69 char line[BUFSIZ + 1];
73 static char RPCDB[] = "/etc/rpc";
75 static struct rpcdata *_rpcdata(void)
77 register struct rpcdata *d = rpcdata;
80 d = (struct rpcdata *) calloc(1, sizeof(struct rpcdata));
89 register struct rpcdata *d = _rpcdata();
104 libc_hidden_proto(endrpcent)
105 libc_hidden_def(endrpcent)
107 void setrpcent(int f)
109 register struct rpcdata *d = _rpcdata();
114 d->rpcf = fopen(RPCDB, "r");
122 libc_hidden_proto(setrpcent)
123 libc_hidden_def(setrpcent)
125 static struct rpcent *interpret(struct rpcdata *);
127 static struct rpcent *__get_next_rpcent(struct rpcdata *d)
129 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
134 struct rpcent *getrpcent(void)
136 register struct rpcdata *d = _rpcdata();
140 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
142 return __get_next_rpcent(d);
144 libc_hidden_proto(getrpcent)
145 libc_hidden_def(getrpcent)
147 struct rpcent *getrpcbynumber(register int number)
149 register struct rpcdata *d = _rpcdata();
150 register struct rpcent *rpc;
155 while ((rpc = getrpcent())) {
156 if (rpc->r_number == number)
162 libc_hidden_proto(getrpcbynumber)
163 libc_hidden_def(getrpcbynumber)
165 struct rpcent *getrpcbyname(const char *name)
171 while ((rpc = getrpcent())) {
172 if (strcmp(rpc->r_name, name) == 0)
174 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
175 if (strcmp(*rp, name) == 0)
182 libc_hidden_proto(getrpcbyname)
183 libc_hidden_def(getrpcbyname)
186 static char *firstwhite(char *s)
191 s2 = strchr(s, '\t');
194 return (s1 < s2) ? s1 : s2;
202 static struct rpcent *interpret(register struct rpcdata *d)
205 register char *cp, **q;
208 d->line[strlen(p)-1] = '\n';
210 return __get_next_rpcent(d);
213 cp = strchr(p, '\n');
215 return __get_next_rpcent(d);
219 if ((cp = firstwhite(p)))
222 return __get_next_rpcent(d);
226 cp = strchr(p, '\t');
228 return __get_next_rpcent(d);
232 /* THIS STUFF IS INTERNET SPECIFIC */
233 d->rpc.r_name = d->line;
234 while (*cp == ' ' || *cp == '\t')
236 d->rpc.r_number = atoi(cp);
237 q = d->rpc.r_aliases = d->rpc_aliases;
239 if ((cp = firstwhite(cp)))
246 cp = strchr(p, '\t');
252 if (*cp == ' ' || *cp == '\t') {
256 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
259 if ((cp = firstwhite(cp)))
266 cp = strchr(p, '\t');
276 #if defined(__UCLIBC_HAS_REENTRANT_RPC__)
278 #if defined(__UCLIBC_HAS_THREADS__)
279 # include <pthread.h>
280 static pthread_mutex_t rpcdata_lock = PTHREAD_MUTEX_INITIALIZER;
282 #define LOCK __pthread_mutex_lock(&rpcdata_lock)
283 #define UNLOCK __pthread_mutex_unlock(&rpcdata_lock)
285 static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer,
286 size_t buflen, struct rpcent **result)
295 /* copy the struct from the shared mem */
296 memset(result_buf, 0x00, sizeof(*result_buf));
297 memset(buffer, 0x00, buflen);
299 result_buf->r_number = r->r_number;
301 /* copy the aliases ... need to not only copy the alias strings,
302 * but the array of pointers to the alias strings */
304 while (r->r_aliases[i++]) ;
306 s = i-- * sizeof(char*);
309 result_buf->r_aliases = (char**)buffer;
314 s = strlen(r->r_aliases[i]) + 1;
317 result_buf->r_aliases[i] = buffer;
320 memcpy(result_buf->r_aliases[i], r->r_aliases[i], s);
324 i = strlen(r->r_name);
327 result_buf->r_name = buffer;
328 memcpy(result_buf->r_name, r->r_name, i);
330 /* that was a hoot eh ? */
331 *result = result_buf;
338 int getrpcbynumber_r(int number, struct rpcent *result_buf, char *buffer,
339 size_t buflen, struct rpcent **result)
343 ret = __copy_rpcent(getrpcbynumber(number), result_buf, buffer, buflen, result);
348 int getrpcbyname_r(const char *name, struct rpcent *result_buf, char *buffer,
349 size_t buflen, struct rpcent **result)
353 ret = __copy_rpcent(getrpcbyname(name), result_buf, buffer, buflen, result);
358 int getrpcent_r(struct rpcent *result_buf, char *buffer,
359 size_t buflen, struct rpcent **result)
363 ret = __copy_rpcent(getrpcent(), result_buf, buffer, buflen, result);
368 #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */