1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "format-util.h"
4 #include "memory-util.h"
5 #include "stdio-util.h"
7 assert_cc(DECIMAL_STR_MAX(int) + 1 <= IF_NAMESIZE
+ 1);
8 char *format_ifname_full(int ifindex
, char buf
[static IF_NAMESIZE
+ 1], FormatIfnameFlag flag
) {
9 /* Buffer is always cleared */
10 memzero(buf
, IF_NAMESIZE
+ 1);
11 if (if_indextoname(ifindex
, buf
))
14 if (!FLAGS_SET(flag
, FORMAT_IFNAME_IFINDEX
))
17 if (FLAGS_SET(flag
, FORMAT_IFNAME_IFINDEX_WITH_PERCENT
))
18 snprintf(buf
, IF_NAMESIZE
+ 1, "%%%d", ifindex
);
20 snprintf(buf
, IF_NAMESIZE
+ 1, "%d", ifindex
);
25 char *format_bytes_full(char *buf
, size_t l
, uint64_t t
, FormatBytesFlag flag
) {
30 static const suffix_table table_iec
[] = {
31 { "E", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
32 { "P", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
33 { "T", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
34 { "G", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
35 { "M", UINT64_C(1024)*UINT64_C(1024) },
36 { "K", UINT64_C(1024) },
38 { "E", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
39 { "P", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
40 { "T", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
41 { "G", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
42 { "M", UINT64_C(1000)*UINT64_C(1000) },
43 { "K", UINT64_C(1000) },
45 const suffix_table
*table
;
48 assert_cc(ELEMENTSOF(table_iec
) == ELEMENTSOF(table_si
));
50 if (t
== (uint64_t) -1)
53 table
= flag
& FORMAT_BYTES_USE_IEC
? table_iec
: table_si
;
54 n
= ELEMENTSOF(table_iec
);
56 for (i
= 0; i
< n
; i
++)
57 if (t
>= table
[i
].factor
) {
58 if (flag
& FORMAT_BYTES_BELOW_POINT
) {
60 "%" PRIu64
".%" PRIu64
"%s",
63 (t
/ table
[i
+ 1].factor
* UINT64_C(10) / table
[n
- 1].factor
) % UINT64_C(10):
64 (t
* UINT64_C(10) / table
[i
].factor
) % UINT64_C(10),
75 snprintf(buf
, l
, "%" PRIu64
"%s", t
, flag
& FORMAT_BYTES_TRAILING_B
? "B" : "");