From bc773fcb355ceb09abf9838ab5c23336b57bd501 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 6 Jan 2021 15:51:05 +0100 Subject: [PATCH 1/3] format-table: add cell type for "mode_t" values --- src/shared/format-table.c | 35 +++++++++++++++++++++++++++++++++++ src/shared/format-table.h | 1 + 2 files changed, 36 insertions(+) diff --git a/src/shared/format-table.c b/src/shared/format-table.c index c4c3439541..71885e6ac7 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -11,6 +11,7 @@ #include "fileio.h" #include "format-table.h" #include "format-util.h" +#include "fs-util.h" #include "gunicode.h" #include "id128-util.h" #include "in-addr-util.h" @@ -106,6 +107,7 @@ typedef struct TableData { uid_t uid; gid_t gid; pid_t pid; + mode_t mode; /* … add more here as we start supporting more cell data types … */ }; } TableData; @@ -309,6 +311,9 @@ static size_t table_data_size(TableDataType type, const void *data) { case TABLE_PID: return sizeof(pid_t); + case TABLE_MODE: + return sizeof(mode_t); + default: assert_not_reached("Uh? Unexpected cell type"); } @@ -815,6 +820,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { uid_t uid; gid_t gid; pid_t pid; + mode_t mode; } buffer; switch (type) { @@ -961,6 +967,11 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { data = &buffer.pid; break; + case TABLE_MODE: + buffer.mode = va_arg(ap, mode_t); + data = &buffer.mode; + break; + case TABLE_SET_MINIMUM_WIDTH: { size_t w = va_arg(ap, size_t); @@ -1273,6 +1284,9 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t case TABLE_PID: return CMP(a->pid, b->pid); + case TABLE_MODE: + return CMP(a->mode, b->mode); + default: ; } @@ -1718,6 +1732,21 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas break; } + case TABLE_MODE: { + _cleanup_free_ char *p; + + if (d->mode == MODE_INVALID) + return "n/a"; + + p = new(char, 4 + 1); + if (!p) + return NULL; + + sprintf(p, "%04o", d->mode & 07777); + d->formatted = TAKE_PTR(p); + break; + } + default: assert_not_reached("Unexpected type?"); } @@ -2525,6 +2554,12 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) { return json_variant_new_integer(ret, d->int_val); + case TABLE_MODE: + if (d->mode == MODE_INVALID) + return json_variant_new_null(ret); + + return json_variant_new_unsigned(ret, d->mode); + default: return -EINVAL; } diff --git a/src/shared/format-table.h b/src/shared/format-table.h index 57f167f7f1..411922acf7 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -43,6 +43,7 @@ typedef enum TableDataType { TABLE_GID, TABLE_PID, TABLE_SIGNAL, + TABLE_MODE, /* as in UNIX file mode (mode_t), in typical octal output */ _TABLE_DATA_TYPE_MAX, /* The following are not really data types, but commands for table_add_cell_many() to make changes to From 21fbf095b8aa46db8d1bd51507af666a04fa0d12 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 6 Jan 2021 15:51:35 +0100 Subject: [PATCH 2/3] format-table: add cell type for outputting 64bit values in hex --- src/shared/format-table.c | 16 ++++++++++++++++ src/shared/format-table.h | 1 + 2 files changed, 17 insertions(+) diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 71885e6ac7..f324962b9c 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -272,6 +272,7 @@ static size_t table_data_size(TableDataType type, const void *data) { case TABLE_SIZE: case TABLE_INT64: case TABLE_UINT64: + case TABLE_UINT64_HEX: case TABLE_BPS: return sizeof(uint64_t); @@ -922,6 +923,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { break; case TABLE_UINT64: + case TABLE_UINT64_HEX: buffer.uint64 = va_arg(ap, uint64_t); data = &buffer.uint64; break; @@ -1257,6 +1259,7 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t return CMP(a->uint32, b->uint32); case TABLE_UINT64: + case TABLE_UINT64_HEX: return CMP(a->uint64, b->uint64); case TABLE_PERCENT: @@ -1608,6 +1611,18 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas break; } + case TABLE_UINT64_HEX: { + _cleanup_free_ char *p; + + p = new(char, 16 + 1); + if (!p) + return NULL; + + sprintf(p, "%" PRIx64, d->uint64); + d->formatted = TAKE_PTR(p); + break; + } + case TABLE_PERCENT: { _cleanup_free_ char *p = NULL; @@ -2503,6 +2518,7 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) { return json_variant_new_unsigned(ret, d->uint32); case TABLE_UINT64: + case TABLE_UINT64_HEX: return json_variant_new_unsigned(ret, d->uint64); case TABLE_PERCENT: diff --git a/src/shared/format-table.h b/src/shared/format-table.h index 411922acf7..e31c71f792 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -33,6 +33,7 @@ typedef enum TableDataType { TABLE_UINT16, TABLE_UINT32, TABLE_UINT64, + TABLE_UINT64_HEX, TABLE_PERCENT, TABLE_IFINDEX, TABLE_IN_ADDR, /* Takes a union in_addr_union (or a struct in_addr) */ From 2c177b30e3756711cacecc2f4f759919920b841b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 4 Mar 2021 22:07:46 +0100 Subject: [PATCH 3/3] format-table: teach table_hide_column_from_display() to accept multiple arguments In case we want to hide multiple columns in one go, make that easy. --- src/shared/format-table.c | 25 ++++++++++++++++++++----- src/shared/format-table.h | 3 ++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/shared/format-table.c b/src/shared/format-table.c index f324962b9c..1c28fa73fa 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -1162,11 +1162,11 @@ int table_set_sort_internal(Table *t, size_t first_column, ...) { return 0; } -int table_hide_column_from_display(Table *t, size_t column) { +int table_hide_column_from_display_internal(Table *t, ...) { + size_t cur = 0; int r; assert(t); - assert(column < t->n_columns); /* If the display map is empty, initialize it with all available columns */ if (!t->display_map) { @@ -1175,10 +1175,25 @@ int table_hide_column_from_display(Table *t, size_t column) { return r; } - size_t allocated = t->n_display_map, cur = 0; + for (size_t i = 0; i < t->n_display_map; i++) { + bool listed = false; + va_list ap; - for (size_t i = 0; i < allocated; i++) { - if (t->display_map[i] == column) + va_start(ap, t); + for (;;) { + size_t column; + + column = va_arg(ap, size_t); + if (column == SIZE_MAX) + break; + if (column == t->display_map[i]) { + listed = true; + break; + } + } + va_end(ap); + + if (listed) continue; t->display_map[cur++] = t->display_map[i]; diff --git a/src/shared/format-table.h b/src/shared/format-table.h index e31c71f792..087daf3485 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -107,7 +107,8 @@ int table_set_display_internal(Table *t, size_t first_column, ...); int table_set_sort_internal(Table *t, size_t first_column, ...); #define table_set_sort(...) table_set_sort_internal(__VA_ARGS__, SIZE_MAX) int table_set_reverse(Table *t, size_t column, bool b); -int table_hide_column_from_display(Table *t, size_t column); +int table_hide_column_from_display_internal(Table *t, ...); +#define table_hide_column_from_display(t, ...) table_hide_column_from_display_internal(t, __VA_ARGS__, (size_t) -1) int table_print(Table *t, FILE *f); int table_format(Table *t, char **ret);