summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorMaciej Naruszewicz <maciej.naruszewicz@intel.com>2012-10-02 08:40:11 +0200
committerNeilBrown <neilb@suse.de>2012-10-02 08:40:11 +0200
commit570abc6f3881b5152cb1244d5e6afcc421c5a4ce (patch)
tree6cf23dffeb323eeff05a14e2f4aff99c2d900413 /util.c
parentFix return code for --detail-platform (diff)
downloadmdadm-570abc6f3881b5152cb1244d5e6afcc421c5a4ce.tar.xz
mdadm-570abc6f3881b5152cb1244d5e6afcc421c5a4ce.zip
Synchronize size calculation in human_size and human_size_brief
It would be better if two size-calculating methods had the same calculating algorithm. The human_size way of calculation seems more readable, so let's use it for both methods. Signed-off-by: Maciej Naruszewicz <maciej.naruszewicz@intel.com> Signed-off-by: NeilBrown <neilb@suse.de>
Diffstat (limited to 'util.c')
-rw-r--r--util.c33
1 files changed, 20 insertions, 13 deletions
diff --git a/util.c b/util.c
index c63a232c..09971a29 100644
--- a/util.c
+++ b/util.c
@@ -686,20 +686,27 @@ char *human_size_brief(long long bytes)
{
static char buf[30];
+ /* We convert bytes to either centi-M{ega,ibi}bytes or
+ * centi-G{igi,ibi}bytes, with appropriate rounding,
+ * and then print 1/100th of those as a decimal.
+ * We allow upto 2048Megabytes before converting to
+ * gigabytes, as that shows more precision and isn't
+ * too large a number.
+ * Terabytes are not yet handled.
+ */
+
if (bytes < 5000*1024)
- snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
- (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
- );
- else if (bytes < 2*1024LL*1024LL*1024LL)
- snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
- (long)(bytes>>20),
- (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
- );
- else
- snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
- (long)(bytes>>30),
- (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
- );
+ buf[0] = 0;
+ else if (bytes < 2*1024LL*1024LL*1024LL) {
+ long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
+ snprintf(buf, sizeof(buf), " (%ld.%02ldMiB)",
+ cMiB/100 , cMiB % 100);
+ } else {
+ long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
+ snprintf(buf, sizeof(buf), " (%ld.%02ldGiB)",
+ cGiB/100 , cGiB % 100);
+ }
+
return buf;
}