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