Check-in of original version of ONCRPC library (and headers) from
[shibboleth/sp.git] / oncrpc / getrpcen.c
1 /*********************************************************************
2  * RPC for the Windows NT Operating System
3  * 1993 by Martin F. Gergeleit
4  * Users may use, copy or modify Sun RPC for the Windows NT Operating 
5  * System according to the Sun copyright below.
6  *
7  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO 
8  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE 
9  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10  *********************************************************************/
11
12 /* @(#)getrpcent.c      2.2 88/07/29 4.0 RPCSRC */
13 #if !defined(lint) && defined(SCCSIDS)
14 static  char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11  Copyr 1984 Sun Micro";
15 #endif
16
17 /*
18  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
19  * unrestricted use provided that this legend is included on all tape
20  * media and as a part of the software program in whole or part.  Users
21  * may copy or modify Sun RPC without charge, but are not authorized
22  * to license or distribute it to anyone else except as part of a product or
23  * program developed by the user.
24  * 
25  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
26  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
28  * 
29  * Sun RPC is provided with no support and without any obligation on the
30  * part of Sun Microsystems, Inc. to assist in its use, correction,
31  * modification or enhancement.
32  * 
33  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
34  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
35  * OR ANY PART THEREOF.
36  * 
37  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
38  * or profits or other special, indirect and consequential damages, even if
39  * Sun has been advised of the possibility of such damages.
40  * 
41  * Sun Microsystems, Inc.
42  * 2550 Garcia Avenue
43  * Mountain View, California  94043
44  */
45
46 /*
47  * Copyright (c) 1985 by Sun Microsystems, Inc.
48  */
49
50 #include <stdio.h>
51 #include <sys/types.h>
52 #include <rpc/rpc.h>
53 #ifndef WIN32
54 #include <netdb.h>
55 #include <sys/socket.h>
56 #endif
57
58 /*
59  * Internet version.
60  */
61 struct rpcdata {
62         FILE    *rpcf;
63         char    *current;
64         int     currentlen;
65         int     stayopen;
66 #define MAXALIASES      35
67         char    *rpc_aliases[MAXALIASES];
68         struct  rpcent rpc;
69         char    line[BUFSIZ+1];
70         char    *domain;
71 } *rpcdata, *_rpcdata();
72
73 static  struct rpcent *interpret();
74 struct  hostent *gethostent();
75 #ifdef WIN32
76 #define index(str,ch) strchr(str,ch)
77 #else
78 char    *inet_ntoa();
79 static  char *index();
80 #endif
81
82 #ifdef WIN32
83 static char RPCDB[1024];
84 #else
85 static char RPCDB[] = "/etc/rpc";
86 #endif
87
88 static struct rpcdata *
89 _rpcdata()
90 {
91         register struct rpcdata *d = rpcdata;
92
93 #ifdef WIN32
94         char *str;
95
96         if ((RPCDB[0] == '\0') && (str = getenv("SystemRoot"))) {
97                 strcpy(RPCDB, str);
98                 strcat(RPCDB, "\\system32\\drivers\\etc\\rpc");
99         }
100 #endif
101         
102         if (d == 0) {
103                 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
104                 rpcdata = d;
105         }
106         return (d);
107 }
108
109 struct rpcent *
110 getrpcbynumber(number)
111         register int number;
112 {
113         register struct rpcdata *d = _rpcdata();
114         register struct rpcent *p;
115         int reason;
116         char adrstr[16], *val = NULL;
117         int vallen;
118
119         if (d == 0)
120                 return (0);
121         setrpcent(0);
122         while (p = getrpcent()) {
123                 if (p->r_number == number)
124                         break;
125         }
126         endrpcent();
127         return (p);
128 }
129
130 struct rpcent *
131 getrpcbyname(name)
132         char *name;
133 {
134         struct rpcent *rpc;
135         char **rp;
136
137         setrpcent(0);
138         while(rpc = getrpcent()) {
139                 if (strcmp(rpc->r_name, name) == 0)
140                         return (rpc);
141                 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
142                         if (strcmp(*rp, name) == 0)
143                                 return (rpc);
144                 }
145         }
146         endrpcent();
147         return (NULL);
148 }
149
150 setrpcent(f)
151         int f;
152 {
153         register struct rpcdata *d = _rpcdata();
154
155         if (d == 0)
156                 return;
157         if (d->rpcf == NULL)
158                 d->rpcf = fopen(RPCDB, "r");
159         else
160                 rewind(d->rpcf);
161         if (d->current)
162                 free(d->current);
163         d->current = NULL;
164         d->stayopen |= f;
165 }
166
167 endrpcent()
168 {
169         register struct rpcdata *d = _rpcdata();
170
171         if (d == 0)
172                 return;
173         if (d->current && !d->stayopen) {
174                 free(d->current);
175                 d->current = NULL;
176         }
177         if (d->rpcf && !d->stayopen) {
178                 fclose(d->rpcf);
179                 d->rpcf = NULL;
180         }
181 }
182
183 struct rpcent *
184 getrpcent()
185 {
186         struct rpcent *hp;
187         int reason;
188         char *key = NULL, *val = NULL;
189         int keylen, vallen;
190         register struct rpcdata *d = _rpcdata();
191
192         if (d == 0)
193                 return(NULL);
194         if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
195                 return (NULL);
196     if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
197                 return (NULL);
198         return interpret(d->line, strlen(d->line));
199 }
200
201 static struct rpcent *
202 interpret(val, len)
203         char *val;
204 {
205         register struct rpcdata *d = _rpcdata();
206         char *p;
207         register char *cp, **q;
208
209         if (d == 0)
210                 return;
211         strncpy(d->line, val, len);
212         p = d->line;
213         d->line[len] = '\n';
214         if (*p == '#')
215                 return (getrpcent());
216         cp = index(p, '#');
217         if (cp == NULL)
218     {
219                 cp = index(p, '\n');
220                 if (cp == NULL)
221                         return (getrpcent());
222         }
223         *cp = '\0';
224         cp = index(p, ' ');
225         if (cp == NULL)
226     {
227                 cp = index(p, '\t');
228                 if (cp == NULL)
229                         return (getrpcent());
230         }
231         *cp++ = '\0';
232         /* THIS STUFF IS INTERNET SPECIFIC */
233         d->rpc.r_name = d->line;
234         while (*cp == ' ' || *cp == '\t')
235                 cp++;
236         d->rpc.r_number = atoi(cp);
237         q = d->rpc.r_aliases = d->rpc_aliases;
238         cp = index(p, ' ');
239         if (cp != NULL)
240                 *cp++ = '\0';
241         else
242     {
243                 cp = index(p, '\t');
244                 if (cp != NULL)
245                         *cp++ = '\0';
246         }
247         while (cp && *cp) {
248                 if (*cp == ' ' || *cp == '\t') {
249                         cp++;
250                         continue;
251                 }
252                 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
253                         *q++ = cp;
254                 cp = index(p, ' ');
255                 if (cp != NULL)
256                         *cp++ = '\0';
257                 else
258             {
259                         cp = index(p, '\t');
260                         if (cp != NULL)
261                                 *cp++ = '\0';
262                 }
263         }
264         *q = NULL;
265         return (&d->rpc);
266 }