1 /* $NetBSD: getrpcent.c,v 1.23 2013/03/11 20:19:29 tron Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
37 static char *sccsid
= "@(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";
39 __RCSID("$NetBSD: getrpcent.c,v 1.23 2013/03/11 20:19:29 tron Exp $");
44 * Copyright (c) 1984 by Sun Microsystems, Inc.
47 #include "namespace.h"
49 #include <sys/types.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
63 __weak_alias(endrpcent
,_endrpcent
)
64 __weak_alias(getrpcbyname
,_getrpcbyname
)
65 __weak_alias(getrpcbynumber
,_getrpcbynumber
)
66 __weak_alias(getrpcent
,_getrpcent
)
67 __weak_alias(setrpcent
,_setrpcent
)
73 static struct rpcdata
{
77 char *rpc_aliases
[MAXALIASES
];
82 static struct rpcent
*interpret(char *val
, size_t len
);
84 #define RPCDB "/etc/rpc"
86 static struct rpcdata
*_rpcdata(void);
88 static struct rpcdata
*
91 struct rpcdata
*d
= rpcdata
;
94 d
= (struct rpcdata
*)calloc(1, sizeof (struct rpcdata
));
101 getrpcbynumber(int number
)
106 while ((rpc
= getrpcent()) != NULL
) {
107 if (rpc
->r_number
== number
)
115 getrpcbyname(const char *name
)
120 _DIAGASSERT(name
!= NULL
);
123 while ((rpc
= getrpcent()) != NULL
) {
124 if (strcmp(rpc
->r_name
, name
) == 0)
126 for (rp
= rpc
->r_aliases
; *rp
!= NULL
; rp
++) {
127 if (strcmp(*rp
, name
) == 0)
139 struct rpcdata
*d
= _rpcdata();
144 d
->rpcf
= fopen(RPCDB
, "re");
153 struct rpcdata
*d
= _rpcdata();
157 if (d
->rpcf
&& !d
->stayopen
) {
166 struct rpcdata
*d
= _rpcdata();
170 if (d
->rpcf
== NULL
&& (d
->rpcf
= fopen(RPCDB
, "re")) == NULL
)
172 if (fgets(d
->line
, BUFSIZ
, d
->rpcf
) == NULL
)
174 return (interpret(d
->line
, strlen(d
->line
)));
177 static struct rpcent
*
178 interpret(char *val
, size_t len
)
180 struct rpcdata
*d
= _rpcdata();
184 _DIAGASSERT(val
!= NULL
);
188 (void) strncpy(d
->line
, val
, len
);
192 return (getrpcent());
193 cp
= strpbrk(p
, "#\n");
195 return (getrpcent());
197 cp
= strpbrk(p
, " \t");
199 return (getrpcent());
201 /* THIS STUFF IS INTERNET SPECIFIC */
202 d
->rpc
.r_name
= d
->line
;
203 while (*cp
== ' ' || *cp
== '\t')
205 d
->rpc
.r_number
= atoi(cp
);
206 q
= d
->rpc
.r_aliases
= d
->rpc_aliases
;
207 cp
= strpbrk(cp
, " \t");
211 if (*cp
== ' ' || *cp
== '\t') {
215 if (q
< &(d
->rpc_aliases
[MAXALIASES
- 1]))
217 cp
= strpbrk(cp
, " \t");