No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / getpwent.c
blobaf514f540acaf2aac7578ce1e8aa324d25b033ed
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: getpwent.c,v 1.3 2005/04/27 04:56:26 sra Exp";
22 #endif
24 /* Imports */
26 #include "port_before.h"
28 #if !defined(WANT_IRS_PW) || defined(__BIND_NOSTATIC)
29 static int __bind_irs_pw_unneeded;
30 #else
32 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
37 #include <errno.h>
38 #include <pwd.h>
39 #include <resolv.h>
40 #include <stdio.h>
41 #include <string.h>
43 #include <irs.h>
45 #include "port_after.h"
47 #include "irs_data.h"
49 /* Forward */
51 static struct net_data * init(void);
53 /* Public */
55 struct passwd *
56 getpwent(void) {
57 struct net_data *net_data = init();
59 return (getpwent_p(net_data));
62 struct passwd *
63 getpwnam(const char *name) {
64 struct net_data *net_data = init();
66 return (getpwnam_p(name, net_data));
69 struct passwd *
70 getpwuid(uid_t uid) {
71 struct net_data *net_data = init();
73 return (getpwuid_p(uid, net_data));
76 int
77 setpassent(int stayopen) {
78 struct net_data *net_data = init();
80 return (setpassent_p(stayopen, net_data));
83 #ifdef SETPWENT_VOID
84 void
85 setpwent() {
86 struct net_data *net_data = init();
88 setpwent_p(net_data);
90 #else
91 int
92 setpwent() {
93 struct net_data *net_data = init();
95 return (setpwent_p(net_data));
97 #endif
99 void
100 endpwent() {
101 struct net_data *net_data = init();
103 endpwent_p(net_data);
106 /* Shared private. */
108 struct passwd *
109 getpwent_p(struct net_data *net_data) {
110 struct irs_pw *pw;
112 if (!net_data || !(pw = net_data->pw))
113 return (NULL);
114 net_data->pw_last = (*pw->next)(pw);
115 return (net_data->pw_last);
118 struct passwd *
119 getpwnam_p(const char *name, struct net_data *net_data) {
120 struct irs_pw *pw;
122 if (!net_data || !(pw = net_data->pw))
123 return (NULL);
124 if (net_data->pw_stayopen && net_data->pw_last &&
125 !strcmp(net_data->pw_last->pw_name, name))
126 return (net_data->pw_last);
127 net_data->pw_last = (*pw->byname)(pw, name);
128 if (!net_data->pw_stayopen)
129 endpwent();
130 return (net_data->pw_last);
133 struct passwd *
134 getpwuid_p(uid_t uid, struct net_data *net_data) {
135 struct irs_pw *pw;
137 if (!net_data || !(pw = net_data->pw))
138 return (NULL);
139 if (net_data->pw_stayopen && net_data->pw_last &&
140 net_data->pw_last->pw_uid == uid)
141 return (net_data->pw_last);
142 net_data->pw_last = (*pw->byuid)(pw, uid);
143 if (!net_data->pw_stayopen)
144 endpwent();
145 return (net_data->pw_last);
149 setpassent_p(int stayopen, struct net_data *net_data) {
150 struct irs_pw *pw;
152 if (!net_data || !(pw = net_data->pw))
153 return (0);
154 (*pw->rewind)(pw);
155 net_data->pw_stayopen = (stayopen != 0);
156 if (stayopen == 0)
157 net_data_minimize(net_data);
158 return (1);
161 #ifdef SETPWENT_VOID
162 void
163 setpwent_p(struct net_data *net_data) {
164 (void) setpassent_p(0, net_data);
166 #else
168 setpwent_p(struct net_data *net_data) {
169 return (setpassent_p(0, net_data));
171 #endif
173 void
174 endpwent_p(struct net_data *net_data) {
175 struct irs_pw *pw;
177 if ((net_data != NULL) && ((pw = net_data->pw) != NULL))
178 (*pw->minimize)(pw);
181 /* Private */
183 static struct net_data *
184 init() {
185 struct net_data *net_data;
186 if (!(net_data = net_data_init(NULL)))
187 goto error;
188 if (!net_data->pw) {
189 net_data->pw = (*net_data->irs->pw_map)(net_data->irs);
191 if (!net_data->pw || !net_data->res) {
192 error:
193 errno = EIO;
194 return (NULL);
196 (*net_data->pw->res_set)(net_data->pw, net_data->res, NULL);
199 return (net_data);
202 #endif /* WANT_IRS_PW */
203 /*! \file */