]> git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/getrpcent.c
Libinfo-278.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.3 2002/02/19 20:36:23 epeyton Exp $";
57 #endif
58
59 /*
60 * Copyright (c) 1984 by Sun Microsystems, Inc.
61 */
62
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <sys/types.h>
66 #include <string.h>
67 #include <rpc/rpc.h>
68
69 /*
70 * Internet version.
71 */
72 struct rpcdata {
73 FILE *rpcf;
74 int stayopen;
75 #define MAXALIASES 35
76 char *rpc_aliases[MAXALIASES];
77 struct rpcent rpc;
78 char line[BUFSIZ+1];
79 } *rpcdata;
80
81 static struct rpcent *interpret();
82 struct hostent *gethostent();
83 char *inet_ntoa();
84
85 static char RPCDB[] = "/etc/rpc";
86
87 static struct rpcdata *
88 _rpcdata()
89 {
90 register struct rpcdata *d = rpcdata;
91
92 if (d == 0) {
93 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
94 rpcdata = d;
95 }
96 return (d);
97 }
98
99 struct rpcent *
100 getrpcbynumber(number)
101 #ifdef __LP64__
102 int32_t number;
103 #else
104 register long number;
105 #endif
106 {
107 register struct rpcdata *d = _rpcdata();
108 register struct rpcent *p;
109 #ifdef __LP64__
110 int x;
111
112 x = number;
113 #endif
114
115 if (d == 0)
116 return (0);
117 setrpcent(0);
118 while ((p = getrpcent()))
119 {
120 #ifdef __LP64__
121 if (p->r_number == x) break;
122 #else
123 if (p->r_number == number) break;
124 #endif
125 }
126 endrpcent();
127 return (p);
128 }
129
130 struct rpcent *
131 getrpcbyname(name)
132 #if defined(__APPLE__)
133 const char *name;
134 #else
135 char *name;
136 #endif
137 {
138 struct rpcent *rpc;
139 char **rp;
140
141 setrpcent(0);
142 while ((rpc = getrpcent())) {
143 if (strcmp(rpc->r_name, name) == 0)
144 return (rpc);
145 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
146 if (strcmp(*rp, name) == 0)
147 return (rpc);
148 }
149 }
150 endrpcent();
151 return (NULL);
152 }
153
154 void
155 setrpcent(f)
156 int f;
157 {
158 register struct rpcdata *d = _rpcdata();
159
160 if (d == 0)
161 return;
162 if (d->rpcf == NULL)
163 d->rpcf = fopen(RPCDB, "r");
164 else
165 rewind(d->rpcf);
166 d->stayopen |= f;
167 }
168
169 void
170 endrpcent()
171 {
172 register struct rpcdata *d = _rpcdata();
173
174 if (d == 0)
175 return;
176 if (d->rpcf && !d->stayopen) {
177 fclose(d->rpcf);
178 d->rpcf = NULL;
179 }
180 }
181
182 struct rpcent *
183 getrpcent()
184 {
185 register struct rpcdata *d = _rpcdata();
186
187 if (d == 0)
188 return(NULL);
189 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
190 return (NULL);
191 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
192 return (NULL);
193 return (interpret(d->line, strlen(d->line)));
194 }
195
196 static struct rpcent *
197 interpret(val, len)
198 char *val;
199 u_int len;
200 {
201 register struct rpcdata *d = _rpcdata();
202 char *p;
203 register char *cp, **q;
204
205 if (d == 0)
206 return (0);
207 (void) strncpy(d->line, val, len);
208 p = d->line;
209 d->line[len] = '\n';
210 if (*p == '#')
211 return (getrpcent());
212 cp = strpbrk(p, "#\n");
213 if (cp == NULL)
214 return (getrpcent());
215 *cp = '\0';
216 cp = strpbrk(p, " \t");
217 if (cp == NULL)
218 return (getrpcent());
219 *cp++ = '\0';
220 /* THIS STUFF IS INTERNET SPECIFIC */
221 d->rpc.r_name = d->line;
222 while (*cp == ' ' || *cp == '\t')
223 cp++;
224 d->rpc.r_number = atoi(cp);
225 q = d->rpc.r_aliases = d->rpc_aliases;
226 cp = strpbrk(cp, " \t");
227 if (cp != NULL)
228 *cp++ = '\0';
229 while (cp && *cp) {
230 if (*cp == ' ' || *cp == '\t') {
231 cp++;
232 continue;
233 }
234 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
235 *q++ = cp;
236 cp = strpbrk(cp, " \t");
237 if (cp != NULL)
238 *cp++ = '\0';
239 }
240 *q = NULL;
241 return (&d->rpc);
242 }
243