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 <equinox@diac24.net> Signed-off-by: David Ahern <dsahern@kernel.org>
This commit is contained in:
committed by
David Ahern
parent
9dafac4fdd
commit
031922c8a3
@@ -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);
|
||||
|
||||
2
ip/ip.c
2
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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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];
|
||||
|
||||
43
lib/utils.c
43
lib/utils.c
@@ -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);
|
||||
}
|
||||
|
||||
1
tc/tc.c
1
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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user