summaryrefslogtreecommitdiff
path: root/ubi-utils/src/common.c
diff options
context:
space:
mode:
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>2008-02-06 18:59:15 +0200
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>2008-02-06 18:59:15 +0200
commita71f0ce8c5f80757096f6e4d0ebca27ccbe14da9 (patch)
tree07645e323044fcfb331df157a80a84b624cdab51 /ubi-utils/src/common.c
parentf3d507be006c46870818c549b8f7dc0b4daccf03 (diff)
ubi-tools: improve printing macros
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Diffstat (limited to 'ubi-utils/src/common.c')
-rw-r--r--ubi-utils/src/common.c40
1 files changed, 36 insertions, 4 deletions
diff --git a/ubi-utils/src/common.c b/ubi-utils/src/common.c
index 56244df..fec640d 100644
--- a/ubi-utils/src/common.c
+++ b/ubi-utils/src/common.c
@@ -26,18 +26,17 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
+#include <stdlib.h>
/**
- * ubiutils_bytes_multiplier - convert size specifier to an integer
- * multiplier.
- *
+ * get_multiplier - convert size specifier to an integer multiplier.
* @str: the size specifier string
*
* This function parses the @str size specifier, which may be one of
* 'KiB', 'MiB', or 'GiB' into an integer multiplier. Returns positive
* size multiplier in case of success and %-1 in case of failure.
*/
-int ubiutils_get_multiplier(const char *str)
+static int get_multiplier(const char *str)
{
if (!str)
return 1;
@@ -57,6 +56,39 @@ int ubiutils_get_multiplier(const char *str)
}
/**
+ * ubiutils_get_bytes - convert a string containing amount of bytes into an
+ * integer
+ * @str: string to convert
+ *
+ * This function parses @str which may have an onee of 'KiB', 'MiB', or 'GiB'
+ * size specifiers. Returns positive amount of bytes in case of success and %-1
+ * in case of failure.
+ */
+long long ubiutils_get_bytes(const char *str)
+{
+ char *endp;
+ long long bytes = strtoull(str, &endp, 0);
+
+ if (endp == str || bytes < 0) {
+ fprintf(stderr, "incorrect amount of bytes: \"%s\"", str);
+ return -1;
+ }
+
+ if (*endp != '\0') {
+ int mult = get_multiplier(endp);
+
+ if (mult == -1) {
+ fprintf(stderr, "bad size specifier: \"%s\" - "
+ "should be 'KiB', 'MiB' or 'GiB'", endp);
+ return -1;
+ }
+ bytes *= mult;
+ }
+
+ return bytes;
+}
+
+/**
* ubiutils_print_bytes - print bytes.
* @bytes: variable to print
* @bracket: whether brackets have to be put or not