mirror of
https://git.kernel.org/pub/scm/network/iproute2/iproute2.git
synced 2026-01-26 14:13:24 +00:00
In preparation for accepting 64 bit burst sizes, modify sprint_size, the formatting function behind print_size, to accept __u64 as its size parameter. Also include a "Gb" size category. Acked-by: Jamal Hadi Salim <jhs@mojatatu.com> Signed-off-by: Jay Vosburgh <jay.vosburgh@canonical.com> Signed-off-by: David Ahern <dsahern@kernel.org>
41 lines
1004 B
C
41 lines
1004 B
C
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <math.h>
|
|
|
|
#include "utils.h"
|
|
#include "json_print.h"
|
|
|
|
char *sprint_size(__u64 sz, char *buf)
|
|
{
|
|
long kilo = 1024;
|
|
long mega = kilo * kilo;
|
|
long giga = mega * kilo;
|
|
size_t len = SPRINT_BSIZE - 1;
|
|
double tmp = sz;
|
|
|
|
if (sz >= giga && fabs(giga * rint(tmp / giga) - sz) < 1024)
|
|
snprintf(buf, len, "%gGb", rint(tmp / giga));
|
|
else if (sz >= mega && fabs(mega * rint(tmp / mega) - sz) < 1024)
|
|
snprintf(buf, len, "%gMb", rint(tmp / mega));
|
|
else if (sz >= kilo && fabs(kilo * rint(tmp / kilo) - sz) < 16)
|
|
snprintf(buf, len, "%gKb", rint(tmp / kilo));
|
|
else
|
|
snprintf(buf, len, "%llub", sz);
|
|
|
|
return buf;
|
|
}
|
|
|
|
int print_color_size(enum output_type type, enum color_attr color,
|
|
const char *key, const char *fmt, __u64 sz)
|
|
{
|
|
SPRINT_BUF(buf);
|
|
|
|
if (_IS_JSON_CONTEXT(type))
|
|
return print_color_uint(type, color, key, "%u", sz);
|
|
|
|
sprint_size(sz, buf);
|
|
return print_color_string(type, color, key, fmt, buf);
|
|
}
|