Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / getgrent.c
blob6bfc5e42378f60efb246602b67fc27baa8493da6
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: getgrent.c,v 1.5 2005/04/27 04:56:24 sra Exp";
22 #endif
24 /* Imports */
26 #include "port_before.h"
28 #if !defined(WANT_IRS_GR) || defined(__BIND_NOSTATIC)
29 static int __bind_irs_gr_unneeded;
30 #else
32 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
37 #include <errno.h>
38 #include <grp.h>
39 #include <resolv.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include <irs.h>
46 #include "port_after.h"
48 #include "irs_data.h"
50 /* Forward */
52 static struct net_data *init(void);
53 void endgrent(void);
55 /* Public */
57 struct group *
58 getgrent() {
59 struct net_data *net_data = init();
61 return (getgrent_p(net_data));
64 struct group *
65 getgrnam(const char *name) {
66 struct net_data *net_data = init();
68 return (getgrnam_p(name, net_data));
71 struct group *
72 getgrgid(gid_t gid) {
73 struct net_data *net_data = init();
75 return (getgrgid_p(gid, net_data));
78 int
79 setgroupent(int stayopen) {
80 struct net_data *net_data = init();
82 return (setgroupent_p(stayopen, net_data));
85 #ifdef SETGRENT_VOID
86 void
87 setgrent(void) {
88 struct net_data *net_data = init();
90 setgrent_p(net_data);
92 #else
93 int
94 setgrent(void) {
95 struct net_data *net_data = init();
97 return (setgrent_p(net_data));
99 #endif /* SETGRENT_VOID */
101 void
102 endgrent() {
103 struct net_data *net_data = init();
105 endgrent_p(net_data);
109 getgrouplist(GETGROUPLIST_ARGS) {
110 struct net_data *net_data = init();
112 return (getgrouplist_p(name, basegid, groups, ngroups, net_data));
115 /* Shared private. */
117 struct group *
118 getgrent_p(struct net_data *net_data) {
119 struct irs_gr *gr;
121 if (!net_data || !(gr = net_data->gr))
122 return (NULL);
123 net_data->gr_last = (*gr->next)(gr);
124 return (net_data->gr_last);
127 struct group *
128 getgrnam_p(const char *name, struct net_data *net_data) {
129 struct irs_gr *gr;
131 if (!net_data || !(gr = net_data->gr))
132 return (NULL);
133 if (net_data->gr_stayopen && net_data->gr_last &&
134 !strcmp(net_data->gr_last->gr_name, name))
135 return (net_data->gr_last);
136 net_data->gr_last = (*gr->byname)(gr, name);
137 if (!net_data->gr_stayopen)
138 endgrent();
139 return (net_data->gr_last);
142 struct group *
143 getgrgid_p(gid_t gid, struct net_data *net_data) {
144 struct irs_gr *gr;
146 if (!net_data || !(gr = net_data->gr))
147 return (NULL);
148 if (net_data->gr_stayopen && net_data->gr_last &&
149 (gid_t)net_data->gr_last->gr_gid == gid)
150 return (net_data->gr_last);
151 net_data->gr_last = (*gr->bygid)(gr, gid);
152 if (!net_data->gr_stayopen)
153 endgrent();
154 return (net_data->gr_last);
158 setgroupent_p(int stayopen, struct net_data *net_data) {
159 struct irs_gr *gr;
161 if (!net_data || !(gr = net_data->gr))
162 return (0);
163 (*gr->rewind)(gr);
164 net_data->gr_stayopen = (stayopen != 0);
165 if (stayopen == 0)
166 net_data_minimize(net_data);
167 return (1);
170 #ifdef SETGRENT_VOID
171 void
172 setgrent_p(struct net_data *net_data) {
173 (void)setgroupent_p(0, net_data);
175 #else
177 setgrent_p(struct net_data *net_data) {
178 return (setgroupent_p(0, net_data));
180 #endif /* SETGRENT_VOID */
182 void
183 endgrent_p(struct net_data *net_data) {
184 struct irs_gr *gr;
186 if ((net_data != NULL) && ((gr = net_data->gr) != NULL))
187 (*gr->minimize)(gr);
191 getgrouplist_p(const char *name, gid_t basegid, gid_t *groups, int *ngroups,
192 struct net_data *net_data) {
193 struct irs_gr *gr;
195 if (!net_data || !(gr = net_data->gr)) {
196 *ngroups = 0;
197 return (-1);
199 return ((*gr->list)(gr, name, basegid, groups, ngroups));
202 /* Private */
204 static struct net_data *
205 init() {
206 struct net_data *net_data;
208 if (!(net_data = net_data_init(NULL)))
209 goto error;
210 if (!net_data->gr) {
211 net_data->gr = (*net_data->irs->gr_map)(net_data->irs);
213 if (!net_data->gr || !net_data->res) {
214 error:
215 errno = EIO;
216 return (NULL);
218 (*net_data->gr->res_set)(net_data->gr, net_data->res,
219 NULL);
222 return (net_data);
225 #endif /* WANT_IRS_GR */
226 /*! \file */