From: Masatake YAMATO Date: Thu, 30 Nov 2023 03:30:20 +0000 (+0900) Subject: lslocks: add -H option printing avaiable columns X-Git-Tag: v2.40-rc1~130^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=06bb044c90349f6449a4b06ca3278a46b210deea;p=thirdparty%2Futil-linux.git lslocks: add -H option printing avaiable columns Signed-off-by: Masatake YAMATO --- diff --git a/misc-utils/lslocks.8.adoc b/misc-utils/lslocks.8.adoc index 0e5c661c24..1efdf7aefb 100644 --- a/misc-utils/lslocks.8.adoc +++ b/misc-utils/lslocks.8.adoc @@ -34,6 +34,9 @@ lslocks - list local system locks *-b*, *--bytes*:: include::man-common/in-bytes.adoc[] +*-H*, *--list-columns*:: +List the available columns. + *-i*, *--noinaccessible*:: Ignore lock files which are inaccessible for the current user. diff --git a/misc-utils/lslocks.c b/misc-utils/lslocks.c index 6eb7f245a1..ec298b2ec8 100644 --- a/misc-utils/lslocks.c +++ b/misc-utils/lslocks.c @@ -47,6 +47,7 @@ #include "closestream.h" #include "optutils.h" #include "procfs.h" +#include "column-list-table.h" /* column IDs */ enum { @@ -78,7 +79,7 @@ static struct colinfo infos[] = { [COL_SRC] = { "COMMAND",15, 0, N_("command of the process holding the lock") }, [COL_PID] = { "PID", 5, SCOLS_FL_RIGHT, N_("PID of the process holding the lock") }, [COL_TYPE] = { "TYPE", 5, SCOLS_FL_RIGHT, N_("kind of lock") }, - [COL_SIZE] = { "SIZE", 4, SCOLS_FL_RIGHT, N_("size of the lock") }, + [COL_SIZE] = { "SIZE", 4, SCOLS_FL_RIGHT, N_("size of the lock. Use if -b/--bytes is given.") }, [COL_INODE] = { "INODE", 5, SCOLS_FL_RIGHT, N_("inode number") }, [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number") }, [COL_MODE] = { "MODE", 5, 0, N_("lock access mode") }, @@ -804,7 +805,6 @@ static int show_locks(struct list_head *locks, pid_t target_pid, void *pid_locks static void __attribute__((__noreturn__)) usage(void) { FILE *out = stdout; - size_t i; fputs(USAGE_HEADER, out); @@ -824,20 +824,44 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -p, --pid display only locks held by this process\n"), out); fputs(_(" -r, --raw use the raw output format\n"), out); fputs(_(" -u, --notruncate don't truncate text in columns\n"), out); + fputs(_(" -H, --list-columns list the available columns\n"), out); fputs(USAGE_SEPARATOR, out); fprintf(out, USAGE_HELP_OPTIONS(24)); fputs(USAGE_COLUMNS, out); - for (i = 0; i < ARRAY_SIZE(infos); i++) - fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help)); - fprintf(out, USAGE_MAN_TAIL("lslocks(8)")); exit(EXIT_SUCCESS); } +static void __attribute__((__noreturn__)) list_colunms(const char *table_name, + FILE *out, + int raw, + int json) +{ + struct libscols_table *col_tb = xcolumn_list_table_new(table_name, out, raw, json); + + for (size_t i = 0; i < ARRAY_SIZE(infos); i++) { + if (i != COL_SIZE) { + int json_type = get_json_type_for_column(i, bytes); + xcolumn_list_table_append_line(col_tb, infos[i].name, + json_type, NULL, + _(infos[i].help)); + } + else + xcolumn_list_table_append_line(col_tb, infos[i].name, + -1, "", + _(infos[i].help)); + } + + scols_print_table(col_tb); + scols_unref_table(col_tb); + + exit(EXIT_SUCCESS); +} + int main(int argc, char *argv[]) { int c, rc = 0; @@ -859,6 +883,7 @@ int main(int argc, char *argv[]) { "noheadings", no_argument, NULL, 'n' }, { "raw", no_argument, NULL, 'r' }, { "noinaccessible", no_argument, NULL, 'i' }, + { "list-columns", no_argument, NULL, 'H' }, { NULL, 0, NULL, 0 } }; @@ -875,7 +900,7 @@ int main(int argc, char *argv[]) close_stdout_atexit(); while ((c = getopt_long(argc, argv, - "biJp:o:nruhV", long_opts, NULL)) != -1) { + "biJp:o:nruhVH", long_opts, NULL)) != -1) { err_exclusive_options(c, long_opts, excl, excl_st); @@ -911,6 +936,8 @@ int main(int argc, char *argv[]) case 'V': print_version(EXIT_SUCCESS); + case 'H': + list_colunms("lslocks-columns", stdout, raw, json); case 'h': usage(); default: