diff options
author | David Sterba <dsterba@suse.com> | 2017-01-03 18:03:44 +0100 |
---|---|---|
committer | David Sterba <dsterba@suse.com> | 2017-01-25 09:47:32 +0100 |
commit | 72ae343f772120ec52533e071e8bea58fd76ad3a (patch) | |
tree | 3abf68685d98b841eec5a5651b1f1d4f43b4cd1f /utils.c | |
parent | 2f682fb89b0b69647a4d27f1de9b4f608b0c7895 (diff) |
btrfs-progs: make negative number pretty printing optional
Add a unit mode that will interpret the input number as a signed 64bit,
optionally and not by default for all numbers.
Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'utils.c')
-rw-r--r-- | utils.c | 44 |
1 files changed, 34 insertions, 10 deletions
@@ -2579,7 +2579,7 @@ out: * Note: this function uses a static per-thread buffer. Do not call this * function more than 10 times within one argument list! */ -const char *pretty_size_mode(s64 size, unsigned mode) +const char *pretty_size_mode(u64 size, unsigned mode) { static __thread int ps_index = 0; static __thread char ps_array[10][32]; @@ -2598,20 +2598,27 @@ static const char* unit_suffix_binary[] = static const char* unit_suffix_decimal[] = { "B", "kB", "MB", "GB", "TB", "PB", "EB"}; -int pretty_size_snprintf(s64 size, char *str, size_t str_size, unsigned unit_mode) +int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mode) { int num_divs; float fraction; - s64 base = 0; + u64 base = 0; int mult = 0; const char** suffix = NULL; - s64 last_size; + u64 last_size; + int negative; if (str_size == 0) return 0; + negative = !!(unit_mode & UNITS_NEGATIVE); + unit_mode &= ~UNITS_NEGATIVE; + if ((unit_mode & ~UNITS_MODE_MASK) == UNITS_RAW) { - snprintf(str, str_size, "%lld", size); + if (negative) + snprintf(str, str_size, "%lld", size); + else + snprintf(str, str_size, "%llu", size); return 0; } @@ -2646,10 +2653,22 @@ int pretty_size_snprintf(s64 size, char *str, size_t str_size, unsigned unit_mod num_divs = 0; break; default: - while ((size < 0 ? -size : size) >= mult) { - last_size = size; - size /= mult; - num_divs++; + if (negative) { + s64 ssize = (s64)size; + s64 last_ssize = ssize; + + while ((ssize < 0 ? -ssize : ssize) >= mult) { + last_ssize = ssize; + ssize /= mult; + num_divs++; + } + last_size = (u64)last_ssize; + } else { + while (size >= mult) { + last_size = size; + size /= mult; + num_divs++; + } } /* * If the value is smaller than base, we didn't do any @@ -2667,7 +2686,12 @@ int pretty_size_snprintf(s64 size, char *str, size_t str_size, unsigned unit_mod assert(0); return -1; } - fraction = (float)last_size / base; + + if (negative) { + fraction = (float)(s64)last_size / base; + } else { + fraction = (float)last_size / base; + } return snprintf(str, str_size, "%.2f%s", fraction, suffix[num_divs]); } |