]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib: utils: move over `print_num` from ip/
authorDavid Lamparter <equinox@diac24.net>
Fri, 4 Oct 2024 09:30:14 +0000 (11:30 +0200)
committerDavid Ahern <dsahern@kernel.org>
Sat, 5 Oct 2024 17:48:11 +0000 (17:48 +0000)
`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 <equinox@diac24.net>
Signed-off-by: David Ahern <dsahern@kernel.org>
include/utils.h
ip/ip.c
ip/ip_common.h
ip/ipaddress.c
lib/utils.c
tc/tc.c
tc/tc_common.h

index a2a98b9bf17d9ed42a6ca4ed7a9c6c601e9c942b..13aac0d033e280a6888ec0f8968b9c0012ab264a 100644 (file)
@@ -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 eb492139a04d7ce8e93ee0b345c51a8c5d5d04f7..c7151fbdc798fe0a62e25b2b6ae1fcb1c620daae 100644 (file)
--- 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;
index 625311c25bce3db459187fdb0e2b92a50a5b325a..350806d9d0cc7173c1a25a447676850a19455ac9 100644 (file)
@@ -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);
index 4e1f934fdbb48b0a6604752a122c695b335beb5a..f7bd14847477e0aed724a0d120ec629941647e22 100644 (file)
@@ -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];
index deb7654a0b013cae66d53741cf85d89c7c2cca0f..6cf990067e0e0e5e1cae8e2151fc2a3f0137b088 100644 (file)
@@ -7,6 +7,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <inttypes.h>
 #include <math.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -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 26e6f69ca81d972713edd8d3ecba583289b1ac40..beb88111dc7925b4f836118aca5059de6952e022 100644 (file)
--- 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;
index f1561d80a43bd065845911496775092d19aed81c..bca6a7c9899f4cacaae8969bcf771be858242a5c 100644 (file)
@@ -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;