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