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 /* Experimentally off - libc_hidden_proto(memcpy) */
48 /* Experimentally off - libc_hidden_proto(memset) */
49 /* Experimentally off - libc_hidden_proto(strchr) */
50 /* Experimentally off - libc_hidden_proto(strcmp) */
51 /* Experimentally off - 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));
87 libc_hidden_proto(endrpcent)
90 register struct rpcdata *d = _rpcdata();
103 libc_hidden_def(endrpcent)
105 libc_hidden_proto(setrpcent)
106 void setrpcent(int f)
108 register struct rpcdata *d = _rpcdata();
113 d->rpcf = fopen(RPCDB, "r");
120 libc_hidden_def(setrpcent)
122 static struct rpcent *interpret(struct rpcdata *);
124 static struct rpcent *__get_next_rpcent(struct rpcdata *d)
126 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
131 libc_hidden_proto(getrpcent)
132 struct rpcent *getrpcent(void)
134 register struct rpcdata *d = _rpcdata();
138 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
140 return __get_next_rpcent(d);
142 libc_hidden_def(getrpcent)
144 libc_hidden_proto(getrpcbynumber)
145 struct rpcent *getrpcbynumber(register int number)
147 register struct rpcdata *d = _rpcdata();
148 register struct rpcent *rpc;
153 while ((rpc = getrpcent())) {
154 if (rpc->r_number == number)
160 libc_hidden_def(getrpcbynumber)
162 libc_hidden_proto(getrpcbyname)
163 struct rpcent *getrpcbyname(const char *name)
169 while ((rpc = getrpcent())) {
170 if (strcmp(rpc->r_name, name) == 0)
172 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
173 if (strcmp(*rp, name) == 0)
180 libc_hidden_def(getrpcbyname)
183 static char *firstwhite(char *s)
188 s2 = strchr(s, '\t');
191 return (s1 < s2) ? s1 : s2;
199 static struct rpcent *interpret(register struct rpcdata *d)
202 register char *cp, **q;
205 d->line[strlen(p)-1] = '\n';
207 return __get_next_rpcent(d);
210 cp = strchr(p, '\n');
212 return __get_next_rpcent(d);
216 if ((cp = firstwhite(p)))
219 return __get_next_rpcent(d);
223 cp = strchr(p, '\t');
225 return __get_next_rpcent(d);
229 /* THIS STUFF IS INTERNET SPECIFIC */
230 d->rpc.r_name = d->line;
231 while (*cp == ' ' || *cp == '\t')
233 d->rpc.r_number = atoi(cp);
234 q = d->rpc.r_aliases = d->rpc_aliases;
236 if ((cp = firstwhite(cp)))
243 cp = strchr(p, '\t');
249 if (*cp == ' ' || *cp == '\t') {
253 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
256 if ((cp = firstwhite(cp)))
263 cp = strchr(p, '\t');
273 #if defined(__UCLIBC_HAS_REENTRANT_RPC__)
275 #include <bits/uClibc_mutex.h>
276 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
279 static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer,
280 size_t buflen, struct rpcent **result)
289 /* copy the struct from the shared mem */
290 memset(result_buf, 0x00, sizeof(*result_buf));
291 memset(buffer, 0x00, buflen);
293 result_buf->r_number = r->r_number;
295 /* copy the aliases ... need to not only copy the alias strings,
296 * but the array of pointers to the alias strings */
298 while (r->r_aliases[i++]) ;
300 s = i-- * sizeof(char*);
303 result_buf->r_aliases = (char**)buffer;
308 s = strlen(r->r_aliases[i]) + 1;
311 result_buf->r_aliases[i] = buffer;
314 memcpy(result_buf->r_aliases[i], r->r_aliases[i], s);
318 i = strlen(r->r_name);
321 result_buf->r_name = buffer;
322 memcpy(result_buf->r_name, r->r_name, i);
324 /* that was a hoot eh ? */
325 *result = result_buf;
332 int getrpcbynumber_r(int number, struct rpcent *result_buf, char *buffer,
333 size_t buflen, struct rpcent **result)
336 __UCLIBC_MUTEX_LOCK(mylock);
337 ret = __copy_rpcent(getrpcbynumber(number), result_buf, buffer, buflen, result);
338 __UCLIBC_MUTEX_UNLOCK(mylock);
342 int getrpcbyname_r(const char *name, struct rpcent *result_buf, char *buffer,
343 size_t buflen, struct rpcent **result)
346 __UCLIBC_MUTEX_LOCK(mylock);
347 ret = __copy_rpcent(getrpcbyname(name), result_buf, buffer, buflen, result);
348 __UCLIBC_MUTEX_UNLOCK(mylock);
352 int getrpcent_r(struct rpcent *result_buf, char *buffer,
353 size_t buflen, struct rpcent **result)
356 __UCLIBC_MUTEX_LOCK(mylock);
357 ret = __copy_rpcent(getrpcent(), result_buf, buffer, buflen, result);
358 __UCLIBC_MUTEX_UNLOCK(mylock);
362 #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */