summaryrefslogtreecommitdiffstats
path: root/pw/pw_user.c
diff options
context:
space:
mode:
authorBaptiste Daroussin <bapt@FreeBSD.org>2015-07-11 18:09:27 +0000
committerBaptiste Daroussin <bapt@FreeBSD.org>2015-07-11 18:09:27 +0000
commitcc424f0d3468b75718e420a691cb5e3b238804b6 (patch)
treeb1131a1c4026663026eefcdb078848e697a610cc /pw/pw_user.c
parentecf012ac29ba3e82e8b5e497ee1b80629ae5624f (diff)
downloadpw-darwin-cc424f0d3468b75718e420a691cb5e3b238804b6.tar.gz
pw-darwin-cc424f0d3468b75718e420a691cb5e3b238804b6.tar.zst
pw-darwin-cc424f0d3468b75718e420a691cb5e3b238804b6.zip
Make separate functions to show users and groups
Diffstat (limited to 'pw/pw_user.c')
-rw-r--r--pw/pw_user.c52
1 files changed, 35 insertions, 17 deletions
diff --git a/pw/pw_user.c b/pw/pw_user.c
index dc9827a..153bc43 100644
--- a/pw/pw_user.c
+++ b/pw/pw_user.c
@@ -162,6 +162,36 @@ pw_usernext(struct userconf *cnf, bool quiet)
return (EXIT_SUCCESS);
}
+static int
+pw_usershow(char *name, long id, struct passwd *fakeuser)
+{
+ struct passwd *pwd = NULL;
+
+ if (id < 0 && name == NULL && !conf.all)
+ errx(EX_DATAERR, "username or id or '-a' required");
+
+ if (conf.all) {
+ SETPWENT();
+ while ((pwd = GETPWENT()) != NULL)
+ print_user(pwd);
+ ENDPWENT();
+ return (EXIT_SUCCESS);
+ }
+
+ pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
+ if (pwd == NULL) {
+ if (conf.force) {
+ pwd = fakeuser;
+ } else {
+ if (name == NULL)
+ errx(EX_NOUSER, "no such uid `%ld'", id);
+ errx(EX_NOUSER, "no such user `%s'", name);
+ }
+ }
+
+ return (print_user(pwd));
+}
+
/*-
* -C config configuration file
* -q quiet operation
@@ -213,7 +243,7 @@ pw_user(int mode, char *name, long id, struct cargs * args)
static struct passwd fakeuser =
{
- NULL,
+ "nouser",
"*",
-1,
-1,
@@ -233,6 +263,9 @@ pw_user(int mode, char *name, long id, struct cargs * args)
if (mode == M_NEXT)
return (pw_usernext(cnf, conf.quiet));
+ if (mode == M_PRINT)
+ return (pw_usershow(name, id, &fakeuser));
+
/*
* We can do all of the common legwork here
*/
@@ -377,14 +410,6 @@ pw_user(int mode, char *name, long id, struct cargs * args)
err(EX_IOERR, "config udpate");
}
- if (mode == M_PRINT && getarg(args, 'a')) {
- SETPWENT();
- while ((pwd = GETPWENT()) != NULL)
- print_user(pwd);
- ENDPWENT();
- return EXIT_SUCCESS;
- }
-
if (name != NULL)
pwd = GETPWNAM(pw_checkname(name, 0));
@@ -395,17 +420,12 @@ pw_user(int mode, char *name, long id, struct cargs * args)
* Update, delete & print require that the user exists
*/
if (mode == M_UPDATE || mode == M_DELETE ||
- mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) {
+ mode == M_LOCK || mode == M_UNLOCK) {
if (name == NULL && pwd == NULL) /* Try harder */
pwd = GETPWUID(id);
if (pwd == NULL) {
- if (mode == M_PRINT && getarg(args, 'F')) {
- fakeuser.pw_name = name ? name : "nouser";
- fakeuser.pw_uid = (uid_t) id;
- return print_user(&fakeuser);
- }
if (name == NULL)
errx(EX_NOUSER, "no such uid `%ld'", id);
errx(EX_NOUSER, "no such user `%s'", name);
@@ -440,8 +460,6 @@ pw_user(int mode, char *name, long id, struct cargs * args)
} else if (mode == M_DELETE)
return (delete_user(cnf, pwd, name,
getarg(args, 'r') != NULL, mode));
- else if (mode == M_PRINT)
- return print_user(pwd);
/*
* The rest is edit code