]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemd-mount: use format-table.[ch]
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 9 Jan 2020 05:39:35 +0000 (14:39 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 9 Jan 2020 11:16:03 +0000 (20:16 +0900)
src/mount/mount-tool.c

index 9f0220a75e7e4a4959dda6b34d79f8aa4936f902..409141287882d08b9e09ffa58950c0ffdf2f2e02 100644 (file)
@@ -14,6 +14,7 @@
 #include "escape.h"
 #include "fd-util.h"
 #include "fileio.h"
+#include "format-table.h"
 #include "format-util.h"
 #include "fs-util.h"
 #include "fstab-util.h"
@@ -1361,44 +1362,13 @@ enum {
         _COLUMN_MAX,
 };
 
-struct item {
-        char* columns[_COLUMN_MAX];
-};
-
-static int compare_item(const struct item *a, const struct item *b) {
-        if (a->columns[COLUMN_NODE] == b->columns[COLUMN_NODE])
-                return 0;
-        if (!a->columns[COLUMN_NODE])
-                return 1;
-        if (!b->columns[COLUMN_NODE])
-                return -1;
-
-        return path_compare(a->columns[COLUMN_NODE], b->columns[COLUMN_NODE]);
-}
-
 static int list_devices(void) {
-
-        static const char * const titles[_COLUMN_MAX] = {
-                [COLUMN_NODE] = "NODE",
-                [COLUMN_PATH] = "PATH",
-                [COLUMN_MODEL] = "MODEL",
-                [COLUMN_WWN] = "WWN",
-                [COLUMN_FSTYPE] = "TYPE",
-                [COLUMN_LABEL] = "LABEL",
-                [COLUMN_UUID] = "UUID"
-        };
-
         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
-        size_t n_allocated = 0, n = 0, i;
-        size_t column_width[_COLUMN_MAX];
-        struct item *items = NULL;
+        _cleanup_(table_unrefp) Table *table = NULL;
         sd_device *d;
         unsigned c;
         int r;
 
-        for (c = 0; c < _COLUMN_MAX; c++)
-                column_width[c] = strlen(titles[c]);
-
         r = sd_device_enumerator_new(&e);
         if (r < 0)
                 return log_oom();
@@ -1411,19 +1381,17 @@ static int list_devices(void) {
         if (r < 0)
                 return log_error_errno(r, "Failed to add property match: %m");
 
-        FOREACH_DEVICE(e, d) {
-                struct item *j;
-
-                if (!GREEDY_REALLOC0(items, n_allocated, n+1)) {
-                        r = log_oom();
-                        goto finish;
-                }
+        table = table_new("NODE", "PATH", "MODEL", "WWN", "TYPE", "LABEL", "UUID");
+        if (!table)
+                return log_oom();
 
-                j = items + n++;
+        r = table_set_sort(table, 0, SIZE_MAX);
+        if (r < 0)
+                return log_error_errno(r, "Failed to set sort index: %m");
 
+        FOREACH_DEVICE(e, d) {
                 for (c = 0; c < _COLUMN_MAX; c++) {
                         const char *x = NULL;
-                        size_t k;
 
                         switch (c) {
 
@@ -1456,59 +1424,19 @@ static int list_devices(void) {
                                 break;
                         }
 
-                        if (isempty(x))
-                                continue;
-
-                        j->columns[c] = strdup(x);
-                        if (!j->columns[c]) {
-                                r = log_oom();
-                                goto finish;
-                        }
-
-                        k = strlen(x);
-                        if (k > column_width[c])
-                                column_width[c] = k;
+                        r = table_add_cell(table, NULL, c == COLUMN_NODE ? TABLE_PATH : TABLE_STRING, strna(x));
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to add cell: %m");
                 }
         }
 
-        if (n == 0) {
-                log_info("No devices found.");
-                goto finish;
-        }
-
-        typesafe_qsort(items, n, compare_item);
-
         (void) pager_open(arg_pager_flags);
 
-        fputs(ansi_underline(), stdout);
-        for (c = 0; c < _COLUMN_MAX; c++) {
-                if (c > 0)
-                        fputc(' ', stdout);
-
-                printf("%-*s", (int) column_width[c], titles[c]);
-        }
-        fputs(ansi_normal(), stdout);
-        fputc('\n', stdout);
-
-        for (i = 0; i < n; i++) {
-                for (c = 0; c < _COLUMN_MAX; c++) {
-                        if (c > 0)
-                                fputc(' ', stdout);
-
-                        printf("%-*s", (int) column_width[c], strna(items[i].columns[c]));
-                }
-                fputc('\n', stdout);
-        }
-
-        r = 0;
-
-finish:
-        for (i = 0; i < n; i++)
-                for (c = 0; c < _COLUMN_MAX; c++)
-                        free(items[i].columns[c]);
+        r = table_print(table, NULL);
+        if (r < 0)
+                return log_error_errno(r, "Failed to print table: %m");
 
-        free(items);
-        return r;
+        return 0;
 }
 
 static int run(int argc, char* argv[]) {