summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2019-06-17 16:08:24 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2019-06-19 23:15:19 +0900
commitaa89266900cbc7f9a89cf8480d1178855524af60 (patch)
tree85fa83b182fa8adbc29573135362115c61524743 /src/basic
parent33a8695fdcdd1c85878287a46bb7b77184f5db8b (diff)
downloadsystemd-aa89266900cbc7f9a89cf8480d1178855524af60.tar.gz
util: introduce format_bytes_full()
And move it into format-util.c.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/format-util.c57
-rw-r--r--src/basic/format-util.h13
-rw-r--r--src/basic/parse-util.c41
-rw-r--r--src/basic/parse-util.h3
4 files changed, 70 insertions, 44 deletions
diff --git a/src/basic/format-util.c b/src/basic/format-util.c
index 39ef2fceef..66551f7526 100644
--- a/src/basic/format-util.c
+++ b/src/basic/format-util.c
@@ -1,5 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
+#include <stdio.h>
+
#include "format-util.h"
#include "memory-util.h"
@@ -8,3 +10,58 @@ char *format_ifname(int ifindex, char buf[static IF_NAMESIZE + 1]) {
memzero(buf, IF_NAMESIZE + 1);
return if_indextoname(ifindex, buf);
}
+
+char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) {
+ typedef struct {
+ const char *suffix;
+ uint64_t factor;
+ } suffix_table;
+ static const suffix_table table_iec[] = {
+ { "E", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
+ { "P", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
+ { "T", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
+ { "G", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
+ { "M", UINT64_C(1024)*UINT64_C(1024) },
+ { "K", UINT64_C(1024) },
+ }, table_non_iec[] = {
+ { "E", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
+ { "P", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
+ { "T", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
+ { "G", UINT64_C(1000)*UINT64_C(1000)*UINT64_C(1000) },
+ { "M", UINT64_C(1000)*UINT64_C(1000) },
+ { "K", UINT64_C(1000) },
+ };
+ const suffix_table *table;
+ size_t i;
+
+ assert_cc(ELEMENTSOF(table_iec) == ELEMENTSOF(table_non_iec));
+
+ if (t == (uint64_t) -1)
+ return NULL;
+
+ table = flag & FORMAT_BYTES_USE_IEC ? table_iec : table_non_iec;
+
+ for (i = 0; i < ELEMENTSOF(table_iec); i++)
+ if (t >= table[i].factor) {
+ if (flag & FORMAT_BYTES_BELOW_POINT)
+ snprintf(buf, l,
+ "%" PRIu64 ".%" PRIu64 "%s",
+ t / table[i].factor,
+ ((t*UINT64_C(10)) / table[i].factor) % UINT64_C(10),
+ table[i].suffix);
+ else
+ snprintf(buf, l,
+ "%" PRIu64 "%s",
+ t / table[i].factor,
+ table[i].suffix);
+
+ goto finish;
+ }
+
+ snprintf(buf, l, "%" PRIu64 "%s", t, flag & FORMAT_BYTES_TRAILING_B ? "B" : "");
+
+finish:
+ buf[l-1] = 0;
+ return buf;
+
+}
diff --git a/src/basic/format-util.h b/src/basic/format-util.h
index 9925a5e991..e0d184a541 100644
--- a/src/basic/format-util.h
+++ b/src/basic/format-util.h
@@ -3,6 +3,7 @@
#include <inttypes.h>
#include <net/if.h>
+#include <stdbool.h>
#if SIZEOF_PID_T == 4
# define PID_PRI PRIi32
@@ -68,3 +69,15 @@
#endif
char *format_ifname(int ifindex, char buf[static IF_NAMESIZE + 1]);
+
+typedef enum {
+ FORMAT_BYTES_USE_IEC = 1 << 0,
+ FORMAT_BYTES_BELOW_POINT = 1 << 1,
+ FORMAT_BYTES_TRAILING_B = 1 << 2,
+} FormatBytesFlag;
+
+#define FORMAT_BYTES_MAX 8
+char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag);
+static inline char *format_bytes(char *buf, size_t l, uint64_t t) {
+ return format_bytes_full(buf, l, t, FORMAT_BYTES_USE_IEC | FORMAT_BYTES_BELOW_POINT | FORMAT_BYTES_TRAILING_B);
+}
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index 7774e794d4..115a1494a2 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -362,47 +362,6 @@ int parse_syscall_and_errno(const char *in, char **name, int *error) {
return 0;
}
-char *format_bytes(char *buf, size_t l, uint64_t t) {
- unsigned i;
-
- /* This only does IEC units so far */
-
- static const struct {
- const char *suffix;
- uint64_t factor;
- } table[] = {
- { "E", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
- { "P", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
- { "T", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
- { "G", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
- { "M", UINT64_C(1024)*UINT64_C(1024) },
- { "K", UINT64_C(1024) },
- };
-
- if (t == (uint64_t) -1)
- return NULL;
-
- for (i = 0; i < ELEMENTSOF(table); i++) {
-
- if (t >= table[i].factor) {
- snprintf(buf, l,
- "%" PRIu64 ".%" PRIu64 "%s",
- t / table[i].factor,
- ((t*UINT64_C(10)) / table[i].factor) % UINT64_C(10),
- table[i].suffix);
-
- goto finish;
- }
- }
-
- snprintf(buf, l, "%" PRIu64 "B", t);
-
-finish:
- buf[l-1] = 0;
- return buf;
-
-}
-
int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
char *x = NULL;
unsigned long l;
diff --git a/src/basic/parse-util.h b/src/basic/parse-util.h
index 5a05dfeac5..3a70b79276 100644
--- a/src/basic/parse-util.h
+++ b/src/basic/parse-util.h
@@ -22,9 +22,6 @@ int parse_range(const char *t, unsigned *lower, unsigned *upper);
int parse_errno(const char *t);
int parse_syscall_and_errno(const char *in, char **name, int *error);
-#define FORMAT_BYTES_MAX 8
-char *format_bytes(char *buf, size_t l, uint64_t t);
-
int safe_atou_full(const char *s, unsigned base, unsigned *ret_u);
static inline int safe_atou(const char *s, unsigned *ret_u) {