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>
master
David Sterba 2017-01-03 18:03:44 +01:00
parent 2f682fb89b
commit 72ae343f77
2 changed files with 38 additions and 12 deletions

44
utils.c
View File

@ -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]);
}

View File

@ -95,6 +95,8 @@ void set_argv0(char **argv);
#define UNITS_RAW (1U << UNITS_MODE_SHIFT)
#define UNITS_BINARY (2U << UNITS_MODE_SHIFT)
#define UNITS_DECIMAL (3U << UNITS_MODE_SHIFT)
/* Interpret the u64 value as s64 */
#define UNITS_NEGATIVE (4U << UNITS_MODE_SHIFT)
#define UNITS_MODE_MASK ((1U << UNITS_MODE_SHIFT) - 1)
#define UNITS_MODE_SHIFT (8)
#define UNITS_HUMAN_BINARY (UNITS_BINARY)
@ -174,9 +176,9 @@ int check_mounted_where(int fd, const char *file, char *where, int size,
int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
int super_offset);
int pretty_size_snprintf(s64 size, char *str, size_t str_bytes, unsigned unit_mode);
int pretty_size_snprintf(u64 size, char *str, size_t str_bytes, unsigned unit_mode);
#define pretty_size(size) pretty_size_mode(size, UNITS_DEFAULT)
const char *pretty_size_mode(s64 size, unsigned mode);
const char *pretty_size_mode(u64 size, unsigned mode);
u64 parse_size(char *s);
u64 parse_qgroupid(const char *p);