Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / getprotoent.c
blob4dfc9db6e0728811c96b22d4874b00d7fa97f749
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if !defined(LINT) && !defined(CODECENTER)
21 static const char rcsid[] = "Id: getprotoent.c,v 1.4 2005/04/27 04:56:26 sra Exp";
22 #endif
24 /* Imports */
26 #include "port_before.h"
28 #if !defined(__BIND_NOSTATIC)
30 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <arpa/nameser.h>
35 #include <errno.h>
36 #include <resolv.h>
37 #include <stdio.h>
38 #include <string.h>
40 #include <irs.h>
42 #include "port_after.h"
44 #include "irs_data.h"
46 /* Forward */
48 static struct net_data *init(void);
50 /* Public */
52 struct protoent *
53 getprotoent() {
54 struct net_data *net_data = init();
56 return (getprotoent_p(net_data));
59 struct protoent *
60 getprotobyname(const char *name) {
61 struct net_data *net_data = init();
63 return (getprotobyname_p(name, net_data));
66 struct protoent *
67 getprotobynumber(int proto) {
68 struct net_data *net_data = init();
70 return (getprotobynumber_p(proto, net_data));
73 void
74 setprotoent(int stayopen) {
75 struct net_data *net_data = init();
77 setprotoent_p(stayopen, net_data);
80 void
81 endprotoent() {
82 struct net_data *net_data = init();
84 endprotoent_p(net_data);
87 /* Shared private. */
89 struct protoent *
90 getprotoent_p(struct net_data *net_data) {
91 struct irs_pr *pr;
93 if (!net_data || !(pr = net_data->pr))
94 return (NULL);
95 net_data->pr_last = (*pr->next)(pr);
96 return (net_data->pr_last);
99 struct protoent *
100 getprotobyname_p(const char *name, struct net_data *net_data) {
101 struct irs_pr *pr;
102 char **pap;
104 if (!net_data || !(pr = net_data->pr))
105 return (NULL);
106 if (net_data->pr_stayopen && net_data->pr_last) {
107 if (!strcmp(net_data->pr_last->p_name, name))
108 return (net_data->pr_last);
109 for (pap = net_data->pr_last->p_aliases; pap && *pap; pap++)
110 if (!strcmp(name, *pap))
111 return (net_data->pr_last);
113 net_data->pr_last = (*pr->byname)(pr, name);
114 if (!net_data->pr_stayopen)
115 endprotoent();
116 return (net_data->pr_last);
119 struct protoent *
120 getprotobynumber_p(int proto, struct net_data *net_data) {
121 struct irs_pr *pr;
123 if (!net_data || !(pr = net_data->pr))
124 return (NULL);
125 if (net_data->pr_stayopen && net_data->pr_last)
126 if (net_data->pr_last->p_proto == proto)
127 return (net_data->pr_last);
128 net_data->pr_last = (*pr->bynumber)(pr, proto);
129 if (!net_data->pr_stayopen)
130 endprotoent();
131 return (net_data->pr_last);
134 void
135 setprotoent_p(int stayopen, struct net_data *net_data) {
136 struct irs_pr *pr;
138 if (!net_data || !(pr = net_data->pr))
139 return;
140 (*pr->rewind)(pr);
141 net_data->pr_stayopen = (stayopen != 0);
142 if (stayopen == 0)
143 net_data_minimize(net_data);
146 void
147 endprotoent_p(struct net_data *net_data) {
148 struct irs_pr *pr;
150 if ((net_data != NULL) && ((pr = net_data->pr) != NULL))
151 (*pr->minimize)(pr);
154 /* Private */
156 static struct net_data *
157 init() {
158 struct net_data *net_data;
160 if (!(net_data = net_data_init(NULL)))
161 goto error;
162 if (!net_data->pr) {
163 net_data->pr = (*net_data->irs->pr_map)(net_data->irs);
165 if (!net_data->pr || !net_data->res) {
166 error:
167 errno = EIO;
168 return (NULL);
170 (*net_data->pr->res_set)(net_data->pr, net_data->res, NULL);
173 return (net_data);
176 #endif /*__BIND_NOSTATIC*/
178 /*! \file */