Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / getnetgrent.c
blobf0976969647c017f8f3a1611970b91ef02850a7d
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1996-1999, 2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or 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 WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid[] = "Id: getnetgrent.c,v 1.6 2008/11/14 02:36:51 marka Exp";
22 #endif /* LIBC_SCCS and not lint */
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>
39 #include <irs.h>
41 #include "port_after.h"
43 #include "irs_data.h"
45 /* Forward */
47 static struct net_data *init(void);
50 /* Public */
52 #ifndef SETNETGRENT_ARGS
53 #define SETNETGRENT_ARGS const char *netgroup
54 #endif
55 void
56 setnetgrent(SETNETGRENT_ARGS) {
57 struct net_data *net_data = init();
59 setnetgrent_p(netgroup, net_data);
62 void
63 endnetgrent(void) {
64 struct net_data *net_data = init();
66 endnetgrent_p(net_data);
69 #ifndef INNETGR_ARGS
70 #define INNETGR_ARGS const char *netgroup, const char *host, \
71 const char *user, const char *domain
72 #endif
73 int
74 innetgr(INNETGR_ARGS) {
75 struct net_data *net_data = init();
77 return (innetgr_p(netgroup, host, user, domain, net_data));
80 int
81 getnetgrent(NGR_R_CONST char **host, NGR_R_CONST char **user,
82 NGR_R_CONST char **domain)
84 struct net_data *net_data = init();
85 const char *ch, *cu, *cd;
86 int ret;
88 ret = getnetgrent_p(&ch, &cu, &cd, net_data);
89 if (ret != 1)
90 return (ret);
92 DE_CONST(ch, *host);
93 DE_CONST(cu, *user);
94 DE_CONST(cd, *domain);
95 return (ret);
98 /* Shared private. */
100 void
101 setnetgrent_p(const char *netgroup, struct net_data *net_data) {
102 struct irs_ng *ng;
104 if ((net_data != NULL) && ((ng = net_data->ng) != NULL))
105 (*ng->rewind)(ng, netgroup);
108 void
109 endnetgrent_p(struct net_data *net_data) {
110 struct irs_ng *ng;
112 if (!net_data)
113 return;
114 if ((ng = net_data->ng) != NULL)
115 (*ng->close)(ng);
116 net_data->ng = NULL;
120 innetgr_p(const char *netgroup, const char *host,
121 const char *user, const char *domain,
122 struct net_data *net_data) {
123 struct irs_ng *ng;
125 if (!net_data || !(ng = net_data->ng))
126 return (0);
127 return ((*ng->test)(ng, netgroup, host, user, domain));
131 getnetgrent_p(const char **host, const char **user, const char **domain,
132 struct net_data *net_data ) {
133 struct irs_ng *ng;
135 if (!net_data || !(ng = net_data->ng))
136 return (0);
137 return ((*ng->next)(ng, host, user, domain));
140 /* Private */
142 static struct net_data *
143 init(void) {
144 struct net_data *net_data;
146 if (!(net_data = net_data_init(NULL)))
147 goto error;
148 if (!net_data->ng) {
149 net_data->ng = (*net_data->irs->ng_map)(net_data->irs);
150 if (!net_data->ng) {
151 error:
152 errno = EIO;
153 return (NULL);
157 return (net_data);
160 #endif /*__BIND_NOSTATIC*/
162 /*! \file */