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>
51 static struct rpcdata {
57 char *rpc_aliases[MAXALIASES];
59 char line[BUFSIZ + 1];
63 static const char RPCDB[] = "/etc/rpc";
65 static struct rpcdata *_rpcdata(void)
67 register struct rpcdata *d = rpcdata;
70 d = (struct rpcdata *) calloc(1, sizeof(struct rpcdata));
79 register struct rpcdata *d = _rpcdata();
92 libc_hidden_def(endrpcent)
96 register struct rpcdata *d = _rpcdata();
101 d->rpcf = fopen(RPCDB, "r");
108 libc_hidden_def(setrpcent)
110 static struct rpcent *interpret(struct rpcdata *);
112 static struct rpcent *__get_next_rpcent(struct rpcdata *d)
114 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
119 struct rpcent *getrpcent(void)
121 register struct rpcdata *d = _rpcdata();
125 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
127 return __get_next_rpcent(d);
129 libc_hidden_def(getrpcent)
131 struct rpcent *getrpcbynumber(register int number)
133 register struct rpcdata *d = _rpcdata();
134 register struct rpcent *rpc;
139 while ((rpc = getrpcent())) {
140 if (rpc->r_number == number)
146 libc_hidden_def(getrpcbynumber)
148 struct rpcent *getrpcbyname(const char *name)
154 while ((rpc = getrpcent())) {
155 if (strcmp(rpc->r_name, name) == 0)
157 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
158 if (strcmp(*rp, name) == 0)
165 libc_hidden_def(getrpcbyname)
168 static char *firstwhite(char *s)
173 s2 = strchr(s, '\t');
176 return (s1 < s2) ? s1 : s2;
184 static struct rpcent *interpret(register struct rpcdata *d)
187 register char *cp, **q;
190 d->line[strlen(p)-1] = '\n';
192 return __get_next_rpcent(d);
195 cp = strchr(p, '\n');
197 return __get_next_rpcent(d);
201 if ((cp = firstwhite(p)))
204 return __get_next_rpcent(d);
208 cp = strchr(p, '\t');
210 return __get_next_rpcent(d);
214 /* THIS STUFF IS INTERNET SPECIFIC */
215 d->rpc.r_name = d->line;
216 while (*cp == ' ' || *cp == '\t')
218 d->rpc.r_number = atoi(cp);
219 q = d->rpc.r_aliases = d->rpc_aliases;
221 if ((cp = firstwhite(cp)))
228 cp = strchr(p, '\t');
234 if (*cp == ' ' || *cp == '\t') {
238 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
241 if ((cp = firstwhite(cp)))
248 cp = strchr(p, '\t');
258 #if defined(__UCLIBC_HAS_REENTRANT_RPC__)
260 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
263 static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer,
264 size_t buflen, struct rpcent **result)
273 /* copy the struct from the shared mem */
274 memset(result_buf, 0x00, sizeof(*result_buf));
275 memset(buffer, 0x00, buflen);
277 result_buf->r_number = r->r_number;
279 /* copy the aliases ... need to not only copy the alias strings,
280 * but the array of pointers to the alias strings */
282 while (r->r_aliases[i++]) ;
284 s = i-- * sizeof(char*);
287 result_buf->r_aliases = (char**)buffer;
292 s = strlen(r->r_aliases[i]) + 1;
295 result_buf->r_aliases[i] = buffer;
298 memcpy(result_buf->r_aliases[i], r->r_aliases[i], s);
302 i = strlen(r->r_name);
305 result_buf->r_name = buffer;
306 memcpy(result_buf->r_name, r->r_name, i);
308 /* that was a hoot eh ? */
309 *result = result_buf;
316 int getrpcbynumber_r(int number, struct rpcent *result_buf, char *buffer,
317 size_t buflen, struct rpcent **result)
320 __UCLIBC_MUTEX_LOCK(mylock);
321 ret = __copy_rpcent(getrpcbynumber(number), result_buf, buffer, buflen, result);
322 __UCLIBC_MUTEX_UNLOCK(mylock);
326 int getrpcbyname_r(const char *name, struct rpcent *result_buf, char *buffer,
327 size_t buflen, struct rpcent **result)
330 __UCLIBC_MUTEX_LOCK(mylock);
331 ret = __copy_rpcent(getrpcbyname(name), result_buf, buffer, buflen, result);
332 __UCLIBC_MUTEX_UNLOCK(mylock);
336 int getrpcent_r(struct rpcent *result_buf, char *buffer,
337 size_t buflen, struct rpcent **result)
340 __UCLIBC_MUTEX_LOCK(mylock);
341 ret = __copy_rpcent(getrpcent(), result_buf, buffer, buflen, result);
342 __UCLIBC_MUTEX_UNLOCK(mylock);
346 #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */