]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/format-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "format-util.h"
6 #include "memory-util.h"
8 char *format_ifname(int ifindex
, char buf
[static IF_NAMESIZE
+ 1]) {
9 /* Buffer is always cleared */
10 memzero(buf
, IF_NAMESIZE
+ 1);
11 return if_indextoname(ifindex
, buf
);
14 char *format_bytes_full(char *buf
, size_t l
, uint64_t t
, FormatBytesFlag flag
) {
19 static const suffix_table table_iec
[] = {
20 { "E", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
21 { "P", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
22 { "T", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
23 { "G", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
24 { "M", UINT64_C(1024)*UINT64_C(1024) },
25 { "K", UINT64_C(1024) },
26 }, table_non_iec
[] = {
27 { "E", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
28 { "P", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
29 { "T", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
30 { "G", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
31 { "M", UINT64_C(1000)*UINT64_C(1000) },
32 { "K", UINT64_C(1000) },
34 const suffix_table
*table
;
37 assert_cc(ELEMENTSOF(table_iec
) == ELEMENTSOF(table_non_iec
));
39 if (t
== (uint64_t) -1)
42 table
= flag
& FORMAT_BYTES_USE_IEC
? table_iec
: table_non_iec
;
44 for (i
= 0; i
< ELEMENTSOF(table_iec
); i
++)
45 if (t
>= table
[i
].factor
) {
46 if (flag
& FORMAT_BYTES_BELOW_POINT
)
48 "%" PRIu64
".%" PRIu64
"%s",
50 ((t
*UINT64_C(10)) / table
[i
].factor
) % UINT64_C(10),
61 snprintf(buf
, l
, "%" PRIu64
"%s", t
, flag
& FORMAT_BYTES_TRAILING_B
? "B" : "");