@@ -213,6 +213,96 @@ do { \
printf("%s %s\n", PROGRAM_NAME, VERSION); \
} while (0)
+/**
+ * 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.
+ */
+static inline int get_multiplier(const char *str)
+{
+ if (!str)
+ return 1;
+
+ /* Remove spaces before the specifier */
+ while (*str == ' ' || *str == '\t')
+ str += 1;
+
+ if (!strcmp(str, "KiB"))
+ return 1024;
+ if (!strcmp(str, "MiB"))
+ return 1024 * 1024;
+ if (!strcmp(str, "GiB"))
+ return 1024 * 1024 * 1024;
+
+ return -1;
+}
+
+/**
+ * parse_bytes - convert a string containing amount of bytes into an
+ * integer
+ * @str: string to convert
+ *
+ * This function parses @str which may have one of 'KiB', 'MiB', or 'GiB'
+ * size specifiers. Returns positive amount of bytes in case of success and %-1
+ * in case of failure.
+ */
+static inline long long parse_bytes(const char *str)
+{
+ char *endp;
+ long long bytes = strtoull(str, &endp, 0);
+
+ if (endp == str || bytes < 0)
+ return errmsg("incorrect amount of bytes: \"%s\"\n", str);
+
+ if (*endp != '\0') {
+ int mult = get_multiplier(endp);
+
+ if (mult == -1)
+ return errmsg("bad size specifier: \"%s\" - "
+ "should be 'KiB', 'MiB' or 'GiB'\n", endp);
+ bytes *= mult;
+ }
+
+ return bytes;
+}
+
+/**
+ * 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.
+ */
+static inline void 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 && bytes != 0)
+ printf("%s%.1f KiB", p, (double)bytes / 1024);
+ else
+ return;
+
+ if (bracket)
+ printf(")");
+}
+
#include "xalloc.h"
#ifdef __cplusplus
@@ -23,8 +23,6 @@
extern "C" {
#endif
-long long ubiutils_get_bytes(const char *str);
-void ubiutils_print_bytes(long long bytes, int bracket);
void ubiutils_print_text(FILE *stream, const char *txt, int len);
int ubiutils_srand(void);
@@ -172,7 +172,7 @@ static void print_ubi_info(const struct mtd_info *mtd_info,
printf("Default UBI VID header offset: %d\n", ui.vid_hdr_offs);
printf("Default UBI data offset: %d\n", ui.data_offs);
printf("Default UBI LEB size: ");
- ubiutils_print_bytes(ui.leb_size, 0);
+ print_bytes(ui.leb_size, 0);
printf("\n");
printf("Maximum UBI volumes count: %d\n", ui.max_volumes);
}
@@ -306,10 +306,10 @@ static int print_dev_info(libmtd_t libmtd, const struct mtd_info *mtd_info, int
printf("Name: %s\n", mtd.name);
printf("Type: %s\n", mtd.type_str);
printf("Eraseblock size: ");
- ubiutils_print_bytes(mtd.eb_size, 0);
+ print_bytes(mtd.eb_size, 0);
printf("\n");
printf("Amount of eraseblocks: %d (", mtd.eb_cnt);
- ubiutils_print_bytes(mtd.size, 0);
+ print_bytes(mtd.size, 0);
printf(")\n");
printf("Minimum input/output unit size: %d %s\n",
mtd.min_io_size, mtd.min_io_size > 1 ? "bytes" : "byte");
@@ -238,11 +238,11 @@ int main(int argc, char * const argv[])
}
printf("UBI device number %d, total %d LEBs (", dev_info.dev_num, dev_info.total_lebs);
- ubiutils_print_bytes(dev_info.total_bytes, 0);
+ print_bytes(dev_info.total_bytes, 0);
printf("), available %d LEBs (", dev_info.avail_lebs);
- ubiutils_print_bytes(dev_info.avail_bytes, 0);
+ print_bytes(dev_info.avail_bytes, 0);
printf("), LEB size ");
- ubiutils_print_bytes(dev_info.leb_size, 1);
+ print_bytes(dev_info.leb_size, 1);
printf("\n");
libubi_close(libubi);
@@ -143,7 +143,7 @@ static int parse_opt(int argc, char * const argv[])
switch (key) {
case 's':
- args.subpage_size = ubiutils_get_bytes(optarg);
+ args.subpage_size = parse_bytes(optarg);
if (args.subpage_size <= 0)
return errmsg("bad sub-page size: \"%s\"", optarg);
if (!is_power_of_2(args.subpage_size))
@@ -170,7 +170,7 @@ static int parse_opt(int argc, char * const argv[])
break;
case 'S':
- args.image_sz = ubiutils_get_bytes(optarg);
+ args.image_sz = parse_bytes(optarg);
if (args.image_sz <= 0)
return errmsg("bad image-size: \"%s\"", optarg);
break;
@@ -791,9 +791,9 @@ int main(int argc, char * const argv[])
if (!args.quiet) {
normsg_cont("mtd%d (%s), size ", mtd.mtd_num, mtd.type_str);
- ubiutils_print_bytes(mtd.size, 1);
+ print_bytes(mtd.size, 1);
printf(", %d eraseblocks of ", mtd.eb_cnt);
- ubiutils_print_bytes(mtd.eb_size, 1);
+ print_bytes(mtd.eb_size, 1);
printf(", min. I/O size %d bytes\n", mtd.min_io_size);
}
@@ -137,7 +137,7 @@ static int parse_opt(int argc, char * const argv[])
break;
case 's':
- args.bytes = ubiutils_get_bytes(optarg);
+ args.bytes = parse_bytes(optarg);
if (args.bytes <= 0)
return errmsg("bad volume size: \"%s\"", optarg);
break;
@@ -278,9 +278,9 @@ int main(int argc, char * const argv[])
}
printf("Volume ID %d, size %d LEBs (", vol_info.vol_id, vol_info.rsvd_lebs);
- ubiutils_print_bytes(vol_info.rsvd_bytes, 0);
+ print_bytes(vol_info.rsvd_bytes, 0);
printf("), LEB size ");
- ubiutils_print_bytes(vol_info.leb_size, 1);
+ print_bytes(vol_info.leb_size, 1);
printf(", %s, name \"%s\", alignment %d\n",
req.vol_type == UBI_DYNAMIC_VOLUME ? "dynamic" : "static",
vol_info.name, vol_info.alignment);
@@ -211,12 +211,12 @@ static int print_vol_info(libubi_t libubi, int dev_num, int vol_id)
printf("Alignment: %d\n", vol_info.alignment);
printf("Size: %d LEBs (", vol_info.rsvd_lebs);
- ubiutils_print_bytes(vol_info.rsvd_bytes, 0);
+ print_bytes(vol_info.rsvd_bytes, 0);
printf(")\n");
if (vol_info.type == UBI_STATIC_VOLUME) {
printf("Data bytes: ");
- ubiutils_print_bytes(vol_info.data_bytes, 1);
+ print_bytes(vol_info.data_bytes, 1);
printf("\n");
}
printf("State: %s\n", vol_info.corrupted ? "corrupted" : "OK");
@@ -240,15 +240,15 @@ static int print_dev_info(libubi_t libubi, int dev_num, int all)
printf("ubi%d\n", dev_info.dev_num);
printf("Volumes count: %d\n", dev_info.vol_count);
printf("Logical eraseblock size: ");
- ubiutils_print_bytes(dev_info.leb_size, 0);
+ print_bytes(dev_info.leb_size, 0);
printf("\n");
printf("Total amount of logical eraseblocks: %d (", dev_info.total_lebs);
- ubiutils_print_bytes(dev_info.total_bytes, 0);
+ print_bytes(dev_info.total_bytes, 0);
printf(")\n");
printf("Amount of available logical eraseblocks: %d (", dev_info.avail_lebs);
- ubiutils_print_bytes(dev_info.avail_bytes, 0);
+ print_bytes(dev_info.avail_bytes, 0);
printf(")\n");
printf("Maximum count of volumes %d\n", dev_info.max_vol_count);
@@ -176,13 +176,13 @@ static int parse_opt(int argc, char * const argv[])
break;
case 'p':
- args.peb_size = ubiutils_get_bytes(optarg);
+ args.peb_size = parse_bytes(optarg);
if (args.peb_size <= 0)
return errmsg("bad physical eraseblock size: \"%s\"", optarg);
break;
case 'm':
- args.min_io_size = ubiutils_get_bytes(optarg);
+ args.min_io_size = parse_bytes(optarg);
if (args.min_io_size <= 0)
return errmsg("bad min. I/O unit size: \"%s\"", optarg);
if (!is_power_of_2(args.min_io_size))
@@ -190,7 +190,7 @@ static int parse_opt(int argc, char * const argv[])
break;
case 's':
- args.subpage_size = ubiutils_get_bytes(optarg);
+ args.subpage_size = parse_bytes(optarg);
if (args.subpage_size <= 0)
return errmsg("bad sub-page size: \"%s\"", optarg);
if (!is_power_of_2(args.subpage_size))
@@ -368,7 +368,7 @@ static int read_section(const struct ubigen_info *ui, const char *sname,
sprintf(buf, "%s:vol_size", sname);
p = iniparser_getstring(args.dict, buf, NULL);
if (p) {
- vi->bytes = ubiutils_get_bytes(p);
+ vi->bytes = parse_bytes(p);
if (vi->bytes <= 0)
return errmsg("bad \"vol_size\" key value \"%s\" (section \"%s\")",
p, sname);
@@ -397,7 +397,7 @@ static int read_section(const struct ubigen_info *ui, const char *sname,
normsg_cont("volume size was not specified in section \"%s\", assume"
" minimum to fit image \"%s\"", sname, *img);
- ubiutils_print_bytes(vi->bytes, 1);
+ print_bytes(vi->bytes, 1);
printf("\n");
}
@@ -114,7 +114,7 @@ static int parse_opt(int argc, char * const argv[])
switch (key) {
case 's':
- args.bytes = ubiutils_get_bytes(optarg);
+ args.bytes = parse_bytes(optarg);
if (args.bytes <= 0)
return errmsg("bad volume size: \"%s\"", optarg);
break;
@@ -34,99 +34,6 @@
#include <unistd.h>
#include "common.h"
-/**
- * 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.
- */
-static int get_multiplier(const char *str)
-{
- if (!str)
- return 1;
-
- /* Remove spaces before the specifier */
- while (*str == ' ' || *str == '\t')
- str += 1;
-
- if (!strcmp(str, "KiB"))
- return 1024;
- if (!strcmp(str, "MiB"))
- return 1024 * 1024;
- if (!strcmp(str, "GiB"))
- return 1024 * 1024 * 1024;
-
- return -1;
-}
-
-/**
- * ubiutils_get_bytes - convert a string containing amount of bytes into an
- * integer
- * @str: string to convert
- *
- * This function parses @str which may have one 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\"\n", 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'\n", 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
- *
- * 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 && bytes != 0)
- printf("%s%.1f KiB", p, (double)bytes / 1024);
- else
- return;
-
- if (bracket)
- printf(")");
-}
/**
* ubiutils_print_text - print text and fold it.
@@ -390,62 +390,6 @@ static int validate_options(void)
return 0;
}
-/**
- * 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.
- */
-static int get_multiplier(const char *str)
-{
- if (!str)
- return 1;
-
- /* Remove spaces before the specifier */
- while (*str == ' ' || *str == '\t')
- str += 1;
-
- if (!strcmp(str, "KiB"))
- return 1024;
- if (!strcmp(str, "MiB"))
- return 1024 * 1024;
- if (!strcmp(str, "GiB"))
- return 1024 * 1024 * 1024;
-
- return -1;
-}
-
-/**
- * get_bytes - convert a string containing amount of bytes into an
- * integer.
- * @str: string to convert
- *
- * This function parses @str which may have one of 'KiB', 'MiB', or 'GiB' size
- * specifiers. Returns positive amount of bytes in case of success and %-1 in
- * case of failure.
- */
-static long long get_bytes(const char *str)
-{
- char *endp;
- long long bytes = strtoull(str, &endp, 0);
-
- if (endp == str || bytes < 0)
- return err_msg("incorrect amount of bytes: \"%s\"", str);
-
- if (*endp != '\0') {
- int mult = get_multiplier(endp);
-
- if (mult == -1)
- return err_msg("bad size specifier: \"%s\" - "
- "should be 'KiB', 'MiB' or 'GiB'", endp);
- bytes *= mult;
- }
-
- return bytes;
-}
-
static int get_options(int argc, char**argv)
{
int opt, i;
@@ -495,17 +439,17 @@ static int get_options(int argc, char**argv)
root);
break;
case 'm':
- c->min_io_size = get_bytes(optarg);
+ c->min_io_size = parse_bytes(optarg);
if (c->min_io_size <= 0)
return err_msg("bad min. I/O size");
break;
case 'e':
- c->leb_size = get_bytes(optarg);
+ c->leb_size = parse_bytes(optarg);
if (c->leb_size <= 0)
return err_msg("bad LEB size");
break;
case 'c':
- c->max_leb_cnt = get_bytes(optarg);
+ c->max_leb_cnt = parse_bytes(optarg);
if (c->max_leb_cnt <= 0)
return err_msg("bad maximum LEB count");
break;
@@ -595,12 +539,12 @@ static int get_options(int argc, char**argv)
#endif
break;
case 'j':
- c->max_bud_bytes = get_bytes(optarg);
+ c->max_bud_bytes = parse_bytes(optarg);
if (c->max_bud_bytes <= 0)
return err_msg("bad maximum amount of buds");
break;
case 'R':
- c->rp_size = get_bytes(optarg);
+ c->rp_size = parse_bytes(optarg);
if (c->rp_size < 0)
return err_msg("bad reserved bytes count");
break;