Libinfo-78.tar.gz
[apple/libinfo.git] / rpc.subproj / getrpcent.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user or with the express written consent of
31 * Sun Microsystems, Inc.
32 *
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 *
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
40 *
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
44 *
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
48 *
49 * Sun Microsystems, Inc.
50 * 2550 Garcia Avenue
51 * Mountain View, California 94043
52 */
53
54 #if defined(LIBC_SCCS) && !defined(lint)
55 /*static char *sccsid = "from: @(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";*/
56 static char *rcsid = "$Id: getrpcent.c,v 1.2 1999/10/14 21:56:53 wsanchez Exp $";
57 #endif
58
59 /*
60 * Copyright (c) 1984 by Sun Microsystems, Inc.
61 */
62
63 #include <stdio.h>
64 #include <sys/types.h>
65 #include <string.h>
66 #include <rpc/rpc.h>
67
68 /*
69 * Internet version.
70 */
71 struct rpcdata {
72 FILE *rpcf;
73 int stayopen;
74 #define MAXALIASES 35
75 char *rpc_aliases[MAXALIASES];
76 struct rpcent rpc;
77 char line[BUFSIZ+1];
78 } *rpcdata;
79
80 static struct rpcent *interpret();
81 struct hostent *gethostent();
82 char *inet_ntoa();
83
84 static char RPCDB[] = "/etc/rpc";
85
86 static struct rpcdata *
87 _rpcdata()
88 {
89 register struct rpcdata *d = rpcdata;
90
91 if (d == 0) {
92 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
93 rpcdata = d;
94 }
95 return (d);
96 }
97
98 struct rpcent *
99 getrpcbynumber(number)
100 register long number;
101 {
102 register struct rpcdata *d = _rpcdata();
103 register struct rpcent *p;
104
105 if (d == 0)
106 return (0);
107 setrpcent(0);
108 while (p = getrpcent()) {
109 if (p->r_number == number)
110 break;
111 }
112 endrpcent();
113 return (p);
114 }
115
116 struct rpcent *
117 getrpcbyname(name)
118 #if defined(__APPLE__)
119 const char *name;
120 #else
121 char *name;
122 #endif
123 {
124 struct rpcent *rpc;
125 char **rp;
126
127 setrpcent(0);
128 while (rpc = getrpcent()) {
129 if (strcmp(rpc->r_name, name) == 0)
130 return (rpc);
131 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
132 if (strcmp(*rp, name) == 0)
133 return (rpc);
134 }
135 }
136 endrpcent();
137 return (NULL);
138 }
139
140 void
141 setrpcent(f)
142 int f;
143 {
144 register struct rpcdata *d = _rpcdata();
145
146 if (d == 0)
147 return;
148 if (d->rpcf == NULL)
149 d->rpcf = fopen(RPCDB, "r");
150 else
151 rewind(d->rpcf);
152 d->stayopen |= f;
153 }
154
155 void
156 endrpcent()
157 {
158 register struct rpcdata *d = _rpcdata();
159
160 if (d == 0)
161 return;
162 if (d->rpcf && !d->stayopen) {
163 fclose(d->rpcf);
164 d->rpcf = NULL;
165 }
166 }
167
168 struct rpcent *
169 getrpcent()
170 {
171 struct rpcent *hp;
172 int reason;
173 register struct rpcdata *d = _rpcdata();
174
175 if (d == 0)
176 return(NULL);
177 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
178 return (NULL);
179 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
180 return (NULL);
181 return (interpret(d->line, strlen(d->line)));
182 }
183
184 static struct rpcent *
185 interpret(val, len)
186 char *val;
187 int len;
188 {
189 register struct rpcdata *d = _rpcdata();
190 char *p;
191 register char *cp, **q;
192
193 if (d == 0)
194 return (0);
195 (void) strncpy(d->line, val, len);
196 p = d->line;
197 d->line[len] = '\n';
198 if (*p == '#')
199 return (getrpcent());
200 cp = strpbrk(p, "#\n");
201 if (cp == NULL)
202 return (getrpcent());
203 *cp = '\0';
204 cp = strpbrk(p, " \t");
205 if (cp == NULL)
206 return (getrpcent());
207 *cp++ = '\0';
208 /* THIS STUFF IS INTERNET SPECIFIC */
209 d->rpc.r_name = d->line;
210 while (*cp == ' ' || *cp == '\t')
211 cp++;
212 d->rpc.r_number = atoi(cp);
213 q = d->rpc.r_aliases = d->rpc_aliases;
214 cp = strpbrk(cp, " \t");
215 if (cp != NULL)
216 *cp++ = '\0';
217 while (cp && *cp) {
218 if (*cp == ' ' || *cp == '\t') {
219 cp++;
220 continue;
221 }
222 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
223 *q++ = cp;
224 cp = strpbrk(cp, " \t");
225 if (cp != NULL)
226 *cp++ = '\0';
227 }
228 *q = NULL;
229 return (&d->rpc);
230 }
231