#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$Id: getrpcent.c,v 1.3 2002/02/19 20:36:23 epeyton Exp $";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <rpc/rpc.h>
void _old_setrpcent(int f);
struct rpcent *_old_getrpcent();
void _old_endrpcent();
struct rpcdata {
FILE *rpcf;
int stayopen;
#define MAXALIASES 35
char *rpc_aliases[MAXALIASES];
struct rpcent rpc;
char line[BUFSIZ+1];
} *rpcdata;
static struct rpcent *interpret();
struct hostent *gethostent();
char *inet_ntoa();
static char RPCDB[] = "/etc/rpc";
static struct rpcdata *
_rpcdata()
{
register struct rpcdata *d = rpcdata;
if (d == 0) {
d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
rpcdata = d;
}
return (d);
}
struct rpcent *
_old_getrpcbynumber(number)
#ifdef __LP64__
int32_t number;
#else
register long number;
#endif
{
register struct rpcdata *d = _rpcdata();
register struct rpcent *p;
#ifdef __LP64__
int x;
x = number;
#endif
if (d == 0)
return (0);
_old_setrpcent(0);
while ((p = _old_getrpcent()))
{
#ifdef __LP64__
if (p->r_number == x) break;
#else
if (p->r_number == number) break;
#endif
}
_old_endrpcent();
return (p);
}
struct rpcent *
_old_getrpcbyname(name)
#if defined(__APPLE__)
const char *name;
#else
char *name;
#endif
{
struct rpcent *rpc;
char **rp;
_old_setrpcent(0);
while ((rpc = _old_getrpcent())) {
if (strcmp(rpc->r_name, name) == 0)
return (rpc);
for (rp = rpc->r_aliases; *rp != NULL; rp++) {
if (strcmp(*rp, name) == 0)
return (rpc);
}
}
_old_endrpcent();
return (NULL);
}
void
_old_setrpcent(f)
int f;
{
register struct rpcdata *d = _rpcdata();
if (d == 0)
return;
if (d->rpcf == NULL)
d->rpcf = fopen(RPCDB, "r");
else
rewind(d->rpcf);
d->stayopen |= f;
}
void
_old_endrpcent()
{
register struct rpcdata *d = _rpcdata();
if (d == 0)
return;
if (d->rpcf && !d->stayopen) {
fclose(d->rpcf);
d->rpcf = NULL;
}
}
struct rpcent *
_old_getrpcent()
{
register struct rpcdata *d = _rpcdata();
if (d == 0)
return(NULL);
if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
return (NULL);
if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
return (NULL);
return (interpret(d->line, strlen(d->line)));
}
static struct rpcent *
interpret(val, len)
char *val;
u_int len;
{
register struct rpcdata *d = _rpcdata();
char *p;
register char *cp, **q;
if (d == 0)
return (0);
(void) strncpy(d->line, val, len);
p = d->line;
d->line[len] = '\n';
if (*p == '#')
return (_old_getrpcent());
cp = strpbrk(p, "#\n");
if (cp == NULL)
return (_old_getrpcent());
*cp = '\0';
cp = strpbrk(p, " \t");
if (cp == NULL)
return (_old_getrpcent());
*cp++ = '\0';
d->rpc.r_name = d->line;
while (*cp == ' ' || *cp == '\t')
cp++;
d->rpc.r_number = atoi(cp);
q = d->rpc.r_aliases = d->rpc_aliases;
cp = strpbrk(cp, " \t");
if (cp != NULL)
*cp++ = '\0';
while (cp && *cp) {
if (*cp == ' ' || *cp == '\t') {
cp++;
continue;
}
if (q < &(d->rpc_aliases[MAXALIASES - 1]))
*q++ = cp;
cp = strpbrk(cp, " \t");
if (cp != NULL)
*cp++ = '\0';
}
*q = NULL;
return (&d->rpc);
}