From: Alex Elder Date: Wed, 24 Aug 2011 21:53:43 +0000 (+0000) Subject: xfsprogs: xfs_quota: improve calculation for percentage display X-Git-Tag: v3.1.6~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=481d1f83a1e32ee44a3abae75c55b7bdc1136392;p=thirdparty%2Fxfsprogs-dev.git xfsprogs: xfs_quota: improve calculation for percentage display The pct_to_string() function determines the percentage it produces in a strange way. Simplify the function, and make it return the simple rounded percentage value. Handle the case of an error return from snprintf() as well. Signed-off-by: Alex Elder Reviewed-by: Christoph Hellwig --- diff --git a/quota/util.c b/quota/util.c index 18ccae259..179aafd2c 100644 --- a/quota/util.c +++ b/quota/util.c @@ -172,18 +172,18 @@ num_to_string( char * pct_to_string( - __uint64_t v, - __uint64_t t, - char *sp, + __uint64_t portion, + __uint64_t whole, + char *buf, uint size) { - if (t == 0 || v == 0) - snprintf(sp, size, "%3u", (uint)0); - else if (t == v) - snprintf(sp, size, "%3u", (uint)100); - else - snprintf(sp, size, "%3u", (uint)(((double)v / t) * 100 + 1)); - return sp; + uint percent; + + percent = whole ? (uint) (100.0 * portion / whole + 0.5) : 0; + if (snprintf(buf, size, "%3u", percent) < 0) + return "???"; + + return buf; } char *