summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMax Zerzouri <maxdamantus@gmail.com>2014-03-20 16:19:47 +1300
committerTingPing <tingping@tingping.se>2014-04-19 22:49:45 -0400
commit231590f71e7a5dc58ba7fc13cbe6bd487d560e10 (patch)
tree3e10a7bc89ed09c5cc05f99661276bcc8936b69c
parent7e55e4ee73be66ade147600de1fe8305f083d680 (diff)
sysinfo: Use less ambiguous IEC prefixes in storage measurements
Closes #922
-rw-r--r--plugins/sysinfo/match.c19
1 files changed, 7 insertions, 12 deletions
diff --git a/plugins/sysinfo/match.c b/plugins/sysinfo/match.c
index 2853aab6..adfbff1b 100644
--- a/plugins/sysinfo/match.c
+++ b/plugins/sysinfo/match.c
@@ -32,36 +32,31 @@ float percentage(unsigned long long *free, unsigned long long *total)
 
 char *pretty_freespace(const char *desc, unsigned long long *free_k, unsigned long long *total_k)
 {
-        char *result, *bytesize;
+        char *result, **quantity;
 	double free_space, total_space;
 	free_space = *free_k;
 	total_space = *total_k;
         result = malloc(bsize * sizeof(char));
-	const char *quantities = "KB\0MB\0GB\0TB\0PB\0EB\0ZB\0YB\0";
-	int i=0;
+	char *quantities[] = { "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", 0 };
 	if (total_space == 0)
 	{
 		snprintf(result, bsize, "%s: none", desc);
 		return result;
 	}
-        bytesize = malloc(3 * sizeof(char));
-	while (total_space > 1023 && i <= 14)
+        quantity = quantities;
+	while (total_space > 1023 && *(quantity + 1))
 	{
-		i=i+3;
-		*bytesize=*(quantities+i);
-		*(bytesize+1)=*(quantities+i+1);
-		*(bytesize+2)=*(quantities+i+2);
+		quantity++;
 		free_space = free_space / 1024;
 		total_space = total_space / 1024;
 	}
 	if (sysinfo_get_percent () != 0)
 		snprintf(result, bsize, "%s: %.1f%s, %.1f%% free",
-		desc, total_space, bytesize,
+		desc, total_space, *quantity,
 		percentage(free_k, total_k));
 	else
 		snprintf(result, bsize, "%s: %.1f%s/%.1f%s free",
-		desc, free_space, bytesize, total_space, bytesize);
-	free (bytesize);
+		desc, free_space, *quantity, total_space, *quantity);
         return result;
 }