Mercurial > hg > toybox
comparison lib/lib.c @ 1276:d48bdc1cb017 draft
Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 06 May 2014 06:31:28 -0500 |
parents | 79e847fec774 |
children | 313980d3d78c |
comparison
equal
deleted
inserted
replaced
1275:6f18a0845db3 | 1276:d48bdc1cb017 |
---|---|
781 if (callback(u, *curname)) break; | 781 if (callback(u, *curname)) break; |
782 if (*curname) break; | 782 if (*curname) break; |
783 } | 783 } |
784 closedir(dp); | 784 closedir(dp); |
785 } | 785 } |
786 | |
787 // display first few digits of number with power of two units, except we're | |
788 // actually just counting decimal digits and showing mil/bil/trillions. | |
789 int human_readable(char *buf, unsigned long long num) | |
790 { | |
791 int end, len; | |
792 | |
793 len = sprintf(buf, "%lld", num); | |
794 end = ((len-1)%3)+1; | |
795 len /= 3; | |
796 | |
797 if (len && end == 1) { | |
798 buf[2] = buf[1]; | |
799 buf[1] = '.'; | |
800 end = 3; | |
801 } | |
802 buf[end++] = ' '; | |
803 if (len) buf[end++] = " KMGTPE"[len]; | |
804 buf[end++] = 'B'; | |
805 buf[end++] = 0; | |
806 | |
807 return end; | |
808 } |