]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lslocks: add -H option printing avaiable columns
authorMasatake YAMATO <yamato@redhat.com>
Thu, 30 Nov 2023 03:30:20 +0000 (12:30 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Thu, 30 Nov 2023 09:41:07 +0000 (18:41 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lslocks.8.adoc
misc-utils/lslocks.c

index 0e5c661c24f7887975e6734ae1faeed56b6915eb..1efdf7aefb60265bdf943567161aa562a14493a3 100644 (file)
@@ -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.
 
index 6eb7f245a1921b0a4ca71d986f9e2201a0af4886..ec298b2ec8e1bc21a828343fa3afa08be67929e5 100644 (file)
@@ -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 <number> 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 <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, "<string|number>",
+                                                      _(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: