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