1 /* $NetBSD: getrpcent.c,v 1.4 1995/02/25 03:01:45 cgd 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.4 1995/02/25 03:01:45 cgd 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 48 /* 49 * Internet version. 50 */ 51 struct rpcdata { 52 FILE *rpcf; 53 int stayopen; 54 #define MAXALIASES 35 55 char *rpc_aliases[MAXALIASES]; 56 struct rpcent rpc; 57 char line[BUFSIZ+1]; 58 } *rpcdata; 59 60 static struct rpcent *interpret(); 61 struct hostent *gethostent(); 62 char *inet_ntoa(); 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 register struct rpcdata *d = _rpcdata(); 83 register struct rpcent *p; 84 85 if (d == 0) 86 return (0); 87 setrpcent(0); 88 while (p = getrpcent()) { 89 if (p->r_number == number) 90 break; 91 } 92 endrpcent(); 93 return (p); 94 } 95 96 struct rpcent * 97 getrpcbyname(name) 98 char *name; 99 { 100 struct rpcent *rpc; 101 char **rp; 102 103 setrpcent(0); 104 while (rpc = getrpcent()) { 105 if (strcmp(rpc->r_name, name) == 0) 106 return (rpc); 107 for (rp = rpc->r_aliases; *rp != NULL; rp++) { 108 if (strcmp(*rp, name) == 0) 109 return (rpc); 110 } 111 } 112 endrpcent(); 113 return (NULL); 114 } 115 116 void 117 setrpcent(f) 118 int f; 119 { 120 register struct rpcdata *d = _rpcdata(); 121 122 if (d == 0) 123 return; 124 if (d->rpcf == NULL) 125 d->rpcf = fopen(RPCDB, "r"); 126 else 127 rewind(d->rpcf); 128 d->stayopen |= f; 129 } 130 131 void 132 endrpcent() 133 { 134 register struct rpcdata *d = _rpcdata(); 135 136 if (d == 0) 137 return; 138 if (d->rpcf && !d->stayopen) { 139 fclose(d->rpcf); 140 d->rpcf = NULL; 141 } 142 } 143 144 struct rpcent * 145 getrpcent() 146 { 147 struct rpcent *hp; 148 int reason; 149 register struct rpcdata *d = _rpcdata(); 150 151 if (d == 0) 152 return(NULL); 153 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL) 154 return (NULL); 155 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL) 156 return (NULL); 157 return (interpret(d->line, strlen(d->line))); 158 } 159 160 static struct rpcent * 161 interpret(val, len) 162 char *val; 163 int len; 164 { 165 register struct rpcdata *d = _rpcdata(); 166 char *p; 167 register char *cp, **q; 168 169 if (d == 0) 170 return (0); 171 (void) strncpy(d->line, val, len); 172 p = d->line; 173 d->line[len] = '\n'; 174 if (*p == '#') 175 return (getrpcent()); 176 cp = strpbrk(p, "#\n"); 177 if (cp == NULL) 178 return (getrpcent()); 179 *cp = '\0'; 180 cp = strpbrk(p, " \t"); 181 if (cp == NULL) 182 return (getrpcent()); 183 *cp++ = '\0'; 184 /* THIS STUFF IS INTERNET SPECIFIC */ 185 d->rpc.r_name = d->line; 186 while (*cp == ' ' || *cp == '\t') 187 cp++; 188 d->rpc.r_number = atoi(cp); 189 q = d->rpc.r_aliases = d->rpc_aliases; 190 cp = strpbrk(cp, " \t"); 191 if (cp != NULL) 192 *cp++ = '\0'; 193 while (cp && *cp) { 194 if (*cp == ' ' || *cp == '\t') { 195 cp++; 196 continue; 197 } 198 if (q < &(d->rpc_aliases[MAXALIASES - 1])) 199 *q++ = cp; 200 cp = strpbrk(cp, " \t"); 201 if (cp != NULL) 202 *cp++ = '\0'; 203 } 204 *q = NULL; 205 return (&d->rpc); 206 } 207 208