diff options
author | Ashwini Kumar <ak.ashwini@gmail.com> | 2012-08-26 21:17:00 -0500 |
---|---|---|
committer | Ashwini Kumar <ak.ashwini@gmail.com> | 2012-08-26 21:17:00 -0500 |
commit | 1a0eedf795546171a7e84b47f0f02c7d286d2ec4 (patch) | |
tree | e2967e41f07c896bdaee27f3c18f95ee0d7cf20f /lib | |
parent | aba51702273dde01e1473e7f8eaeed5a27d6c861 (diff) | |
download | toybox-1a0eedf795546171a7e84b47f0f02c7d286d2ec4.tar.gz |
Add du command.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lib.c | 27 | ||||
-rw-r--r-- | lib/lib.h | 2 |
2 files changed, 29 insertions, 0 deletions
@@ -1073,3 +1073,30 @@ mode_t string_to_mode(char *modestr, mode_t mode) barf: error_exit("bad mode '%s'", modestr); } + + +char* make_human_readable(unsigned long long size, unsigned long unit) +{ + unsigned int frac = 0; + if(unit) { + size = (size/(unit)) + (size%(unit)?1:0); + return xmsprintf("%llu", size); + } + else { + static char units[] = {'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'}; + int index = 0; + while(size >= 1024) { + frac = size%1024; + size /= 1024; + index++; + } + frac = (frac/102) + ((frac%102)?1:0); + if(frac >= 10) { + size += 1; + frac = 0; + } + if(frac) return xmsprintf("%llu.%u%c", size, frac, units[index]); + else return xmsprintf("%llu%c", size, units[index]); + } + return NULL; //not reached +} @@ -174,3 +174,5 @@ mode_t string_to_mode(char *mode_str, mode_t base); int read_password(char * buff, int buflen, char* mesg); int update_password(char *filename, char* username, char* encrypted); +// du helper functions +char* make_human_readable(unsigned long long size, unsigned long unit); |