X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fstkstring.c;h=4fa5ab14ed551f5a7d5eebb87de49e78ed8ea1df;hb=f0459952ab7a84b2a2e16ee38456087b0986a228;hp=c3b77301adba1744725721b87e7deb8f7af633b8;hpb=a8dfd90a18017f08c95e3b25b00c0c4f5111c49d;p=libucw.git diff --git a/lib/stkstring.c b/lib/stkstring.c index c3b77301..4fa5ab14 100644 --- a/lib/stkstring.c +++ b/lib/stkstring.c @@ -62,6 +62,27 @@ stk_hexdump_internal(char *dst, byte *src, uns n) *dst = 0; } +void +stk_fsize_internal(char *buf, u64 x) +{ + if (x < 1<<10) + sprintf(buf, "%dB", (int)x); + else if (x < 10<<10) + sprintf(buf, "%.1fK", (double)x/(1<<10)); + else if (x < 1<<20) + sprintf(buf, "%dK", (int)(x/(1<<10))); + else if (x < 10<<20) + sprintf(buf, "%.1fM", (double)x/(1<<20)); + else if (x < 1<<30) + sprintf(buf, "%dM", (int)(x/(1<<20))); + else if (x < (u64)10<<30) + sprintf(buf, "%.1fG", (double)x/(1<<30)); + else if (x != ~(u64)0) + sprintf(buf, "%dG", (int)(x/(1<<30))); + else + strcpy(buf, "unknown"); +} + #ifdef TEST int main(void) @@ -76,6 +97,7 @@ int main(void) puts(stk_hexdump(a, 3)); char *ary[] = { "The", "jaws", "that", "bite" }; puts(stk_strjoin(ary, 4, ' ')); + puts(stk_fsize(1234567)); return 0; }