Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / getservent.c
blobd7688eed1d1a9bb28191de362ce2082752d27d35
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: getservent.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 servent *
53 getservent(void) {
54 struct net_data *net_data = init();
56 return (getservent_p(net_data));
59 struct servent *
60 getservbyname(const char *name, const char *proto) {
61 struct net_data *net_data = init();
63 return (getservbyname_p(name, proto, net_data));
66 struct servent *
67 getservbyport(int port, const char *proto) {
68 struct net_data *net_data = init();
70 return (getservbyport_p(port, proto, net_data));
73 void
74 setservent(int stayopen) {
75 struct net_data *net_data = init();
77 setservent_p(stayopen, net_data);
80 void
81 endservent() {
82 struct net_data *net_data = init();
84 endservent_p(net_data);
87 /* Shared private. */
89 struct servent *
90 getservent_p(struct net_data *net_data) {
91 struct irs_sv *sv;
93 if (!net_data || !(sv = net_data->sv))
94 return (NULL);
95 net_data->sv_last = (*sv->next)(sv);
96 return (net_data->sv_last);
99 struct servent *
100 getservbyname_p(const char *name, const char *proto,
101 struct net_data *net_data) {
102 struct irs_sv *sv;
103 char **sap;
105 if (!net_data || !(sv = net_data->sv))
106 return (NULL);
107 if (net_data->sv_stayopen && net_data->sv_last)
108 if (!proto || !strcmp(net_data->sv_last->s_proto, proto)) {
109 if (!strcmp(net_data->sv_last->s_name, name))
110 return (net_data->sv_last);
111 for (sap = net_data->sv_last->s_aliases;
112 sap && *sap; sap++)
113 if (!strcmp(name, *sap))
114 return (net_data->sv_last);
116 net_data->sv_last = (*sv->byname)(sv, name, proto);
117 if (!net_data->sv_stayopen)
118 endservent();
119 return (net_data->sv_last);
122 struct servent *
123 getservbyport_p(int port, const char *proto, struct net_data *net_data) {
124 struct irs_sv *sv;
126 if (!net_data || !(sv = net_data->sv))
127 return (NULL);
128 if (net_data->sv_stayopen && net_data->sv_last)
129 if (port == net_data->sv_last->s_port &&
130 ( !proto ||
131 !strcmp(net_data->sv_last->s_proto, proto)))
132 return (net_data->sv_last);
133 net_data->sv_last = (*sv->byport)(sv, port, proto);
134 return (net_data->sv_last);
137 void
138 setservent_p(int stayopen, struct net_data *net_data) {
139 struct irs_sv *sv;
141 if (!net_data || !(sv = net_data->sv))
142 return;
143 (*sv->rewind)(sv);
144 net_data->sv_stayopen = (stayopen != 0);
145 if (stayopen == 0)
146 net_data_minimize(net_data);
149 void
150 endservent_p(struct net_data *net_data) {
151 struct irs_sv *sv;
153 if ((net_data != NULL) && ((sv = net_data->sv) != NULL))
154 (*sv->minimize)(sv);
157 /* Private */
159 static struct net_data *
160 init() {
161 struct net_data *net_data;
163 if (!(net_data = net_data_init(NULL)))
164 goto error;
165 if (!net_data->sv) {
166 net_data->sv = (*net_data->irs->sv_map)(net_data->irs);
168 if (!net_data->sv || !net_data->res) {
169 error:
170 errno = EIO;
171 return (NULL);
173 (*net_data->sv->res_set)(net_data->sv, net_data->res, NULL);
176 return (net_data);
179 #endif /*__BIND_NOSTATIC*/
181 /*! \file */