From 9c7e81ff1ecfd785ac5b554774c2729b4f3633a8 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 14 Feb 2022 13:58:30 +0100 Subject: [PATCH] lslogins: add -y,--shell Signed-off-by: Karel Zak --- login-utils/lslogins.1.adoc | 5 ++++- login-utils/lslogins.c | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/login-utils/lslogins.1.adoc b/login-utils/lslogins.1.adoc index dd9033668f..5d3111252a 100644 --- a/login-utils/lslogins.1.adoc +++ b/login-utils/lslogins.1.adoc @@ -38,7 +38,7 @@ Alternate path for btmp. Separate info about each user with a colon instead of a newline. *-e*, *--export*:: -Output data in the format of NAME=VALUE. +Output data in the format of NAME=VALUE. See also option *--shell*. *-f*, *--failed*:: Display data about the users' last failed login attempts. @@ -95,6 +95,9 @@ Alternate path for wtmp. *--lastlog* _path_:: Alternate path for *lastlog*(8). +*y-*, *--shell*:: +The column name will be modified to contain only characters allowed for shell variable identifiers. This is usable, for example, with *--export*. Note that this feature has been automatically enabled for *--export* in version 2.37, but due to compatibility issues, now it's necessary to request this behavior by *--shell*. + *-Z*, *--context*:: Display the users' security context. diff --git a/login-utils/lslogins.c b/login-utils/lslogins.c index 132046cbf4..4a1c62fe9d 100644 --- a/login-utils/lslogins.c +++ b/login-utils/lslogins.c @@ -281,6 +281,7 @@ struct lslogins_control { unsigned int selinux_enabled : 1, fail_on_unknown : 1, /* fail if user does not exist */ ulist_on : 1, + shellvar : 1, noheadings : 1, notrunc : 1; }; @@ -1072,6 +1073,8 @@ static struct libscols_table *setup_table(struct lslogins_control *ctl) err(EXIT_FAILURE, _("failed to allocate output table")); if (ctl->noheadings) scols_table_enable_noheadings(table, 1); + if (ctl->shellvar) + scols_table_enable_shellvar(table, 1); switch(outmode) { case OUT_COLON: @@ -1420,6 +1423,7 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -s, --system-accs display system accounts\n"), out); fputs(_(" --time-format= display dates in short, full or iso format\n"), out); fputs(_(" -u, --user-accs display user accounts\n"), out); + fputs(_(" -y, --shell use column names to be usable as shell variable identifiers\n"), out); fputs(_(" -Z, --context display SELinux contexts\n"), out); fputs(_(" -z, --print0 delimit user entries with a nul character\n"), out); fputs(_(" --wtmp-file set an alternate path for wtmp\n"), out); @@ -1460,6 +1464,7 @@ int main(int argc, char *argv[]) { "acc-expiration", no_argument, 0, 'a' }, { "colon-separate", no_argument, 0, 'c' }, { "export", no_argument, 0, 'e' }, + { "shell", no_argument, 0, 'y' }, { "failed", no_argument, 0, 'f' }, { "groups", required_argument, 0, 'g' }, { "help", no_argument, 0, 'h' }, @@ -1509,7 +1514,7 @@ int main(int argc, char *argv[]) add_column(columns, ncolumns++, COL_UID); add_column(columns, ncolumns++, COL_USER); - while ((c = getopt_long(argc, argv, "acefGg:hLl:no:prsuVzZ", + while ((c = getopt_long(argc, argv, "acefGg:hLl:no:prsuVyzZ", longopts, NULL)) != -1) { err_exclusive_options(c, longopts, excl, excl_st); @@ -1585,6 +1590,9 @@ int main(int argc, char *argv[]) add_column(columns, ncolumns++, COL_HUSH_STATUS); add_column(columns, ncolumns++, COL_PWDMETHOD); break; + case 'y': + ctl->shellvar = 1; + break; case 'z': outmode = OUT_NUL; break; -- 2.47.2