Home | History | Annotate | Line # | Download | only in rpc
getrpcent.c revision 1.7
      1 /*	$NetBSD: getrpcent.c,v 1.7 1997/07/13 20:13:10 christos Exp $	*/
      2 
      3 /*
      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 or with the express written consent of
     10  * Sun Microsystems, Inc.
     11  *
     12  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     13  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     14  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     15  *
     16  * Sun RPC is provided with no support and without any obligation on the
     17  * part of Sun Microsystems, Inc. to assist in its use, correction,
     18  * modification or enhancement.
     19  *
     20  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     21  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     22  * OR ANY PART THEREOF.
     23  *
     24  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     25  * or profits or other special, indirect and consequential damages, even if
     26  * Sun has been advised of the possibility of such damages.
     27  *
     28  * Sun Microsystems, Inc.
     29  * 2550 Garcia Avenue
     30  * Mountain View, California  94043
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #if defined(LIBC_SCCS) && !defined(lint)
     35 #if 0
     36 static char *sccsid = "@(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";
     37 #else
     38 __RCSID("$NetBSD: getrpcent.c,v 1.7 1997/07/13 20:13:10 christos Exp $");
     39 #endif
     40 #endif
     41 
     42 /*
     43  * Copyright (c) 1984 by Sun Microsystems, Inc.
     44  */
     45 
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <sys/types.h>
     49 #include <string.h>
     50 #include <rpc/rpc.h>
     51 #include <netdb.h>
     52 #include <arpa/inet.h>
     53 
     54 /*
     55  * Internet version.
     56  */
     57 struct rpcdata {
     58 	FILE	*rpcf;
     59 	int	stayopen;
     60 #define	MAXALIASES	35
     61 	char	*rpc_aliases[MAXALIASES];
     62 	struct	rpcent rpc;
     63 	char	line[BUFSIZ+1];
     64 } *rpcdata;
     65 
     66 static	struct rpcent *interpret __P((char *val, int len));
     67 
     68 static char RPCDB[] = "/etc/rpc";
     69 
     70 static struct rpcdata *_rpcdata __P((void));
     71 
     72 static struct rpcdata *
     73 _rpcdata()
     74 {
     75 	register struct rpcdata *d = rpcdata;
     76 
     77 	if (d == 0) {
     78 		d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
     79 		rpcdata = d;
     80 	}
     81 	return (d);
     82 }
     83 
     84 struct rpcent *
     85 getrpcbynumber(number)
     86 	register int number;
     87 {
     88 	struct rpcent *rpc;
     89 
     90 	setrpcent(0);
     91 	while ((rpc = getrpcent()) != NULL) {
     92 		if (rpc->r_number == number)
     93 			break;
     94 	}
     95 	endrpcent();
     96 	return (rpc);
     97 }
     98 
     99 struct rpcent *
    100 getrpcbyname(name)
    101 	char *name;
    102 {
    103 	struct rpcent *rpc;
    104 	char **rp;
    105 
    106 	setrpcent(0);
    107 	while ((rpc = getrpcent()) != NULL) {
    108 		if (strcmp(rpc->r_name, name) == 0)
    109 			break;
    110 		for (rp = rpc->r_aliases; *rp != NULL; rp++) {
    111 			if (strcmp(*rp, name) == 0)
    112 				break;
    113 		}
    114 	}
    115 	endrpcent();
    116 	return (rpc);
    117 }
    118 
    119 void
    120 setrpcent(f)
    121 	int f;
    122 {
    123 	register struct rpcdata *d = _rpcdata();
    124 
    125 	if (d == 0)
    126 		return;
    127 	if (d->rpcf == NULL)
    128 		d->rpcf = fopen(RPCDB, "r");
    129 	else
    130 		rewind(d->rpcf);
    131 	d->stayopen |= f;
    132 }
    133 
    134 void
    135 endrpcent()
    136 {
    137 	register struct rpcdata *d = _rpcdata();
    138 
    139 	if (d == 0)
    140 		return;
    141 	if (d->rpcf && !d->stayopen) {
    142 		fclose(d->rpcf);
    143 		d->rpcf = NULL;
    144 	}
    145 }
    146 
    147 struct rpcent *
    148 getrpcent()
    149 {
    150 	register struct rpcdata *d = _rpcdata();
    151 
    152 	if (d == 0)
    153 		return(NULL);
    154 	if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
    155 		return (NULL);
    156         if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
    157 		return (NULL);
    158 	return (interpret(d->line, strlen(d->line)));
    159 }
    160 
    161 static struct rpcent *
    162 interpret(val, len)
    163 	char *val;
    164 	int len;
    165 {
    166 	register struct rpcdata *d = _rpcdata();
    167 	char *p;
    168 	register char *cp, **q;
    169 
    170 	if (d == 0)
    171 		return (0);
    172 	(void) strncpy(d->line, val, len);
    173 	p = d->line;
    174 	d->line[len] = '\n';
    175 	if (*p == '#')
    176 		return (getrpcent());
    177 	cp = strpbrk(p, "#\n");
    178 	if (cp == NULL)
    179 		return (getrpcent());
    180 	*cp = '\0';
    181 	cp = strpbrk(p, " \t");
    182 	if (cp == NULL)
    183 		return (getrpcent());
    184 	*cp++ = '\0';
    185 	/* THIS STUFF IS INTERNET SPECIFIC */
    186 	d->rpc.r_name = d->line;
    187 	while (*cp == ' ' || *cp == '\t')
    188 		cp++;
    189 	d->rpc.r_number = atoi(cp);
    190 	q = d->rpc.r_aliases = d->rpc_aliases;
    191 	cp = strpbrk(cp, " \t");
    192 	if (cp != NULL)
    193 		*cp++ = '\0';
    194 	while (cp && *cp) {
    195 		if (*cp == ' ' || *cp == '\t') {
    196 			cp++;
    197 			continue;
    198 		}
    199 		if (q < &(d->rpc_aliases[MAXALIASES - 1]))
    200 			*q++ = cp;
    201 		cp = strpbrk(cp, " \t");
    202 		if (cp != NULL)
    203 			*cp++ = '\0';
    204 	}
    205 	*q = NULL;
    206 	return (&d->rpc);
    207 }
    208 
    209