From: David Ahern Date: Sat, 5 Oct 2024 23:06:06 +0000 (+0000) Subject: Merge remote-tracking branch 'main/main' into next X-Git-Tag: v6.13.0~15^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0fb4ef8bbbf536bd972cb92829730a64f6e4c5ae;p=thirdparty%2Fiproute2.git Merge remote-tracking branch 'main/main' into next Fixed conflicts in lib/utils.c Signed-off-by: David Ahern --- 0fb4ef8bbbf536bd972cb92829730a64f6e4c5ae diff --cc lib/utils.c index 6cf990067,98c06ab6a..66713251f --- a/lib/utils.c +++ b/lib/utils.c @@@ -39,10 -38,11 +39,13 @@@ int resolve_hosts; int timestamp_short; int pretty; +int use_iec; +int human_readable; const char *_SL_ = "\n"; + static int open_fds[5]; + static int open_fds_cnt; + static int af_byte_len(int af); static void print_time(char *buf, int len, __u32 time); static void print_time64(char *buf, int len, __s64 time); @@@ -2021,42 -2021,22 +2024,61 @@@ FILE *generic_proc_open(const char *env 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); +} ++ + int open_fds_add(int fd) + { + if (open_fds_cnt >= ARRAY_SIZE(open_fds)) + return -1; + + open_fds[open_fds_cnt++] = fd; + return 0; + } + - + void open_fds_close(void) + { + int i; + + for (i = 0; i < open_fds_cnt; i++) + close(open_fds[i]); + + open_fds_cnt = 0; + }