(size_trunc_len): support automatic maximum unit for specified size.

Fix typos.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2013-01-21 15:02:58 +04:00
parent 22f0cac4ea
commit ef94b84eaf
1 changed files with 13 additions and 2 deletions

View File

@ -406,6 +406,7 @@ size_trunc_sep (uintmax_t size, gboolean use_si)
*
* Units: size units (filesystem sizes are 1K blocks)
* 0=bytes, 1=Kbytes, 2=Mbytes, etc.
* -1 means maximum possible unit for specified size
*/
void
@ -425,7 +426,7 @@ size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gbool
10000000ULL,
100000000ULL,
1000000000ULL
/* maximmum value of uintmax_t (in case of 4 bytes) is
/* maximum value of uintmax_t (in case of 4 bytes) is
4294967295
*/
#if SIZEOF_UINTMAX_T == 8
@ -440,7 +441,7 @@ size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gbool
100000000000000000ULL,
1000000000000000000ULL,
10000000000000000000ULL
/* maximmum value of uintmax_t (in case of 8 bytes) is
/* maximum value of uintmax_t (in case of 8 bytes) is
18447644073710439615
*/
#endif
@ -462,6 +463,16 @@ size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gbool
len = 9;
#endif
/* find maximum unit */
if (units < 0)
{
const unsigned int divider = use_si ? 1000 : 1024;
uintmax_t size_remain = size;
for (units = 0; size_remain >= divider; units++)
size_remain /= divider;
}
/*
* recalculate from 1024 base to 1000 base if units>0
* We can't just multiply by 1024 - that might cause overflow