From 810bf9488fe74c90258b542ba2cb0d130e3785dd Mon Sep 17 00:00:00 2001 From: Jan Kara Date: Wed, 10 Jan 2018 09:54:34 +0100 Subject: Implement manual choice of output unit Add an optional argument to option -s allowing user to manually specify units used for outputting results. Signed-off-by: Jan Kara --- quotasys.c | 86 +++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 32 deletions(-) (limited to 'quotasys.c') diff --git a/quotasys.c b/quotasys.c index 52938ac..9336411 100644 --- a/quotasys.c +++ b/quotasys.c @@ -18,8 +18,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -532,30 +532,39 @@ int str2timeunits(time_t num, char *unit, time_t *res) /* * Convert number in quota blocks to some nice short form for printing */ -void space2str(qsize_t space, char *buf, int format) +void space2str(qsize_t space, char *buf, enum s2s_unit format) { int i; - char suffix[8] = " MGT"; - qsize_t aspace = space >= 0 ? space : -space; - - space = qb2kb(space); - if (format) { - for (i = 3; i > 0; i--) { - long long unit = 1LL << (QUOTABLOCK_BITS*i); - - if (aspace >= unit * 100) { - int sign = aspace != space ? -1 : 1; + char suffix[8] = "KMGT"; + qsize_t aspace; + int sign = 1; + long long unit; + + aspace = space = qb2kb(space); + if (format == S2S_NONE) { + sprintf(buf, "%lld", (long long)space); + return; + } + if (aspace < 0) { + aspace = -space; + sign = -1; + } + if (format == S2S_AUTO) { + for (i = 3; i >= 0; i--) { + unit = 1LL << (QUOTABLOCK_BITS*i); - sprintf(buf, "%lld%c", (long long) - DIV_ROUND_UP(aspace, unit) * sign, - suffix[i]); - return; - } + if (aspace >= unit * 100) + goto print; } - sprintf(buf, "%lldK", (long long)space); - return; + unit = 1; + i = 0; + } else { + i = format - S2S_KB; + unit = 1LL << (QUOTABLOCK_BITS*i); } - sprintf(buf, "%lld", (long long)space); +print: + sprintf(buf, "%lld%c", (long long)DIV_ROUND_UP(aspace, unit) * sign, + suffix[i]); } /* @@ -593,25 +602,38 @@ const char *str2space(const char *string, qsize_t *space) /* * Convert number to some nice short form for printing */ -void number2str(long long num, char *buf, int format) +void number2str(long long num, char *buf, enum s2s_unit format) { int i; - unsigned long long div; + unsigned long long div;; char suffix[8] = " kmgt"; + long long anum = num; + int sign = 1; - if (format) { - long long anum = num >= 0 ? num : -num; - int sign = num != anum ? -1 : 1; + if (format == S2S_NONE) { + sprintf(buf, "%lld", num); + return; + } + if (num < 0) { + anum = -num; + sign = -1; + } + if (format == S2S_AUTO) { for (i = 4, div = 1000000000000LL; i > 0; i--, div /= 1000) - if (anum >= 100*div) { - sprintf(buf, "%lld%c", - DIV_ROUND_UP(anum, div) * sign, - suffix[i]); - return; - } + if (anum >= 100*div) + break; + } else { + div = 1; + for (i = 0; i < format - S2S_NONE; i++) + div *= 1000; + } + if (suffix[i] != ' ') { + sprintf(buf, "%lld%c", DIV_ROUND_UP(anum, div) * sign, + suffix[i]); + } else { + sprintf(buf, "%lld", DIV_ROUND_UP(anum, div) * sign); } - sprintf(buf, "%lld", num); } /* -- cgit v1.2.3