From 8390d39165c38e9a155b7a4b5bf0ccef99126a0a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 24 May 2019 18:11:13 +0900 Subject: [PATCH] table: add TABLE_IFINDEX type --- src/shared/format-table.c | 32 ++++++++++++++++++++++++++++++++ src/shared/format-table.h | 1 + 2 files changed, 33 insertions(+) diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 09357694dd..4f562be81a 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include +#include #include "alloc-util.h" #include "fd-util.h" @@ -80,6 +81,7 @@ typedef struct TableData { uint32_t uint32; uint64_t uint64; int percent; /* we use 'int' as datatype for percent values in order to match the result of parse_percent() */ + int ifindex; /* … add more here as we start supporting more cell data types … */ }; } TableData; @@ -241,6 +243,7 @@ static size_t table_data_size(TableDataType type, const void *data) { case TABLE_INT: case TABLE_UINT: case TABLE_PERCENT: + case TABLE_IFINDEX: return sizeof(int); default: @@ -693,6 +696,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { uint32_t uint32; uint64_t uint64; int percent; + int ifindex; bool b; } buffer; @@ -757,6 +761,11 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { data = &buffer.percent; break; + case TABLE_IFINDEX: + buffer.ifindex = va_arg(ap, int); + data = &buffer.ifindex; + break; + case _TABLE_DATA_TYPE_MAX: /* Used as end marker */ va_end(ap); @@ -898,6 +907,9 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t case TABLE_PERCENT: return CMP(a->percent, b->percent); + case TABLE_IFINDEX: + return CMP(a->ifindex, b->ifindex); + default: ; } @@ -1094,6 +1106,23 @@ static const char *table_data_format(TableData *d) { break; } + case TABLE_IFINDEX: { + _cleanup_free_ char *p; + char name[IF_NAMESIZE + 1] = {}; + + if (if_indextoname(d->ifindex, name)) { + p = strdup(name); + if (!p) + return NULL; + } else { + if (asprintf(&p, "%i" , d->ifindex) < 0) + return NULL; + } + + d->formatted = TAKE_PTR(p); + break; + } + default: assert_not_reached("Unexpected type?"); } @@ -1618,6 +1647,9 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) { case TABLE_PERCENT: return json_variant_new_integer(ret, d->percent); + case TABLE_IFINDEX: + return json_variant_new_integer(ret, d->ifindex); + default: return -EINVAL; } diff --git a/src/shared/format-table.h b/src/shared/format-table.h index c5cf5708d4..ec2bbba292 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -22,6 +22,7 @@ typedef enum TableDataType { TABLE_UINT32, TABLE_UINT64, TABLE_PERCENT, + TABLE_IFINDEX, _TABLE_DATA_TYPE_MAX, _TABLE_DATA_TYPE_INVALID = -1, } TableDataType;