comparison lib/lib.c @ 658:2b957eaa00c7

Add du command.
author Ashwini Kumar <ak.ashwini@gmail.com>
date Sun, 26 Aug 2012 21:17:00 -0500
parents 7bdebd2af1d6
children e4b96507688a
comparison
equal deleted inserted replaced
657:7f5db1d707c0 658:2b957eaa00c7
1071 } 1071 }
1072 return mode; 1072 return mode;
1073 barf: 1073 barf:
1074 error_exit("bad mode '%s'", modestr); 1074 error_exit("bad mode '%s'", modestr);
1075 } 1075 }
1076
1077
1078 char* make_human_readable(unsigned long long size, unsigned long unit)
1079 {
1080 unsigned int frac = 0;
1081 if(unit) {
1082 size = (size/(unit)) + (size%(unit)?1:0);
1083 return xmsprintf("%llu", size);
1084 }
1085 else {
1086 static char units[] = {'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
1087 int index = 0;
1088 while(size >= 1024) {
1089 frac = size%1024;
1090 size /= 1024;
1091 index++;
1092 }
1093 frac = (frac/102) + ((frac%102)?1:0);
1094 if(frac >= 10) {
1095 size += 1;
1096 frac = 0;
1097 }
1098 if(frac) return xmsprintf("%llu.%u%c", size, frac, units[index]);
1099 else return xmsprintf("%llu%c", size, units[index]);
1100 }
1101 return NULL; //not reached
1102 }