1 /* 2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3 * unrestricted use provided that this legend is included on all tape 4 * media and as a part of the software program in whole or part. Users 5 * may copy or modify Sun RPC without charge, but are not authorized 6 * to license or distribute it to anyone else except as part of a product or 7 * program developed by the user or with the express written consent of 8 * Sun Microsystems, Inc. 9 * 10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 13 * 14 * Sun RPC is provided with no support and without any obligation on the 15 * part of Sun Microsystems, Inc. to assist in its use, correction, 16 * modification or enhancement. 17 * 18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 20 * OR ANY PART THEREOF. 21 * 22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 23 * or profits or other special, indirect and consequential damages, even if 24 * Sun has been advised of the possibility of such damages. 25 * 26 * Sun Microsystems, Inc. 27 * 2550 Garcia Avenue 28 * Mountain View, California 94043 29 */ 30 31 #if defined(LIBC_SCCS) && !defined(lint) 32 static char *rcsid = "$OpenBSD: getrpcent.c,v 1.10 2001/06/27 00:58:56 lebel Exp $"; 33 #endif /* LIBC_SCCS and not lint */ 34 35 /* 36 * Copyright (c) 1984 by Sun Microsystems, Inc. 37 */ 38 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <sys/types.h> 42 #include <string.h> 43 #include <rpc/rpc.h> 44 45 /* 46 * Internet version. 47 */ 48 struct rpcdata { 49 FILE *rpcf; 50 int stayopen; 51 #define MAXALIASES 35 52 char *rpc_aliases[MAXALIASES]; 53 struct rpcent rpc; 54 char line[BUFSIZ+1]; 55 } *rpcdata; 56 57 static struct rpcent *interpret(); 58 struct hostent *gethostent(); 59 char *inet_ntoa(); 60 61 static char RPCDB[] = "/etc/rpc"; 62 63 static struct rpcdata * 64 _rpcdata() 65 { 66 register struct rpcdata *d = rpcdata; 67 68 if (d == NULL) { 69 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata)); 70 rpcdata = d; 71 } 72 return (d); 73 } 74 75 struct rpcent * 76 getrpcbynumber(number) 77 register int number; 78 { 79 register struct rpcdata *d = _rpcdata(); 80 register struct rpcent *p; 81 82 if (d == NULL) 83 return (0); 84 setrpcent(0); 85 while ((p = getrpcent())) { 86 if (p->r_number == number) 87 break; 88 } 89 endrpcent(); 90 return (p); 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 goto done; 104 for (rp = rpc->r_aliases; *rp != NULL; rp++) { 105 if (strcmp(*rp, name) == 0) 106 goto done; 107 } 108 } 109 done: 110 endrpcent(); 111 return (rpc); 112 } 113 114 void 115 setrpcent(f) 116 int f; 117 { 118 register struct rpcdata *d = _rpcdata(); 119 120 if (d == NULL) 121 return; 122 if (d->rpcf == NULL) 123 d->rpcf = fopen(RPCDB, "r"); 124 else 125 rewind(d->rpcf); 126 d->stayopen |= f; 127 } 128 129 void 130 endrpcent() 131 { 132 register struct rpcdata *d = _rpcdata(); 133 134 if (d == NULL) 135 return; 136 if (d->rpcf && !d->stayopen) { 137 fclose(d->rpcf); 138 d->rpcf = NULL; 139 } 140 } 141 142 struct rpcent * 143 getrpcent() 144 { 145 register struct rpcdata *d = _rpcdata(); 146 147 if (d == NULL) 148 return(NULL); 149 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL) 150 return (NULL); 151 /* -1 so there is room to append a \n below */ 152 if (fgets(d->line, BUFSIZ-1, 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 == NULL) 167 return (0); 168 strlcpy(d->line, val, sizeof(d->line)); 169 p = d->line; 170 p[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