From: David Lamparter Date: Fri, 4 Oct 2024 09:30:14 +0000 (+0200) Subject: lib: utils: move over `print_num` from ip/ X-Git-Tag: v6.13.0~15^2~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=031922c8a302d86d928c5083a9b7732a58e5f5d5;p=thirdparty%2Fiproute2.git lib: utils: move over `print_num` from ip/ `print_num()` was born in `ip/ipaddress.c` but considering it has nothing to do with IP addresses it should really live in `lib/utils.c`. (I've had reason to call it from bridge/* on some random hackery.) Signed-off-by: David Lamparter Signed-off-by: David Ahern --- diff --git a/include/utils.h b/include/utils.h index a2a98b9b..13aac0d0 100644 --- a/include/utils.h +++ b/include/utils.h @@ -37,6 +37,7 @@ extern int batch_mode; extern int numeric; extern bool do_all; extern int echo_request; +extern int use_iec; #ifndef CONF_USR_DIR #define CONF_USR_DIR "/usr/lib/iproute2" @@ -336,6 +337,7 @@ int get_time(unsigned int *time, const char *str); int get_time64(__s64 *time, const char *str); char *sprint_time(__u32 time, char *buf); char *sprint_time64(__s64 time, char *buf); +void print_num(FILE *fp, unsigned int width, uint64_t count); int do_batch(const char *name, bool force, int (*cmd)(int argc, char *argv[], void *user), void *user); diff --git a/ip/ip.c b/ip/ip.c index eb492139..c7151fbd 100644 --- a/ip/ip.c +++ b/ip/ip.c @@ -27,8 +27,6 @@ #endif int preferred_family = AF_UNSPEC; -int human_readable; -int use_iec; int show_stats; int show_details; int oneline; diff --git a/ip/ip_common.h b/ip/ip_common.h index 625311c2..350806d9 100644 --- a/ip/ip_common.h +++ b/ip/ip_common.h @@ -7,8 +7,6 @@ #include "json_print.h" -extern int use_iec; - struct link_filter { int ifindex; int family; @@ -223,7 +221,6 @@ int ipstats_stat_desc_show_xstats(struct ipstats_stat_show_attrs *attrs, #define LABEL_MAX_MASK 0xFFFFFU #endif -void print_num(FILE *fp, unsigned int width, uint64_t count); void print_rt_flags(FILE *fp, unsigned int flags); void print_rta_ifidx(FILE *fp, __u32 ifidx, const char *prefix); void __print_rta_gateway(FILE *fp, unsigned char family, const char *gateway); diff --git a/ip/ipaddress.c b/ip/ipaddress.c index 4e1f934f..f7bd1484 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -568,46 +568,6 @@ void size_columns(unsigned int cols[], unsigned int n, ...) va_end(args); } -void print_num(FILE *fp, unsigned int width, uint64_t count) -{ - const char *prefix = "kMGTPE"; - const unsigned int base = use_iec ? 1024 : 1000; - uint64_t powi = 1; - uint16_t powj = 1; - uint8_t precision = 2; - char buf[64]; - - if (!human_readable || count < base) { - fprintf(fp, "%*"PRIu64" ", width, count); - return; - } - - /* increase value by a factor of 1000/1024 and print - * if result is something a human can read - */ - for (;;) { - powi *= base; - if (count / base < powi) - break; - - if (!prefix[1]) - break; - ++prefix; - } - - /* try to guess a good number of digits for precision */ - for (; precision > 0; precision--) { - powj *= 10; - if (count / powi < powj) - break; - } - - snprintf(buf, sizeof(buf), "%.*f%c%s", precision, - (double) count / powi, *prefix, use_iec ? "i" : ""); - - fprintf(fp, "%*s ", width, buf); -} - static void print_vf_stats64(FILE *fp, struct rtattr *vfstats) { struct rtattr *vf[IFLA_VF_STATS_MAX + 1]; diff --git a/lib/utils.c b/lib/utils.c index deb7654a..6cf99006 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -38,6 +39,8 @@ int resolve_hosts; int timestamp_short; int pretty; +int use_iec; +int human_readable; const char *_SL_ = "\n"; static int af_byte_len(int af); @@ -2017,3 +2020,43 @@ FILE *generic_proc_open(const char *env, const char *name) return fopen(p, "r"); } + +void print_num(FILE *fp, unsigned int width, uint64_t count) +{ + const char *prefix = "kMGTPE"; + const unsigned int base = use_iec ? 1024 : 1000; + uint64_t powi = 1; + uint16_t powj = 1; + uint8_t precision = 2; + char buf[64]; + + if (!human_readable || count < base) { + fprintf(fp, "%*"PRIu64" ", width, count); + return; + } + + /* increase value by a factor of 1000/1024 and print + * if result is something a human can read + */ + for (;;) { + powi *= base; + if (count / base < powi) + break; + + if (!prefix[1]) + break; + ++prefix; + } + + /* try to guess a good number of digits for precision */ + for (; precision > 0; precision--) { + powj *= 10; + if (count / powi < powj) + break; + } + + snprintf(buf, sizeof(buf), "%.*f%c%s", precision, + (double) count / powi, *prefix, use_iec ? "i" : ""); + + fprintf(fp, "%*s ", width, buf); +} diff --git a/tc/tc.c b/tc/tc.c index 26e6f69c..beb88111 100644 --- a/tc/tc.c +++ b/tc/tc.c @@ -31,7 +31,6 @@ int show_graph; int timestamp; int batch_mode; -int use_iec; int force; bool use_names; int json; diff --git a/tc/tc_common.h b/tc/tc_common.h index f1561d80..bca6a7c9 100644 --- a/tc/tc_common.h +++ b/tc/tc_common.h @@ -27,4 +27,3 @@ int check_size_table_opts(struct tc_sizespec *s); extern int show_graph; extern bool use_names; -extern int use_iec;