allow print_size to print large numbers on 32-bit systems
PATCH v3 1/1 allow print_size to print large numbers on 32-bit systems
Modify print_size() so that it can accept numbers larger than 4GB on 32-bit systems.
Add support for display terabyte, petabyte, and exabyte sizes. Change the output to use International Electrotechnical Commission binary prefix standard.
Signed-off-by Timur Tabi <[email protected]>
--- lib_generic/display_options.c | 24 ++++++++++++------------ 1 files changed, 12 insertions(+), 12 deletions(-)
lib_generic/display_options.c +13 -13
@@ -39,23 +39,23 @@ int display_options (void) }/* -* print sizes as "xxx kB", "xxx.y kB", "xxx MB", "xxx.y MB", -* xxx GB, or xxx.y GB as needed; allow for optional trailing string +* print sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB", +* xxx GiB, xxx.y GiB, etc as needed; allow for optional trailing string * (like "\n") */ void print_size (unsigned long long size,const char *s) {unsigned long m =0 , n; -unsigned long long d =1 <<30 ;/* 1 GB */ -char c ='G' ; - -if (size < d) {/* try MB */ - c ='M' ; - d =1 <<20 ; -if (size < d) {/* print in kB */ - c ='k' ; - d =1 <<10 ; - } +static const char names[] = {'K' ,'M' ,'G' ,'T' ,'P' ,'E' }; +unsigned long long d; +char c; +unsigned int i; + +for (i =ARRAY_SIZE (names); i >0 ; i--) { + d =1ULL << (10 * i); + c = names[i -1 ]; +if (size >= d) +break ; } n = size / d;
@@ -74,7 +74,7 @@ void print_size(unsigned long long size, const char *s)if (m) {printf (".%ld" , m); } -printf (" %cB%s" , c, s); +printf (" %ciB%s" , c, s); }/*
Signature · 1 line
1.6.5