]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_vpw.c
3 * David L. Nugent. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 static const char rcsid
[] =
40 #include <sys/param.h>
44 static FILE * pwd_fp
= NULL
;
61 static struct passwd
*
62 vnextpwent(char const *nam
, uid_t uid
, int doclose
)
74 if (pwd_fp
!= NULL
|| (pwd_fp
= fopen(getpwpath(_MASTERPASSWD
), "r")) != NULL
) {
75 while ((linelen
= getline(&line
, &linecap
, pwd_fp
)) > 0) {
76 /* Skip comments and empty lines */
77 if (*line
== '\n' || *line
== '#')
80 if (line
[linelen
- 1 ] == '\n')
81 line
[linelen
- 1] = '\0';
82 pw
= pw_scan(line
, PWSCAN_MASTER
);
83 if (uid
!= (uid_t
)-1) {
84 if (uid
== pw
->pw_uid
)
86 } else if (nam
!= NULL
) {
87 if (strcmp(nam
, pw
->pw_name
) == 0)
105 return vnextpwent(NULL
, -1, 0);
111 return vnextpwent(NULL
, uid
, 1);
115 vgetpwnam(const char * nam
)
117 return vnextpwent(nam
, -1, 1);
121 static FILE * grp_fp
= NULL
;
126 if (grp_fp
!= NULL
) {
136 #if defined(__FreeBSD__)
141 static struct group
*
142 vnextgrent(char const *nam
, gid_t gid
, int doclose
)
154 if (grp_fp
!= NULL
|| (grp_fp
= fopen(getgrpath(_GROUP
), "r")) != NULL
) {
155 while ((linelen
= getline(&line
, &linecap
, grp_fp
)) > 0) {
156 /* Skip comments and empty lines */
157 if (*line
== '\n' || *line
== '#')
160 if (line
[linelen
- 1 ] == '\n')
161 line
[linelen
- 1] = '\0';
163 if (gid
!= (gid_t
)-1) {
164 if (gid
== gr
->gr_gid
)
166 } else if (nam
!= NULL
) {
167 if (strcmp(nam
, gr
->gr_name
) == 0)
185 return vnextgrent(NULL
, -1, 0);
192 return vnextgrent(NULL
, gid
, 1);
196 vgetgrnam(const char * nam
)
198 return vnextgrent(nam
, -1, 1);