summaryrefslogtreecommitdiff
path: root/ubi-utils/src/common.c
diff options
context:
space:
mode:
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>2007-12-22 15:43:19 +0200
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>2007-12-22 17:49:58 +0200
commitc036f9958b3719a949999ca4fe9866ba56b3f713 (patch)
treec223609251d68d34b57d6b2989451d51960f1580 /ubi-utils/src/common.c
parentc4d478578c7a1343ad053fca79d80fdc4962ca6f (diff)
ubi-utils: introduce ubinfo utility
Add new handy UBI utility which prints various type of UBI information. This commit also includes a lot of fixes and cleanups in libubi, and other utilities. It was quite complex to separate this all out and I figured that nobody anyway would really need this, and decided to save my time for more useful things. Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Diffstat (limited to 'ubi-utils/src/common.c')
-rw-r--r--ubi-utils/src/common.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/ubi-utils/src/common.c b/ubi-utils/src/common.c
index 4a56128..761d47d 100644
--- a/ubi-utils/src/common.c
+++ b/ubi-utils/src/common.c
@@ -20,6 +20,7 @@
* This file contains various common stuff used by UBI utilities.
*/
+#include <stdio.h>
#include <string.h>
/**
@@ -50,3 +51,37 @@ int ubiutils_get_multiplier(const char *str)
return -1;
}
+
+/**
+ * ubiutils_print_bytes - print bytes.
+ * @bytes: variable to print
+ * @bracket: whether brackets have to be put or not
+ *
+ * This is a helper function which prints amount of bytes in a human-readable
+ * form, i.e., it prints the exact amount of bytes following by the approximate
+ * amount of Kilobytes, Megabytes, or Gigabytes, depending on how big @bytes
+ * is.
+ */
+void ubiutils_print_bytes(long long bytes, int bracket)
+{
+ const char *p;
+
+ if (bracket)
+ p = " (";
+ else
+ p = ", ";
+
+ printf("%lld bytes", bytes);
+
+ if (bytes > 1024 * 1024 * 1024)
+ printf("%s%.1f GiB", p, (double)bytes / (1024 * 1024 * 1024));
+ else if (bytes > 1024 * 1024)
+ printf("%s%.1f MiB", p, (double)bytes / (1024 * 1024));
+ else if (bytes > 1024)
+ printf("%s%.1f KiB", p, (double)bytes / 1024);
+ else
+ return;
+
+ if (bracket)
+ printf(")");
+}