From 2166b4d190bcca36311898bedec5d9f8410f7206 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 7 Jun 2015 15:27:17 +0000 Subject: [PATCH] Handle pretty print (-P) via global pwconf --- usr.sbin/pw/pw.c | 4 ++++ usr.sbin/pw/pw_group.c | 16 +++++++--------- usr.sbin/pw/pw_user.c | 18 ++++++------------ usr.sbin/pw/pwupd.h | 1 + 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/usr.sbin/pw/pw.c b/usr.sbin/pw/pw.c index 7a866802bb16..22665e959ebc 100644 --- a/usr.sbin/pw/pw.c +++ b/usr.sbin/pw/pw.c @@ -133,6 +133,7 @@ main(int argc, char *argv[]) relocated = nis = false; conf.rootdir[0] = '\0'; conf.dryrun = false; + conf.pretty = false; strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath)); LIST_INIT(&arglist); @@ -222,6 +223,9 @@ main(int argc, char *argv[]) case 'N': conf.dryrun = true; break; + case 'P': + conf.pretty = true; + break; case 'Y': nis = true; break; diff --git a/usr.sbin/pw/pw_group.c b/usr.sbin/pw/pw_group.c index 302d44ed01cb..d5d7d5236903 100644 --- a/usr.sbin/pw/pw_group.c +++ b/usr.sbin/pw/pw_group.c @@ -44,7 +44,7 @@ static const char rcsid[] = static struct passwd *lookup_pwent(const char *user); static void delete_members(char ***members, int *grmembers, int *i, struct carg *arg, struct group *grp); -static int print_group(struct group * grp, int pretty); +static int print_group(struct group * grp); static gid_t gr_gidpolicy(struct userconf * cnf, struct cargs * args); int @@ -89,11 +89,9 @@ pw_group(int mode, struct cargs * args) } if (mode == M_PRINT && getarg(args, 'a')) { - int pretty = getarg(args, 'P') != NULL; - SETGRENT(); while ((grp = GETGRENT()) != NULL) - print_group(grp, pretty); + print_group(grp); ENDGRENT(); return EXIT_SUCCESS; } @@ -119,7 +117,7 @@ pw_group(int mode, struct cargs * args) fakegroup.gr_name = a_name ? a_name->val : "nogroup"; fakegroup.gr_gid = a_gid ? (gid_t) atol(a_gid->val) : (gid_t)-1; fakegroup.gr_mem = fmems; - return print_group(&fakegroup, getarg(args, 'P') != NULL); + return print_group(&fakegroup); } errx(EX_DATAERR, "unknown group `%s'", a_name ? a_name->val : a_gid->val); } @@ -141,7 +139,7 @@ pw_group(int mode, struct cargs * args) pw_log(cnf, mode, W_GROUP, "%s(%u) removed", a_name->val, gid); return EXIT_SUCCESS; } else if (mode == M_PRINT) - return print_group(grp, getarg(args, 'P') != NULL); + return print_group(grp); if (a_gid) grp->gr_gid = (gid_t) atoi(a_gid->val); @@ -259,7 +257,7 @@ pw_group(int mode, struct cargs * args) } if (conf.dryrun) - return print_group(grp, getarg(args, 'P') != NULL); + return print_group(grp); if (mode == M_ADD && (rc = addgrent(grp)) != 0) { if (rc == -1) @@ -412,9 +410,9 @@ gr_gidpolicy(struct userconf * cnf, struct cargs * args) static int -print_group(struct group * grp, int pretty) +print_group(struct group * grp) { - if (!pretty) { + if (!conf.pretty) { char *buf = NULL; buf = gr_make(grp); diff --git a/usr.sbin/pw/pw_user.c b/usr.sbin/pw/pw_user.c index fac4d251323b..bfbde78bab49 100644 --- a/usr.sbin/pw/pw_user.c +++ b/usr.sbin/pw/pw_user.c @@ -53,7 +53,7 @@ static char locked_str[] = "*LOCKED*"; static int delete_user(struct userconf *cnf, struct passwd *pwd, struct carg *a_name, int delete, int mode); -static int print_user(struct passwd * pwd, int pretty, int v7); +static int print_user(struct passwd * pwd, int v7); static uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); static uid_t pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer); static time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); @@ -316,11 +316,10 @@ pw_user(int mode, struct cargs * args) } if (mode == M_PRINT && getarg(args, 'a')) { - int pretty = getarg(args, 'P') != NULL; int v7 = getarg(args, '7') != NULL; SETPWENT(); while ((pwd = GETPWENT()) != NULL) - print_user(pwd, pretty, v7); + print_user(pwd, v7); ENDPWENT(); return EXIT_SUCCESS; } @@ -363,7 +362,6 @@ pw_user(int mode, struct cargs * args) fakeuser.pw_name = a_name ? a_name->val : "nouser"; fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : (uid_t) -1; return print_user(&fakeuser, - getarg(args, 'P') != NULL, getarg(args, '7') != NULL); } if (a_name == NULL) @@ -401,9 +399,7 @@ pw_user(int mode, struct cargs * args) return (delete_user(cnf, pwd, a_name, getarg(args, 'r') != NULL, mode)); else if (mode == M_PRINT) - return print_user(pwd, - getarg(args, 'P') != NULL, - getarg(args, '7') != NULL); + return print_user(pwd, getarg(args, '7') != NULL); /* * The rest is edit code @@ -621,9 +617,7 @@ pw_user(int mode, struct cargs * args) * Special case: -N only displays & exits */ if (conf.dryrun) - return print_user(pwd, - getarg(args, 'P') != NULL, - getarg(args, '7') != NULL); + return print_user(pwd, getarg(args, '7') != NULL); if (mode == M_ADD) { edited = 1; /* Always */ @@ -1167,9 +1161,9 @@ delete_user(struct userconf *cnf, struct passwd *pwd, struct carg *a_name, } static int -print_user(struct passwd * pwd, int pretty, int v7) +print_user(struct passwd * pwd, int v7) { - if (!pretty) { + if (!conf.pretty) { char *buf; if (!v7) diff --git a/usr.sbin/pw/pwupd.h b/usr.sbin/pw/pwupd.h index ec9ac30b2afc..63aaa642d9d6 100644 --- a/usr.sbin/pw/pwupd.h +++ b/usr.sbin/pw/pwupd.h @@ -84,6 +84,7 @@ struct pwconf { char rootdir[MAXPATHLEN]; char etcpath[MAXPATHLEN]; bool dryrun; + bool pretty; struct userconf *userconf; };