From: Lennart Poettering Date: Tue, 4 Jun 2024 09:04:01 +0000 (+0200) Subject: format-table: add field type that outputs hex values prefixed with 0x X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=86279dc970bdab56ed5bfc7e363e6288e5e650b2;p=thirdparty%2Fsystemd.git format-table: add field type that outputs hex values prefixed with 0x --- diff --git a/src/shared/format-table.c b/src/shared/format-table.c index c129f260e8c..1c728a3d2bc 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -313,12 +313,14 @@ static size_t table_data_size(TableDataType type, const void *data) { case TABLE_INT64: case TABLE_UINT64: case TABLE_UINT64_HEX: + case TABLE_UINT64_HEX_0x: case TABLE_BPS: return sizeof(uint64_t); case TABLE_INT32: case TABLE_UINT32: case TABLE_UINT32_HEX: + case TABLE_UINT32_HEX_0x: return sizeof(uint32_t); case TABLE_INT16: @@ -1028,12 +1030,14 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { case TABLE_UINT32: case TABLE_UINT32_HEX: + case TABLE_UINT32_HEX_0x: buffer.uint32 = va_arg(ap, uint32_t); data = &buffer.uint32; break; case TABLE_UINT64: case TABLE_UINT64_HEX: + case TABLE_UINT64_HEX_0x: buffer.uint64 = va_arg(ap, uint64_t); data = &buffer.uint64; break; @@ -1455,10 +1459,12 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t case TABLE_UINT32: case TABLE_UINT32_HEX: + case TABLE_UINT32_HEX_0x: return CMP(a->uint32, b->uint32); case TABLE_UINT64: case TABLE_UINT64_HEX: + case TABLE_UINT64_HEX_0x: return CMP(a->uint64, b->uint64); case TABLE_PERCENT: @@ -1859,6 +1865,18 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas break; } + case TABLE_UINT32_HEX_0x: { + _cleanup_free_ char *p = NULL; + + p = new(char, 2 + 8 + 1); + if (!p) + return NULL; + + sprintf(p, "0x%" PRIx32, d->uint32); + d->formatted = TAKE_PTR(p); + break; + } + case TABLE_UINT64: { _cleanup_free_ char *p = NULL; @@ -1883,6 +1901,18 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas break; } + case TABLE_UINT64_HEX_0x: { + _cleanup_free_ char *p = NULL; + + p = new(char, 2 + 16 + 1); + if (!p) + return NULL; + + sprintf(p, "0x%" PRIx64, d->uint64); + d->formatted = TAKE_PTR(p); + break; + } + case TABLE_PERCENT: { _cleanup_free_ char *p = NULL; @@ -2822,10 +2852,12 @@ static int table_data_to_json(TableData *d, sd_json_variant **ret) { case TABLE_UINT32: case TABLE_UINT32_HEX: + case TABLE_UINT32_HEX_0x: return sd_json_variant_new_unsigned(ret, d->uint32); case TABLE_UINT64: case TABLE_UINT64_HEX: + case TABLE_UINT64_HEX_0x: return sd_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 2222373eb33..b0b9b2060e4 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -40,8 +40,10 @@ typedef enum TableDataType { TABLE_UINT16, TABLE_UINT32, TABLE_UINT32_HEX, + TABLE_UINT32_HEX_0x, TABLE_UINT64, TABLE_UINT64_HEX, + TABLE_UINT64_HEX_0x, TABLE_PERCENT, TABLE_IFINDEX, TABLE_IN_ADDR, /* Takes a union in_addr_union (or a struct in_addr) */