From c036f9958b3719a949999ca4fe9866ba56b3f713 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Sat, 22 Dec 2007 15:43:19 +0200 Subject: 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 --- ubi-utils/src/common.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'ubi-utils/src/common.c') 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 #include /** @@ -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(")"); +} -- cgit v1.2.3