diff options
author | Lennart Poettering <lennart@poettering.net> | 2022-12-21 17:06:14 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2022-12-23 17:35:21 +0100 |
commit | d0242ac94604aa9a862f8ca5746c70bc6719fe85 (patch) | |
tree | 27e68dd9f8217791bc977b6e47a0212ab47acdef /src/shared/format-table.c | |
parent | tmpfiles: port basename() → path_extract_filename() (diff) | |
download | systemd-d0242ac94604aa9a862f8ca5746c70bc6719fe85.tar.xz systemd-d0242ac94604aa9a862f8ca5746c70bc6719fe85.zip |
format-table: add field type TABLE_PATH_BASENAME
This is just like TABLE_PATH, but only shows the basename in regular
tabular output.
This is useful in systemd-repart for example
Diffstat (limited to 'src/shared/format-table.c')
-rw-r--r-- | src/shared/format-table.c | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 1c1db2f43a..4795e1ad80 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -281,6 +281,7 @@ static size_t table_data_size(TableDataType type, const void *data) { case TABLE_STRING: case TABLE_PATH: + case TABLE_PATH_BASENAME: case TABLE_FIELD: case TABLE_HEADER: return strlen(data) + 1; @@ -513,7 +514,7 @@ int table_add_cell_stringf_full(Table *t, TableCell **ret_cell, TableDataType dt int r; assert(t); - assert(IN_SET(dt, TABLE_STRING, TABLE_PATH, TABLE_FIELD, TABLE_HEADER)); + assert(IN_SET(dt, TABLE_STRING, TABLE_PATH, TABLE_PATH_BASENAME, TABLE_FIELD, TABLE_HEADER)); va_start(ap, format); r = vasprintf(&buffer, format, ap); @@ -873,6 +874,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { case TABLE_STRING: case TABLE_PATH: + case TABLE_PATH_BASENAME: case TABLE_FIELD: case TABLE_HEADER: data = va_arg(ap, const char *); @@ -1281,6 +1283,7 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t return strcmp(a->string, b->string); case TABLE_PATH: + case TABLE_PATH_BASENAME: return path_compare(a->string, b->string); case TABLE_STRV: @@ -1453,15 +1456,24 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas case TABLE_STRING: case TABLE_PATH: + case TABLE_PATH_BASENAME: case TABLE_FIELD: - case TABLE_HEADER: + case TABLE_HEADER: { + _cleanup_free_ char *bn = NULL; + const char *s; + + if (d->type == TABLE_PATH_BASENAME) + s = path_extract_filename(d->string, &bn) < 0 ? d->string : bn; + else + s = d->string; + if (d->uppercase && !avoid_uppercasing) { - d->formatted = new(char, strlen(d->string) + (d->type == TABLE_FIELD) + 1); + d->formatted = new(char, strlen(s) + (d->type == TABLE_FIELD) + 1); if (!d->formatted) return NULL; char *q = d->formatted; - for (char *p = d->string; *p; p++) + for (const char *p = s; *p; p++) *(q++) = (char) toupper((unsigned char) *p); if (d->type == TABLE_FIELD) @@ -1470,14 +1482,20 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas *q = 0; return d->formatted; } else if (d->type == TABLE_FIELD) { - d->formatted = strjoin(d->string, ":"); + d->formatted = strjoin(s, ":"); if (!d->formatted) return NULL; return d->formatted; } + if (bn) { + d->formatted = TAKE_PTR(bn); + return d->formatted; + } + return d->string; + } case TABLE_STRV: if (strv_isempty(d->strv)) @@ -2544,6 +2562,7 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) { case TABLE_STRING: case TABLE_PATH: + case TABLE_PATH_BASENAME: case TABLE_FIELD: case TABLE_HEADER: return json_variant_new_string(ret, d->string); |