1 /* $OpenBSD: getrpcent.c,v 1.15 2010/09/01 14:43:34 millert 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.
36 #include <sys/types.h>
47 char *rpc_aliases
[MAXALIASES
];
52 static struct rpcent
*interpret(char *val
, int len
);
54 static char RPCDB
[] = "/etc/rpc";
56 static struct rpcdata
*
59 struct rpcdata
*d
= rpcdata
;
62 d
= (struct rpcdata
*)calloc(1, sizeof (struct rpcdata
));
69 getrpcbynumber(int number
)
71 struct rpcdata
*d
= _rpcdata();
77 while ((p
= getrpcent())) {
78 if (p
->r_number
== number
)
86 getrpcbyname(char *name
)
92 while ((rpc
= getrpcent())) {
93 if (strcmp(rpc
->r_name
, name
) == 0)
95 for (rp
= rpc
->r_aliases
; *rp
!= NULL
; rp
++) {
96 if (strcmp(*rp
, name
) == 0)
108 struct rpcdata
*d
= _rpcdata();
113 d
->rpcf
= fopen(RPCDB
, "r");
122 struct rpcdata
*d
= _rpcdata();
126 if (d
->rpcf
&& !d
->stayopen
) {
135 struct rpcdata
*d
= _rpcdata();
139 if (d
->rpcf
== NULL
&& (d
->rpcf
= fopen(RPCDB
, "r")) == NULL
)
141 /* -1 so there is room to append a \n below */
142 if (fgets(d
->line
, sizeof(d
->line
) - 1, d
->rpcf
) == NULL
)
144 return (interpret(d
->line
, strlen(d
->line
)));
147 static struct rpcent
*
148 interpret(char *val
, int len
)
150 struct rpcdata
*d
= _rpcdata();
156 strlcpy(d
->line
, val
, sizeof(d
->line
));
160 return (getrpcent());
161 cp
= strpbrk(p
, "#\n");
163 return (getrpcent());
165 cp
= strpbrk(p
, " \t");
167 return (getrpcent());
169 /* THIS STUFF IS INTERNET SPECIFIC */
170 d
->rpc
.r_name
= d
->line
;
171 while (*cp
== ' ' || *cp
== '\t')
173 d
->rpc
.r_number
= atoi(cp
);
174 q
= d
->rpc
.r_aliases
= d
->rpc_aliases
;
175 cp
= strpbrk(cp
, " \t");
179 if (*cp
== ' ' || *cp
== '\t') {
183 if (q
< &(d
->rpc_aliases
[MAXALIASES
- 1]))
185 cp
= strpbrk(cp
, " \t");