As strange as it might sound it could indeed be improved.
I think it should list exabytes though.

We should probably change the off_t type (to unsigned?) and the calculation
using "double size".

Then listing a directory with multiple exabyte-sized files will be 0.00001%
faster also ;)

On Fri, Feb 06, 2026 at 12:32:33PM +0100, Arusekk wrote:
> While it might sound silly, who knows what happens some day.
> Individual data centers are moving beyond exabytes,
> and RISC-V already specifies a 128-bit architecture variant.
> Alternatively, someone might decide that multiple exabytes
> should be reported in terabytes instead (corectness improves
> hackability).
> 
> I tested the patch by changing the constant "BKMGTPE" to just "BKMGT"
> and running:
> 
> $ ./truncate -s 1E /tmp/zeros
> $ ./ls -lh /tmp/zeros
> 
> Before (incorrect, 1 EiB != 1024.0 B):
> -rw-r--r--    1 arusekk  arusekk      1024.0 Feb 06 11:53 /tmp/zeros
> After (corrected, 1 EiB == 1048576 TiB):
> -rw-r--r--    1 arusekk  arusekk  1048576.0T Feb 06 11:53 /tmp/zeros
> 
> A side effect is less memory accesses/calls, so listing a large
> directory could become marginally faster.
> ---
>  libutil/human.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/libutil/human.c b/libutil/human.c
> index 7e39ba5..0f80b9b 100644
> --- a/libutil/human.c
> +++ b/libutil/human.c
> @@ -1,7 +1,7 @@
>  /* See LICENSE file for copyright and license details. */
> +#include <stdint.h>
>  #include <stdio.h>
>  #include <string.h>
> -#include <stdint.h>
>  
>  #include "../util.h"
>  
> @@ -13,7 +13,7 @@ humansize(off_t n)
>       double size;
>       int i;
>  
> -     for (size = n, i = 0; size >= 1024 && i < strlen(postfixes); i++)
> +     for (size = n, i = 0; size >= 1024 && postfixes[i+1]; i++)
>               size /= 1024;
>  
>       if (!i)
> -- 
> 2.52.0
> 
> 

-- 
Kind regards,
Hiltjo

Reply via email to