From 1a11bb768f450a512304e5f1235e3ce395ce59c1 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Mon, 17 Aug 2020 14:13:16 -0500 Subject: [PATCH] ifctrstat: Use new structure and recalculate binary size --- Makefile | 2 +- cmd/ifctrstat-linux.c | 48 ++++++++++++++++++++----------------------- cmd/ifctrstat.c | 8 ++++---- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index d050032..8ec124a 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ MULTICALL_${CONFIG_IFQUERY}_OBJ += ${IFQUERY_SRC:.c=.o} CMDS_${CONFIG_IFQUERY} += ifquery CPPFLAGS_${CONFIG_IFQUERY} += -DCONFIG_IFQUERY -# enable ifctrstat applet (+16 KB) +# enable ifctrstat applet (+1 KB) CONFIG_IFCTRSTAT ?= Y IFCTRSTAT_SRC = cmd/ifctrstat.c cmd/ifctrstat-${LAYOUT}.c MULTICALL_${CONFIG_IFCTRSTAT}_OBJ += ${IFCTRSTAT_SRC:.c=.o} diff --git a/cmd/ifctrstat-linux.c b/cmd/ifctrstat-linux.c index 52d48b8..e7dcfa3 100644 --- a/cmd/ifctrstat-linux.c +++ b/cmd/ifctrstat-linux.c @@ -19,19 +19,28 @@ #include #include "multicall.h" -const char *avail_counters[] = { - "rx.octets", - "rx.packets", - "rx.discard", - "rx.errors", - "tx.octets", - "tx.packets", - "tx.discard", - "tx.errors" +struct counter_desc { + const char *name; + const void *data; +} avail_counters[] = { + {"rx.discard", "rx_dropped"}, + {"rx.errors", "rx_errors"}, + {"rx.octets", "rx_bytes"}, + {"rx.packets", "rx_packets"}, + {"tx.discard", "tx_dropped"}, + {"tx.errors", "tx_errors"}, + {"tx.octets", "tx_bytes"}, + {"tx.packets", "tx_packets"} }; size_t avail_counters_count = ARRAY_SIZE(avail_counters); +static int +counter_compare(const void *key, const void *candidate) +{ + return strcasecmp((const char *)key, ((struct counter_desc *)candidate)->name); +} + char * read_counter(const char *interface, const char *counter) { @@ -40,26 +49,13 @@ read_counter(const char *interface, const char *counter) char full_path[PATH_MAX]; char buffer[1024]; size_t in_count; + struct counter_desc *ctrdata; errno = 0; - if (strcasecmp(counter, "rx.octets") == 0) - { - path = "rx_bytes"; - } else if (strcasecmp(counter, "rx.packets") == 0) { - path = "rx_packets"; - } else if (strcasecmp(counter, "rx.discard") == 0) { - path = "rx_dropped"; - } else if (strcasecmp(counter, "rx.errors") == 0) { - path = "rx_errors"; - } else if (strcasecmp(counter, "tx.octets") == 0) { - path = "tx_bytes"; - } else if (strcasecmp(counter, "tx.packets") == 0) { - path = "tx_packets"; - } else if (strcasecmp(counter, "tx.discard") == 0) { - path = "tx_dropped"; - } else if (strcasecmp(counter, "tx.errors") == 0) { - path = "tx_errors"; + ctrdata = bsearch(counter, avail_counters, avail_counters_count, sizeof(struct counter_desc), counter_compare); + if (ctrdata) { + path = (const char *)ctrdata->data; } else { errno = ENOSYS; return NULL; diff --git a/cmd/ifctrstat.c b/cmd/ifctrstat.c index 0409438..9c12231 100644 --- a/cmd/ifctrstat.c +++ b/cmd/ifctrstat.c @@ -21,7 +21,7 @@ #include "libifupdown/libifupdown.h" #include "cmd/multicall.h" -extern const char *avail_counters[]; +extern struct counter_desc { const char *name; const void *data; } avail_counters[]; extern int avail_counters_count; extern const char *read_counter(const char *interface, const char *counter); @@ -33,7 +33,7 @@ counter_is_valid(const char *candidate) { for (int i = 0; i < avail_counters_count; i++) { - if (strcasecmp(avail_counters[i], candidate) == 0) + if (strcasecmp(avail_counters[i].name, candidate) == 0) return true; } @@ -56,7 +56,7 @@ print_all_counters(const char *iface) const char *res; for (int i = 0; i < avail_counters_count; i++) { - const char *ctr = avail_counters[i]; + const char *ctr = avail_counters[i].name; res = read_counter(iface, ctr); if (!res) @@ -81,7 +81,7 @@ ifctrstat_list_counters(int short_opt, const struct if_option *opt, const char * for (int i = 0; i < avail_counters_count; i++) { - fprintf(stdout, "%s\n", avail_counters[i]); + fprintf(stdout, "%s\n", avail_counters[i].name); } exit(EXIT_SUCCESS);