[PATCH 3/3] xfsprogs: xfs_quota: improve calculation for percentage display

Alex Elder aelder at sgi.com
Wed Aug 24 16:53:43 CDT 2011


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 at sgi.com>
---
 quota/util.c |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/quota/util.c b/quota/util.c
index 18ccae2..179aafd 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 *
-- 
1.7.6




More information about the xfs mailing list