Home | History | Annotate | Line # | Download | only in rpc
getrpcent.c revision 1.11
      1 /*	$NetBSD: getrpcent.c,v 1.11 1998/02/13 05:52:22 lukem 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.11 1998/02/13 05:52:22 lukem Exp $");
     39 #endif
     40 #endif
     41 
     42 /*
     43  * Copyright (c) 1984 by Sun Microsystems, Inc.
     44  */
     45 
     46 #include "namespace.h"
     47 
     48 #include <sys/types.h>
     49 
     50 #include <netinet/in.h>
     51 #include <arpa/inet.h>
     52 
     53 #include <netdb.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 
     58 #include <rpc/rpc.h>
     59 
     60 #ifdef __weak_alias
     61 __weak_alias(endrpcent,_endrpcent);
     62 __weak_alias(getrpcbyname,_getrpcbyname);
     63 __weak_alias(getrpcbynumber,_getrpcbynumber);
     64 __weak_alias(getrpcent,_getrpcent);
     65 __weak_alias(setrpcent,_setrpcent);
     66 #endif
     67 
     68 /*
     69  * Internet version.
     70  */
     71 struct rpcdata {
     72 	FILE	*rpcf;
     73 	int	stayopen;
     74 #define	MAXALIASES	35
     75 	char	*rpc_aliases[MAXALIASES];
     76 	struct	rpcent rpc;
     77 	char	line[BUFSIZ+1];
     78 } *rpcdata;
     79 
     80 static	struct rpcent *interpret __P((char *val, int len));
     81 
     82 static char RPCDB[] = "/etc/rpc";
     83 
     84 static struct rpcdata *_rpcdata __P((void));
     85 
     86 static struct rpcdata *
     87 _rpcdata()
     88 {
     89 	struct rpcdata *d = rpcdata;
     90 
     91 	if (d == 0) {
     92 		d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
     93 		rpcdata = d;
     94 	}
     95 	return (d);
     96 }
     97 
     98 struct rpcent *
     99 getrpcbynumber(number)
    100 	int number;
    101 {
    102 	struct rpcent *rpc;
    103 
    104 	setrpcent(0);
    105 	while ((rpc = getrpcent()) != NULL) {
    106 		if (rpc->r_number == number)
    107 			break;
    108 	}
    109 	endrpcent();
    110 	return (rpc);
    111 }
    112 
    113 struct rpcent *
    114 getrpcbyname(name)
    115 	char *name;
    116 {
    117 	struct rpcent *rpc;
    118 	char **rp;
    119 
    120 	setrpcent(0);
    121 	while ((rpc = getrpcent()) != NULL) {
    122 		if (strcmp(rpc->r_name, name) == 0)
    123 			break;
    124 		for (rp = rpc->r_aliases; *rp != NULL; rp++) {
    125 			if (strcmp(*rp, name) == 0)
    126 				break;
    127 		}
    128 	}
    129 	endrpcent();
    130 	return (rpc);
    131 }
    132 
    133 void
    134 setrpcent(f)
    135 	int f;
    136 {
    137 	struct rpcdata *d = _rpcdata();
    138 
    139 	if (d == 0)
    140 		return;
    141 	if (d->rpcf == NULL)
    142 		d->rpcf = fopen(RPCDB, "r");
    143 	else
    144 		rewind(d->rpcf);
    145 	d->stayopen |= f;
    146 }
    147 
    148 void
    149 endrpcent()
    150 {
    151 	struct rpcdata *d = _rpcdata();
    152 
    153 	if (d == 0)
    154 		return;
    155 	if (d->rpcf && !d->stayopen) {
    156 		fclose(d->rpcf);
    157 		d->rpcf = NULL;
    158 	}
    159 }
    160 
    161 struct rpcent *
    162 getrpcent()
    163 {
    164 	struct rpcdata *d = _rpcdata();
    165 
    166 	if (d == 0)
    167 		return(NULL);
    168 	if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
    169 		return (NULL);
    170         if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
    171 		return (NULL);
    172 	return (interpret(d->line, strlen(d->line)));
    173 }
    174 
    175 static struct rpcent *
    176 interpret(val, len)
    177 	char *val;
    178 	int len;
    179 {
    180 	struct rpcdata *d = _rpcdata();
    181 	char *p;
    182 	char *cp, **q;
    183 
    184 	if (d == 0)
    185 		return (0);
    186 	(void) strncpy(d->line, val, len);
    187 	p = d->line;
    188 	d->line[len] = '\n';
    189 	if (*p == '#')
    190 		return (getrpcent());
    191 	cp = strpbrk(p, "#\n");
    192 	if (cp == NULL)
    193 		return (getrpcent());
    194 	*cp = '\0';
    195 	cp = strpbrk(p, " \t");
    196 	if (cp == NULL)
    197 		return (getrpcent());
    198 	*cp++ = '\0';
    199 	/* THIS STUFF IS INTERNET SPECIFIC */
    200 	d->rpc.r_name = d->line;
    201 	while (*cp == ' ' || *cp == '\t')
    202 		cp++;
    203 	d->rpc.r_number = atoi(cp);
    204 	q = d->rpc.r_aliases = d->rpc_aliases;
    205 	cp = strpbrk(cp, " \t");
    206 	if (cp != NULL)
    207 		*cp++ = '\0';
    208 	while (cp && *cp) {
    209 		if (*cp == ' ' || *cp == '\t') {
    210 			cp++;
    211 			continue;
    212 		}
    213 		if (q < &(d->rpc_aliases[MAXALIASES - 1]))
    214 			*q++ = cp;
    215 		cp = strpbrk(cp, " \t");
    216 		if (cp != NULL)
    217 			*cp++ = '\0';
    218 	}
    219 	*q = NULL;
    220 	return (&d->rpc);
    221 }
    222