iproute2/lib/json_print_math.c
Roi Dayan 1a22ad2721 build: Fix link errors on some systems
Since moving get_rate() and get_size() from tc to lib, on some
systems we fail to link because of missing math lib.
Move the functions that require math lib to their own c file
and add -lm to dcb that now use those functions.

../lib/libutil.a(utils.o): In function `get_rate':
utils.c:(.text+0x10dc): undefined reference to `floor'
../lib/libutil.a(utils.o): In function `get_size':
utils.c:(.text+0x1394): undefined reference to `floor'
../lib/libutil.a(json_print.o): In function `sprint_size':
json_print.c:(.text+0x14c0): undefined reference to `rint'
json_print.c:(.text+0x14f4): undefined reference to `rint'
json_print.c:(.text+0x157c): undefined reference to `rint'

Fixes: f3be0e6366 ("lib: Move get_rate(), get_rate64() from tc here")
Fixes: 44396bdfcc ("lib: Move get_size() from tc here")
Fixes: adbe5de966 ("lib: Move sprint_size() from tc here, add print_size()")

Signed-off-by: Roi Dayan <roid@nvidia.com>
Reviewed-by: Petr Machata <petrm@nvidia.com>
Tested-by: Petr Machata <petrm@nvidia.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
2021-01-18 12:28:47 -08:00

38 lines
861 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(__u32 sz, char *buf)
{
long kilo = 1024;
long mega = kilo * kilo;
size_t len = SPRINT_BSIZE - 1;
double tmp = sz;
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, "%ub", sz);
return buf;
}
int print_color_size(enum output_type type, enum color_attr color,
const char *key, const char *fmt, __u32 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);
}