human.c (476B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdint.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 < 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 }