libutil: add O_NOCTTY back to old pty open code
[minix.git] / lib / libc / rpc / getrpcent.c
blob696893fd51d3488191c3100e4d2a1c4acdda7bd9
1 /* $NetBSD: getrpcent.c,v 1.21 2004/08/16 02:47:54 ginsbach Exp $ */
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user or with the express written consent of
10 * Sun Microsystems, Inc.
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California 94043
33 #include <sys/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 #if 0
36 static char *sccsid = "@(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";
37 #else
38 __RCSID("$NetBSD: getrpcent.c,v 1.21 2004/08/16 02:47:54 ginsbach Exp $");
39 #endif
40 #endif
43 * Copyright (c) 1984 by Sun Microsystems, Inc.
46 #include "namespace.h"
48 #include <sys/types.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
53 #include <assert.h>
54 #include <netdb.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
59 #include <rpc/rpc.h>
61 #ifdef __weak_alias
62 __weak_alias(endrpcent,_endrpcent)
63 __weak_alias(getrpcbyname,_getrpcbyname)
64 __weak_alias(getrpcbynumber,_getrpcbynumber)
65 __weak_alias(getrpcent,_getrpcent)
66 __weak_alias(setrpcent,_setrpcent)
67 #endif
70 * Internet version.
72 static 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;
81 static struct rpcent *interpret(char *val, size_t len);
83 #define RPCDB "/etc/rpc"
85 static struct rpcdata *_rpcdata(void);
87 static struct rpcdata *
88 _rpcdata(void)
90 struct rpcdata *d = rpcdata;
92 if (d == 0) {
93 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
94 rpcdata = d;
96 return (d);
99 struct rpcent *
100 getrpcbynumber(int number)
102 struct rpcent *rpc;
104 setrpcent(0);
105 while ((rpc = getrpcent()) != NULL) {
106 if (rpc->r_number == number)
107 break;
109 endrpcent();
110 return (rpc);
113 struct rpcent *
114 getrpcbyname(const char *name)
116 struct rpcent *rpc;
117 char **rp;
119 _DIAGASSERT(name != NULL);
121 setrpcent(0);
122 while ((rpc = getrpcent()) != NULL) {
123 if (strcmp(rpc->r_name, name) == 0)
124 break;
125 for (rp = rpc->r_aliases; *rp != NULL; rp++) {
126 if (strcmp(*rp, name) == 0)
127 goto found;
130 found:
131 endrpcent();
132 return (rpc);
135 void
136 setrpcent(int f)
138 struct rpcdata *d = _rpcdata();
140 if (d == 0)
141 return;
142 if (d->rpcf == NULL)
143 d->rpcf = fopen(RPCDB, "r");
144 else
145 rewind(d->rpcf);
146 d->stayopen |= f;
149 void
150 endrpcent(void)
152 struct rpcdata *d = _rpcdata();
154 if (d == 0)
155 return;
156 if (d->rpcf && !d->stayopen) {
157 fclose(d->rpcf);
158 d->rpcf = NULL;
162 struct rpcent *
163 getrpcent(void)
165 struct rpcdata *d = _rpcdata();
167 if (d == 0)
168 return(NULL);
169 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
170 return (NULL);
171 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
172 return (NULL);
173 return (interpret(d->line, strlen(d->line)));
176 static struct rpcent *
177 interpret(char *val, size_t len)
179 struct rpcdata *d = _rpcdata();
180 char *p;
181 char *cp, **q;
183 _DIAGASSERT(val != NULL);
185 if (d == 0)
186 return (0);
187 (void) strncpy(d->line, val, len);
188 p = d->line;
189 d->line[len] = '\n';
190 if (*p == '#')
191 return (getrpcent());
192 cp = strpbrk(p, "#\n");
193 if (cp == NULL)
194 return (getrpcent());
195 *cp = '\0';
196 cp = strpbrk(p, " \t");
197 if (cp == NULL)
198 return (getrpcent());
199 *cp++ = '\0';
200 /* THIS STUFF IS INTERNET SPECIFIC */
201 d->rpc.r_name = d->line;
202 while (*cp == ' ' || *cp == '\t')
203 cp++;
204 d->rpc.r_number = atoi(cp);
205 q = d->rpc.r_aliases = d->rpc_aliases;
206 cp = strpbrk(cp, " \t");
207 if (cp != NULL)
208 *cp++ = '\0';
209 while (cp && *cp) {
210 if (*cp == ' ' || *cp == '\t') {
211 cp++;
212 continue;
214 if (q < &(d->rpc_aliases[MAXALIASES - 1]))
215 *q++ = cp;
216 cp = strpbrk(cp, " \t");
217 if (cp != NULL)
218 *cp++ = '\0';
220 *q = NULL;
221 return (&d->rpc);