]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfsprogs: xfs_quota: improve calculation for percentage display
authorAlex Elder <aelder@sgi.com>
Wed, 24 Aug 2011 21:53:43 +0000 (21:53 +0000)
committerAlex Elder <aelder@sgi.com>
Fri, 26 Aug 2011 01:40:38 +0000 (20:40 -0500)
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 <aelder@sgi.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
quota/util.c

index 18ccae259291fd0f8ae8664e3963b951c4f33477..179aafd2c55147382ca90437b98f68f6faf99edb 100644 (file)
@@ -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 *