aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

human.c (508B)


      1 /* see LICENSE file for copyright and license details */
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 
      6 #include "../util.h"
      7 
      8 char *
      9 humansize(off_t n)
     10 {
     11   static char buf[16];
     12   const char  postfixes[] = "BKMGTPE";
     13   double      size;
     14   int         i;
     15 
     16   for (size = n, i = 0; size >= 1024 && i < (int)strlen(postfixes); i++)
     17     size /= 1024;
     18 
     19   if (!i)
     20     snprintf(buf, sizeof(buf), "%ju", (uintmax_t)n);
     21   else
     22     snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]);
     23 
     24   return buf;
     25 }