]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
format-table: add field type that outputs hex values prefixed with 0x
authorLennart Poettering <lennart@poettering.net>
Tue, 4 Jun 2024 09:04:01 +0000 (11:04 +0200)
committerLennart Poettering <lennart@poettering.net>
Sun, 2 Nov 2025 20:14:35 +0000 (21:14 +0100)
src/shared/format-table.c
src/shared/format-table.h

index c129f260e8c03d67be24ce5cb5c15e01bfcdc3e6..1c728a3d2bcf72ed951abf578150ca454e612f35 100644 (file)
@@ -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:
index 2222373eb33a495656739fda3d34103a1ac0c8b5..b0b9b2060e483f32da89b70f017bcedd368d41fc 100644 (file)
@@ -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) */