]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_vpw.c
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * David L. Nugent. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char rcsid
[] =
46 static FILE * pwd_fp
= NULL
;
47 static int pwd_scanflag
;
48 static const char *pwd_filename
;
65 static struct passwd
*
66 vnextpwent(char const *nam
, uid_t uid
, int doclose
)
79 pwd_filename
= _MASTERPASSWD
;
80 pwd_scanflag
= PWSCAN_MASTER
;
82 pwd_filename
= _PASSWD
;
85 pwd_fp
= fopen(getpwpath(pwd_filename
), "r");
89 while ((linelen
= getline(&line
, &linecap
, pwd_fp
)) > 0) {
90 /* Skip comments and empty lines */
91 if (*line
== '\n' || *line
== '#')
94 if (line
[linelen
- 1 ] == '\n')
95 line
[linelen
- 1] = '\0';
96 pw
= pw_scan(line
, pwd_scanflag
);
98 errx(EXIT_FAILURE
, "Invalid user entry in '%s':"
99 " '%s'", getpwpath(pwd_filename
), line
);
100 if (uid
!= (uid_t
)-1) {
101 if (uid
== pw
->pw_uid
)
103 } else if (nam
!= NULL
) {
104 if (strcmp(nam
, pw
->pw_name
) == 0)
122 return vnextpwent(NULL
, -1, 0);
128 return vnextpwent(NULL
, uid
, 1);
132 vgetpwnam(const char * nam
)
134 return vnextpwent(nam
, -1, 1);
138 static FILE * grp_fp
= NULL
;
143 if (grp_fp
!= NULL
) {
155 static struct group
*
156 vnextgrent(char const *nam
, gid_t gid
, int doclose
)
167 if (grp_fp
!= NULL
|| (grp_fp
= fopen(getgrpath(_GROUP
), "r")) != NULL
) {
168 while ((linelen
= getline(&line
, &linecap
, grp_fp
)) > 0) {
169 /* Skip comments and empty lines */
170 if (*line
== '\n' || *line
== '#')
173 if (line
[linelen
- 1 ] == '\n')
174 line
[linelen
- 1] = '\0';
177 errx(EXIT_FAILURE
, "Invalid group entry in '%s':"
178 " '%s'", getgrpath(_GROUP
), line
);
179 if (gid
!= (gid_t
)-1) {
180 if (gid
== gr
->gr_gid
)
182 } else if (nam
!= NULL
) {
183 if (strcmp(nam
, gr
->gr_name
) == 0)
201 return vnextgrent(NULL
, -1, 0);
208 return vnextgrent(NULL
, gid
, 1);
212 vgetgrnam(const char * nam
)
214 return vnextgrent(nam
, -1, 1);