X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libbb%2Fhuman_readable.c;h=ff1b5514118fcb49eef1cde03c288313820cc85c;hb=35fb51272863c8723a40e59d2024c7f4c9ec8946;hp=548712c75bcd624f456182d0498964267ee1ef24;hpb=d877d44d127cb58181f8f8886675406fb99cc6aa;p=oweals%2Fbusybox.git diff --git a/libbb/human_readable.c b/libbb/human_readable.c index 548712c75..ff1b55141 100644 --- a/libbb/human_readable.c +++ b/libbb/human_readable.c @@ -1,3 +1,4 @@ +/* vi: set sw=4 ts=4: */ /* * June 30, 2001 Manuel Novoa III * @@ -13,10 +14,9 @@ * representations (say, powers of 1024) and manipulating coefficients. * The base ten "bytes" output could be handled similarly. * - * 2) The output of "ls -sh" can be misaligned because this routine always - * outputs a decimal point and a tenths digit when display_unit != 0. - * Hence, it isn't uncommon for the returned string to have a length - * of 5 or 6 instead of <= 4 (as assumed). + * 2) This routine always outputs a decimal point and a tenths digit when + * display_unit != 0. Hence, it isn't uncommon for the returned string + * to have a length of 5 or 6. * * It might be nice to add a flag to indicate no decimal digits in * that case. This could be either an additional parameter, or a @@ -29,17 +29,16 @@ #include #include "libbb.h" -const char *make_human_readable_str(unsigned long size, - unsigned long block_size, - unsigned long display_unit) +const char *make_human_readable_str(unsigned long long size, + unsigned long block_size, unsigned long display_unit) { /* The code will adjust for additional (appended) units. */ static const char zero_and_units[] = { '0', 0, 'k', 'M', 'G', 'T' }; - static const char fmt[] = "%Lu"; - static const char fmt_tenths[] = "%Lu.%d%c"; + static const char fmt[] = "%llu"; + static const char fmt_tenths[] = "%llu.%d%c"; static char str[21]; /* Sufficient for 64 bit unsigned integers. */ - + unsigned long long val; int frac; const char *u; @@ -49,7 +48,7 @@ const char *make_human_readable_str(unsigned long size, f = fmt; frac = 0; - val = ((unsigned long long) size) * block_size; + val = size * block_size; if (val == 0) { return u; } @@ -59,12 +58,13 @@ const char *make_human_readable_str(unsigned long size, val /= display_unit; /* Don't combine with the line above!!! */ } else { ++u; - while ((val >= KILOBYTE) - && (u < zero_and_units + sizeof(zero_and_units) - 1)) { + while ((val >= 1024) + && (u < zero_and_units + sizeof(zero_and_units) - 1) + ) { f = fmt_tenths; ++u; - frac = ((((int)(val % KILOBYTE)) * 10) + (KILOBYTE/2)) / KILOBYTE; - val /= KILOBYTE; + frac = (((int)(val % 1024)) * 10 + 1024/2) / 1024; + val /= 1024; } if (frac >= 10) { /* We need to round up here. */ ++val; @@ -76,7 +76,7 @@ const char *make_human_readable_str(unsigned long size, if ( frac >= 5 ) { ++val; } - f = "%Lu%*c" /* fmt_no_tenths */ ; + f = "%llu%*c" /* fmt_no_tenths */ ; frac = 1; } #endif