pretty_sizes() returns incorrect values

pretty_sizes() returns incorrect values if the argument is < 1024.

pretty_sizes(0) -> 0.00		OK
pretty_sizes(102) -> 0.10	WRONG
pretty_sizes(1023) -> 1.00	WRONG
pretty_sizes(1024) -> 1.00KB	OK

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
This commit is contained in:
Goffredo Baroncelli 2012-10-01 14:11:27 -06:00 committed by root
parent e86f7198d9
commit 29db8b12cd

22
utils.c
View File

@ -1091,25 +1091,27 @@ char *pretty_sizes(u64 size)
{ {
int num_divs = 0; int num_divs = 0;
int pretty_len = 16; int pretty_len = 16;
u64 last_size = size;
u64 fract_size = size;
float fraction; float fraction;
char *pretty; char *pretty;
while(size > 0) { if( size < 1024 ){
fract_size = last_size; fraction = size;
num_divs = 0;
} else {
u64 last_size = size;
num_divs = 0;
while(size >= 1024){
last_size = size; last_size = size;
size /= 1024; size /= 1024;
num_divs++; num_divs ++;
} }
if (num_divs == 0)
num_divs = 1;
if (num_divs > ARRAY_SIZE(size_strs)) if (num_divs > ARRAY_SIZE(size_strs))
return NULL; return NULL;
fraction = (float)last_size / 1024;
fraction = (float)fract_size / 1024; }
pretty = malloc(pretty_len); pretty = malloc(pretty_len);
snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs-1]); snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs]);
return pretty; return pretty;
} }