#if !defined(lint) && defined(SCCSIDS)
static char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11 Copyr 1984 Sun Micro";
#endif
#include <stdio.h>
#include <sys/types.h>
#include <netdb.h>
#include <gssrpc/rpc.h>
#include <string.h>
#include <sys/socket.h>
SETRPCENT_TYPE setrpcent (int);
ENDRPCENT_TYPE endrpcent (void);
struct rpcdata {
FILE *rpcf;
char *current;
int currentlen;
int stayopen;
#define MAXALIASES 35
char *rpc_aliases[MAXALIASES];
struct rpcent rpc;
char line[BUFSIZ+1];
char *domain;
} *rpcdata;
static struct rpcdata *get_rpcdata();
static struct rpcent *interpret();
struct hostent *gethostent();
char *inet_ntoa();
static char RPCDB[] = "/etc/rpc";
static struct rpcdata *
get_rpcdata(void)
{
register struct rpcdata *d = rpcdata;
if (d == 0) {
d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
rpcdata = d;
}
return (d);
}
struct rpcent *
getrpcbynumber(register int number)
{
register struct rpcdata *d = get_rpcdata();
register struct rpcent *p;
int reason;
char adrstr[16], *val = NULL;
int vallen;
if (d == 0)
return (0);
setrpcent(0);
while (p = getrpcent()) {
if (p->r_number == number)
break;
}
endrpcent();
return (p);
}
struct rpcent *
getrpcbyname(const char *name)
{
struct rpcent *rpc;
char **rp;
setrpcent(0);
while(rpc = 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);
}
}
endrpcent();
return (NULL);
}
SETRPCENT_TYPE setrpcent(int f)
{
register struct rpcdata *d = _rpcdata();
if (d == 0)
return;
if (d->rpcf == NULL)
d->rpcf = fopen(RPCDB, "r");
else
rewind(d->rpcf);
if (d->current)
free(d->current);
d->current = NULL;
d->stayopen |= f;
}
ENDRPCENT_TYPE endrpcent(void)
{
register struct rpcdata *d = _rpcdata();
if (d == 0)
return;
if (d->current && !d->stayopen) {
free(d->current);
d->current = NULL;
}
if (d->rpcf && !d->stayopen) {
fclose(d->rpcf);
d->rpcf = NULL;
}
}
struct rpcent *
getrpcent(void)
{
struct rpcent *hp;
int reason;
char *key = NULL, *val = NULL;
int keylen, vallen;
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(char *val, int len)
{
register struct rpcdata *d = _rpcdata();
char *p;
register char *cp, **q;
if (d == 0)
return;
strncpy(d->line, val, len);
p = d->line;
d->line[len] = '\n';
if (*p == '#')
return (getrpcent());
cp = strchr(p, '#');
if (cp == NULL)
{
cp = strchr(p, '\n');
if (cp == NULL)
return (getrpcent());
}
*cp = '\0';
cp = strchr(p, ' ');
if (cp == NULL)
{
cp = strchr(p, '\t');
if (cp == NULL)
return (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 = strchr(p, ' ');
if (cp != NULL)
*cp++ = '\0';
else
{
cp = strchr(p, '\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 = strchr(p, ' ');
if (cp != NULL)
*cp++ = '\0';
else
{
cp = strchr(p, '\t');
if (cp != NULL)
*cp++ = '\0';
}
}
*q = NULL;
return (&d->rpc);
}