format-table: add cell type for "mode_t" values

This commit is contained in:
Lennart Poettering
2021-07-08 10:29:43 +02:00
parent 105a4245ff
commit bc773fcb35
2 changed files with 36 additions and 0 deletions
+35
View File
@@ -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;
}
+1
View File
@@ -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