]> www.infradead.org Git - mtd-utils.git/commitdiff
ubi-tools: improve printing macros
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Wed, 6 Feb 2008 16:59:15 +0000 (18:59 +0200)
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Wed, 6 Feb 2008 16:59:15 +0000 (18:59 +0200)
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
13 files changed:
ubi-utils/src/common.c
ubi-utils/src/common.h
ubi-utils/src/libubi.c
ubi-utils/src/libubi_int.h
ubi-utils/src/libubigen.c
ubi-utils/src/ubiattach.c
ubi-utils/src/ubicrc32.c
ubi-utils/src/ubidetach.c
ubi-utils/src/ubimkvol.c
ubi-utils/src/ubinfo.c
ubi-utils/src/ubinize.c
ubi-utils/src/ubirmvol.c
ubi-utils/src/ubiupdate.c

index 56244dfc4501b291b69e27b23cf7730d04d41414..fec640d783b012dcb862ab0d5fd35c1d32a3eeba 100644 (file)
 #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;
@@ -56,6 +55,39 @@ int ubiutils_get_multiplier(const char *str)
        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 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
index 3ee93ff971e8b76c8a5700857d3c39d10018de82..203238e831f72d39ad7af27495d9b5f9356e75d0 100644 (file)
@@ -20,6 +20,8 @@
 #define __UBI_UTILS_COMMON_H__
 
 #include <stdio.h>
+#include <string.h>
+#include <errno.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -40,16 +42,25 @@ extern "C" {
 } while(0)
 
 /* Error messages */
-#define errmsg(fmt, ...) do {                                             \
+#define errmsg(fmt, ...)  ({                                              \
        fprintf(stderr, PROGRAM_NAME " error: " fmt "\n", ##__VA_ARGS__); \
-} while(0)
+       -1;                                                               \
+})
+
+/* System error messages */
+#define sys_errmsg(fmt, ...)  ({                                          \
+       int _err = errno;                                                 \
+       fprintf(stderr, PROGRAM_NAME " error: " fmt "\n", ##__VA_ARGS__); \
+       fprintf(stderr, "error %d (%s)", _err, strerror(_err));           \
+       -1;                                                               \
+})
 
 /* Warnings */
 #define warnmsg(fmt, ...) do {                                              \
        fprintf(stderr, PROGRAM_NAME " warning: " fmt "\n", ##__VA_ARGS__); \
 } while(0)
 
-int ubiutils_get_multiplier(const char *str);
+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);
 
index 1cfefab1f1c4207f0e4cc8150319e64946d75ae0..b53f18c0db92d3c41cd82d9f35cdc8cc4ffe4eb7 100644 (file)
@@ -27,7 +27,6 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <dirent.h>
-#include <errno.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <limits.h>
@@ -50,8 +49,7 @@ static char *mkpath(const char *path, const char *name)
 
        n = malloc(len1 + len2 + 2);
        if (!n) {
-               errmsg("cannot allocate %d bytes", len1 + len2 + 2);
-               perror("malloc");
+               sys_errmsg("cannot allocate %d bytes", len1 + len2 + 2);
                return NULL;
        }
 
@@ -83,8 +81,7 @@ static int read_positive_ll(const char *file, long long *value)
 
        rd = read(fd, buf, 50);
        if (rd == -1) {
-               errmsg("cannot read \"%s\"", file);
-               perror("read");
+               sys_errmsg("cannot read \"%s\"", file);
                goto out_error;
        }
        if (rd == 50) {
@@ -106,11 +103,8 @@ static int read_positive_ll(const char *file, long long *value)
                goto out_error;
        }
 
-       if (close(fd)) {
-               errmsg("close failed on \"%s\"", file);
-               perror("close");
-               return -1;
-       }
+       if (close(fd))
+               return sys_errmsg("close failed on \"%s\"", file);
 
        return 0;
 
@@ -166,16 +160,14 @@ static int read_data(const char *file, void *buf, int buf_len)
 
        rd = read(fd, buf, buf_len);
        if (rd == -1) {
-               errmsg("cannot read \"%s\"", file);
-               perror("read");
+               sys_errmsg("cannot read \"%s\"", file);
                goto out_error;
        }
 
        /* Make sure all data is read */
        tmp1 = read(fd, &tmp, 1);
        if (tmp1 == 1) {
-               errmsg("cannot read \"%s\"", file);
-               perror("read");
+               sys_errmsg("cannot read \"%s\"", file);
                goto out_error;
        }
        if (tmp1) {
@@ -186,8 +178,7 @@ static int read_data(const char *file, void *buf, int buf_len)
        }
 
        if (close(fd)) {
-               errmsg("close failed on \"%s\"", file);
-               perror("close");
+               sys_errmsg("close failed on \"%s\"", file);
                return -1;
        }
 
@@ -217,16 +208,14 @@ static int read_major(const char *file, int *major, int *minor)
 
        ret = sscanf(buf, "%d:%d\n", major, minor);
        if (ret != 2) {
-               errmsg("\"%s\" does not have major:minor format", file);
                errno = EINVAL;
-               return -1;
+               return errmsg("\"%s\" does not have major:minor format", file);
        }
 
        if (*major < 0 || *minor < 0) {
-               errmsg("bad major:minor %d:%d in \"%s\"",
-                      *major, *minor, file);
                errno = EINVAL;
-               return -1;
+               return errmsg("bad major:minor %d:%d in \"%s\"",
+                             *major, *minor, file);
        }
 
        return 0;
@@ -377,18 +366,16 @@ static int vol_node2nums(struct libubi *lib, const char *node, int *dev_num,
                return -1;
 
        if (!S_ISCHR(st.st_mode)) {
-               errmsg("\"%s\" is not a character device", node);
                errno = EINVAL;
-               return -1;
+               return errmsg("\"%s\" is not a character device", node);
        }
 
        major = major(st.st_rdev);
        minor = minor(st.st_rdev);
 
        if (minor == 0) {
-               errmsg("\"%s\" is not a volume character device", node);
                errno = EINVAL;
-               return -1;
+               return errmsg("\"%s\" is not a volume character device", node);
        }
 
        if (ubi_get_info((libubi_t *)lib, &info))
@@ -445,18 +432,16 @@ static int dev_node2num(struct libubi *lib, const char *node, int *dev_num)
                return -1;
 
        if (!S_ISCHR(stat.st_mode)) {
-               errmsg("\"%s\" is not a character device", node);
                errno = EINVAL;
-               return -1;
+               return errmsg("\"%s\" is not a character device", node);
        }
 
        major = major(stat.st_rdev);
        minor = minor(stat.st_rdev);
 
        if (minor != 0) {
-               errmsg("\"%s\" is not an UBI character device", node);
                errno = EINVAL;
-               return -1;
+               return errmsg("\"%s\" is not an UBI character device", node);
        }
 
        if (ubi_get_info((libubi_t *)lib, &info))
@@ -551,8 +536,7 @@ libubi_t libubi_open(void)
        }
 
        if (close(fd)) {
-               errmsg("close failed on \"%s\"", lib->sysfs_ubi);
-               perror("close");
+               sys_errmsg("close failed on \"%s\"", lib->sysfs_ubi);
                goto out_error;
        }
 
@@ -840,11 +824,8 @@ int ubi_get_info(libubi_t desc, struct ubi_info *info)
         * devices are present.
         */
        sysfs_ubi = opendir(lib->sysfs_ubi);
-       if (!sysfs_ubi) {
-               errmsg("cannot open %s", lib->sysfs_ubi);
-               perror("opendir");
-               return -1;
-       }
+       if (!sysfs_ubi)
+               return sys_errmsg("cannot open %s", lib->sysfs_ubi);
 
        info->lowest_dev_num = INT_MAX;
        while (1) {
@@ -874,16 +855,12 @@ int ubi_get_info(libubi_t desc, struct ubi_info *info)
        }
 
        if (!dirent && errno) {
-               errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
-               perror("readdir");
+               sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
                goto out_close;
        }
 
-       if (closedir(sysfs_ubi)) {
-               errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
-               perror("closedir");
-               return -1;
-       }
+       if (closedir(sysfs_ubi))
+               return sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
 
        if (info->lowest_dev_num == INT_MAX)
                info->lowest_dev_num = 0;
@@ -1044,16 +1021,12 @@ int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info)
        }
 
        if (!dirent && errno) {
-               errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
-               perror("readdir");
+               sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
                goto out_close;
        }
 
-       if (closedir(sysfs_ubi)) {
-               errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
-               perror("closedir");
-               return -1;
-       }
+       if (closedir(sysfs_ubi))
+               return sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
 
        if (info->lowest_vol_num == INT_MAX)
                info->lowest_vol_num = 0;
index 649086443ca094bc0c24648f1878ee57fd3ccd7d..5b186a795ac37f78f16036679c4c0a1cfbb3aa32 100644 (file)
 #ifndef __LIBUBI_INT_H__
 #define __LIBUBI_INT_H__
 
+#include <string.h>
+#include <errno.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 /* Error messages */
-#define errmsg(fmt, ...) do {                                      \
+#define errmsg(fmt, ...)  ({                                       \
         fprintf(stderr, "libubi error: " fmt "\n", ##__VA_ARGS__); \
-} while(0)
+       -1;                                                        \
+})
+
+/* System error messages */
+#define sys_errmsg(fmt, ...)  ({                                   \
+       int _err = errno;                                          \
+       fprintf(stderr, "libubi error: " fmt "\n", ##__VA_ARGS__); \
+       fprintf(stderr, "error %d (%s)", _err, strerror(_err));    \
+       -1;                                                        \
+})
 
 /*
  * The below are pre-define UBI file and directory names.
index f7e6d5febca2c1744b5cf862ef6e3328c2d47a2b..8d71fdee4a220aedad6e2ba74ec795240fa49c26 100644 (file)
@@ -114,16 +114,13 @@ int ubigen_add_volume(const struct ubigen_info *ui,
        struct ubi_vtbl_record *vtbl_rec = &vtbl[vi->id];
        uint32_t tmp;
 
-       if (vi->id >= ui->max_volumes) {
-               errmsg("too high volume id %d, max. volumes is %d",
-                      vi->id, ui->max_volumes);
-               return -1;
-       }
-       if (vi->alignment >= ui->leb_size) {
-               errmsg("too large alignment %d, max is %d (LEB size)",
-                      vi->alignment, ui->leb_size);
-               return -1;
-       }
+       if (vi->id >= ui->max_volumes)
+               return errmsg("too high volume id %d, max. volumes is %d",
+                             vi->id, ui->max_volumes);
+
+       if (vi->alignment >= ui->leb_size)
+               return errmsg("too large alignment %d, max is %d (LEB size)",
+                             vi->alignment, ui->leb_size);
 
        memset(vtbl_rec, '\0', sizeof(struct ubi_vtbl_record));
        tmp = (vi->bytes + ui->leb_size - 1) / ui->leb_size;
@@ -225,16 +222,13 @@ int ubigen_write_volume(const struct ubigen_info *ui,
        int len = vi->usable_leb_size, rd, lnum = 0;
        char inbuf[ui->leb_size], outbuf[ui->peb_size];
 
-       if (vi->id >= ui->max_volumes) {
-               errmsg("too high volume id %d, max. volumes is %d",
-                      vi->id, ui->max_volumes);
-               return -1;
-       }
-       if (vi->alignment >= ui->leb_size) {
-               errmsg("too large alignment %d, max is %d (LEB size)",
-                      vi->alignment, ui->leb_size);
-               return -1;
-       }
+       if (vi->id >= ui->max_volumes)
+               return errmsg("too high volume id %d, max. volumes is %d",
+                             vi->id, ui->max_volumes);
+
+       if (vi->alignment >= ui->leb_size)
+               return errmsg("too large alignment %d, max is %d (LEB size)",
+                             vi->alignment, ui->leb_size);
 
        memset(outbuf, 0xFF, ui->data_offs);
        init_ec_hdr(ui, (struct ubi_ec_hdr *)outbuf);
@@ -252,11 +246,9 @@ int ubigen_write_volume(const struct ubigen_info *ui,
                        rd = fread(inbuf + len - l, 1, l, in);
                        if (rd == 0) {
                                if (ferror(in))
-                                       errmsg("cannot read %d bytes from the input"
-                                              " file", l);
+                                       return errmsg("cannot read %d bytes from the input file", l);
                                else
-                                       errmsg("not enough data in the input file");
-                               return -1;
+                                       return errmsg("not enough data in the input file");
                        }
 
                        l -= rd;
@@ -269,11 +261,8 @@ int ubigen_write_volume(const struct ubigen_info *ui,
                memset(outbuf + ui->data_offs + len, 0xFF,
                       ui->peb_size - ui->data_offs - len);
 
-               if (fwrite(outbuf, 1, ui->peb_size, out) != ui->peb_size) {
-                       errmsg("cannot write %d bytes from the output"
-                              " file", l);
-                       return -1;
-               }
+               if (fwrite(outbuf, 1, ui->peb_size, out) != ui->peb_size)
+                       return errmsg("cannot write %d bytes from the output file", l);
 
                lnum += 1;
        }
@@ -324,11 +313,8 @@ int ubigen_write_layout_vol(const struct ubigen_info *ui,
        if (size == ui->peb_size) {
                init_vid_hdr(ui, &vi, vid_hdr, 1, NULL, 0);
                size = fwrite(outbuf, 1, ui->peb_size, out);
-               if (size != ui->peb_size) {
-                       errmsg("cannot write %d bytes", ui->peb_size);
-                       perror("write");
-                       return -1;
-               }
+               if (size != ui->peb_size)
+                       return sys_errmsg("cannot write %d bytes", ui->peb_size);
        }
 
        return 0;
index 2e6c20d49b9b0ddceb41d3d94fe8af1082b4cb17..b3d768a2f4796f6d4be75da8e2aa8201b0b1711d 100644 (file)
@@ -26,7 +26,6 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 
 #include <libubi.h>
 #include "common.h"
@@ -92,28 +91,22 @@ static int parse_opt(int argc, char * const argv[])
                switch (key) {
                case 'd':
                        args.devn = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args.devn < 0) {
-                               errmsg("bad UBI device number: \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (*endp != '\0' || endp == optarg || args.devn < 0)
+                               return errmsg("bad UBI device number: \"%s\"", optarg);
 
                        break;
 
                case 'm':
                        args.mtdn = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args.mtdn < 0) {
-                               errmsg("bad MTD device number: \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (*endp != '\0' || endp == optarg || args.mtdn < 0)
+                               return errmsg("bad MTD device number: \"%s\"", optarg);
 
                        break;
 
                case 'o':
                        args.vidoffs = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args.vidoffs <= 0) {
-                               errmsg("bad VID header offset: \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (*endp != '\0' || endp == optarg || args.vidoffs <= 0)
+                               return errmsg("bad VID header offset: \"%s\"", optarg);
 
                        break;
 
@@ -128,8 +121,7 @@ static int parse_opt(int argc, char * const argv[])
                        exit(EXIT_SUCCESS);
 
                case ':':
-                       errmsg("parameter is missing");
-                       return -1;
+                       return errmsg("parameter is missing");
 
                default:
                        fprintf(stderr, "Use -h for help\n");
@@ -137,18 +129,13 @@ static int parse_opt(int argc, char * const argv[])
                }
        }
 
-       if (optind == argc) {
-               errmsg("UBI control device name was not specified (use -h for help)");
-               return -1;
-       } else if (optind != argc - 1) {
-               errmsg("more then one UBI control device specified (use -h for help)");
-               return -1;
-       }
+       if (optind == argc)
+               return errmsg("UBI control device name was not specified (use -h for help)");
+       else if (optind != argc - 1)
+               return errmsg("more then one UBI control device specified (use -h for help)");
 
-       if (args.mtdn == -1) {
-               errmsg("MTD device number was not specified (use -h for help)");
-               return -1;
-       }
+       if (args.mtdn == -1)
+               return errmsg("MTD device number was not specified (use -h for help)");
 
        args.node = argv[optind];
        return 0;
@@ -167,19 +154,15 @@ int main(int argc, char * const argv[])
                return -1;
 
        libubi = libubi_open();
-       if (libubi == NULL) {
-               errmsg("cannot open libubi");
-               perror("libubi_open");
-               return -1;
-       }
+       if (libubi == NULL)
+               return sys_errmsg("cannot open libubi");
 
        /*
         * Make sure the kernel is fresh enough and this feature is supported.
         */
        err = ubi_get_info(libubi, &ubi_info);
        if (err) {
-               errmsg("cannot get UBI information");
-               perror("ubi_get_info");
+               sys_errmsg("cannot get UBI information");
                goto out_libubi;
        }
 
@@ -194,16 +177,14 @@ int main(int argc, char * const argv[])
 
        err = ubi_attach_mtd(libubi, args.node, &req);
        if (err) {
-               errmsg("cannot attach mtd%d", args.mtdn);
-               perror("ubi_attach_mtd");
+               sys_errmsg("cannot attach mtd%d", args.mtdn);
                goto out_libubi;
        }
 
        /* Print some information about the new UBI device */
        err = ubi_get_dev_info1(libubi, req.dev_num, &dev_info);
        if (err) {
-               errmsg("cannot get information about newly created UBI device");
-               perror("ubi_get_dev_info1");
+               sys_errmsg("cannot get information about newly created UBI device");
                goto out_libubi;
        }
 
index 806443955ec6c168d621fc3e6297000dd3d2c4e7..5c7a8ba8979cbc1057ee52b76faa7d98cf01bde2 100644 (file)
@@ -28,7 +28,6 @@
 #include <getopt.h>
 #include <argp.h>
 #include <unistd.h>
-#include <errno.h>
 #include <mtd/ubi-header.h>
 
 #include "crc32.h"
@@ -76,8 +75,7 @@ static int parse_opt(int argc, char * const argv[])
                        exit(EXIT_SUCCESS);
 
                case ':':
-                       errmsg("parameter is missing");
-                       return -1;
+                       return errmsg("parameter is missing");
 
                default:
                        fprintf(stderr, "Use -h for help\n");
@@ -97,11 +95,8 @@ int main(int argc, char * const argv[])
 
        if (argc > 1) {
                fp = fopen(argv[1], "r");
-               if (!fp) {
-                       errmsg("cannot open \"%s\"", argv[1]);
-                       perror("fopen");
-                       return -1;
-               }
+               if (!fp)
+                       return sys_errmsg("cannot open \"%s\"", argv[1]);
        } else
                fp = stdin;
 
@@ -114,8 +109,7 @@ int main(int argc, char * const argv[])
 
                read = fread(buf, 1, BUFSIZE, fp);
                if (ferror(fp)) {
-                       errmsg("cannot read input file");
-                       perror("fread");
+                       sys_errmsg("cannot read input file");
                        err = -1;
                        goto out_close;
                }
index fe539970870839bdca1429ffb35766aa3937b879..cd4836811e97991673a3d7f60f15a496c3f5a365 100644 (file)
@@ -26,7 +26,6 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 
 #include <libubi.h>
 #include "common.h"
@@ -84,19 +83,15 @@ static int parse_opt(int argc, char * const argv[])
                switch (key) {
                case 'd':
                        args.devn = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args.devn < 0) {
-                               errmsg("bad UBI device number: \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (*endp != '\0' || endp == optarg || args.devn < 0)
+                               return errmsg("bad UBI device number: \"%s\"", optarg);
 
                        break;
 
                case 'm':
                        args.mtdn = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args.mtdn < 0) {
-                               errmsg("bad MTD device number: \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (*endp != '\0' || endp == optarg || args.mtdn < 0)
+                               return errmsg("bad MTD device number: \"%s\"", optarg);
 
                        break;
 
@@ -111,8 +106,7 @@ static int parse_opt(int argc, char * const argv[])
                        exit(EXIT_SUCCESS);
 
                case ':':
-                       errmsg("parameter is missing");
-                       return -1;
+                       return errmsg("parameter is missing");
 
                default:
                        fprintf(stderr, "Use -h for help\n");
@@ -120,23 +114,16 @@ static int parse_opt(int argc, char * const argv[])
                }
        }
 
-       if (optind == argc) {
-               errmsg("UBI control device name was not specified (use -h for help)");
-               return -1;
-       } else if (optind != argc - 1) {
-               errmsg("more then one UBI control device specified (use -h for help)");
-               return -1;
-       }
+       if (optind == argc)
+               return errmsg("UBI control device name was not specified (use -h for help)");
+       else if (optind != argc - 1)
+               return errmsg("more then one UBI control device specified (use -h for help)");
 
-       if (args.mtdn == -1 && args.devn == -1) {
-               errmsg("neither MTD nor UBI devices were specified (use -h for help)");
-               return -1;
-       }
+       if (args.mtdn == -1 && args.devn == -1)
+               return errmsg("neither MTD nor UBI devices were specified (use -h for help)");
 
-       if (args.mtdn != -1 && args.devn != -1) {
-               errmsg("specify either MTD or UBI device (use -h for help)");
-               return -1;
-       }
+       if (args.mtdn != -1 && args.devn != -1)
+               return errmsg("specify either MTD or UBI device (use -h for help)");
 
        args.node = argv[optind];
        return 0;
@@ -153,19 +140,15 @@ int main(int argc, char * const argv[])
                return -1;
 
        libubi = libubi_open();
-       if (libubi == NULL) {
-               errmsg("cannot open libubi");
-               perror("libubi_open");
-               return -1;
-       }
+       if (libubi == NULL)
+               return sys_errmsg("cannot open libubi");
 
        /*
         * Make sure the kernel is fresh enough and this feature is supported.
         */
        err = ubi_get_info(libubi, &ubi_info);
        if (err) {
-               errmsg("cannot get UBI information");
-               perror("ubi_get_info");
+               sys_errmsg("cannot get UBI information");
                goto out_libubi;
        }
 
@@ -177,15 +160,13 @@ int main(int argc, char * const argv[])
        if (args.devn != -1) {
                err = ubi_remove_dev(libubi, args.node, args.devn);
                if (err) {
-                       errmsg("cannot remove ubi%d", args.devn);
-                       perror("ubi_remove_dev");
+                       sys_errmsg("cannot remove ubi%d", args.devn);
                        goto out_libubi;
                }
        } else {
                err = ubi_detach_mtd(libubi, args.node, args.mtdn);
                if (err) {
-                       errmsg("cannot detach mtd%d", args.mtdn);
-                       perror("ubi_detach_mtd");
+                       sys_errmsg("cannot detach mtd%d", args.mtdn);
                        goto out_libubi;
                }
        }
index 2415be8807fefbdf6c1cd39aca4b5ef2440d8577..75c180dcbdd94f1ef2d7b94a38d5b0a0f5e3869c 100644 (file)
@@ -28,7 +28,6 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 
 #include <libubi.h>
 #include "common.h"
@@ -104,29 +103,20 @@ static int param_sanity_check(void)
 {
        int len;
 
-       if (args.bytes == -1 && !args.maxavs && args.lebs == -1) {
-               errmsg("volume size was not specified (use -h for help)");
-               return -1;
-       }
+       if (args.bytes == -1 && !args.maxavs && args.lebs == -1)
+               return errmsg("volume size was not specified (use -h for help)");
 
        if ((args.bytes != -1 && (args.maxavs || args.lebs != -1))  ||
            (args.lebs != -1  && (args.maxavs || args.bytes != -1)) ||
-           (args.maxavs && (args.bytes != -1 || args.lebs != -1))) {
-               errmsg("size specified with more then one option");
-               return -1;
-       }
+           (args.maxavs && (args.bytes != -1 || args.lebs != -1)))
+               return errmsg("size specified with more then one option");
 
-       if (args.name == NULL) {
-               errmsg("volume name was not specified (use -h for help)");
-               return -1;
-       }
+       if (args.name == NULL)
+               return errmsg("volume name was not specified (use -h for help)");
 
        len = strlen(args.name);
-       if (len > UBI_MAX_VOLUME_NAME) {
-               errmsg("too long name (%d symbols), max is %d",
-                       len, UBI_MAX_VOLUME_NAME);
-               return -1;
-       }
+       if (len > UBI_MAX_VOLUME_NAME)
+               return errmsg("too long name (%d symbols), max is %d", len, UBI_MAX_VOLUME_NAME);
 
        return 0;
 }
@@ -147,52 +137,32 @@ static int parse_opt(int argc, char * const argv[])
                                args.vol_type = UBI_DYNAMIC_VOLUME;
                        else if (!strcmp(optarg, "static"))
                                args.vol_type = UBI_STATIC_VOLUME;
-                       else {
-                               errmsg("bad volume type: \"%s\"", optarg);
-                               return -1;
-                       }
+                       else
+                               return errmsg("bad volume type: \"%s\"", optarg);
                        break;
 
                case 's':
-                       args.bytes = strtoull(optarg, &endp, 0);
-                       if (endp == optarg || args.bytes <= 0) {
-                               errmsg("bad volume size: \"%s\"", optarg);
-                               return -1;
-                       }
-                       if (*endp != '\0') {
-                               int mult = ubiutils_get_multiplier(endp);
-
-                               if (mult == -1) {
-                                       errmsg("bad size specifier: \"%s\" - "
-                                              "should be 'KiB', 'MiB' or 'GiB'", endp);
-                                       return -1;
-                               }
-                               args.bytes *= mult;
-                       }
+                       args.bytes = ubiutils_get_bytes(optarg);
+                       if (args.bytes <= 0)
+                               return errmsg("bad volume size: \"%s\"", optarg);
                        break;
 
                case 'S':
                        args.lebs = strtoull(optarg, &endp, 0);
-                       if (endp == optarg || args.lebs <= 0 || *endp != '\0') {
-                               errmsg("bad LEB count: \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (endp == optarg || args.lebs <= 0 || *endp != '\0')
+                               return errmsg("bad LEB count: \"%s\"", optarg);
                        break;
 
                case 'a':
                        args.alignment = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args.alignment <= 0) {
-                               errmsg("bad volume alignment: \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (*endp != '\0' || endp == optarg || args.alignment <= 0)
+                               return errmsg("bad volume alignment: \"%s\"", optarg);
                        break;
 
                case 'n':
                        args.vol_id = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args.vol_id < 0) {
-                               errmsg("bad volume ID: " "\"%s\"", optarg);
-                               return -1;
-                       }
+                       if (*endp != '\0' || endp == optarg || args.vol_id < 0)
+                               return errmsg("bad volume ID: " "\"%s\"", optarg);
                        break;
 
                case 'N':
@@ -215,8 +185,7 @@ static int parse_opt(int argc, char * const argv[])
                        break;
 
                case ':':
-                       errmsg("parameter is missing");
-                       return -1;
+                       return errmsg("parameter is missing");
 
                default:
                        fprintf(stderr, "Use -h for help\n");
@@ -224,13 +193,10 @@ static int parse_opt(int argc, char * const argv[])
                }
        }
 
-       if (optind == argc) {
-               errmsg("UBI device name was not specified (use -h for help)");
-               return -1;
-       } else if (optind != argc - 1) {
-               errmsg("more then one UBI device specified (use -h for help)");
-               return -1;
-       }
+       if (optind == argc)
+               return errmsg("UBI device name was not specified (use -h for help)");
+       else if (optind != argc - 1)
+               return errmsg("more then one UBI device specified (use -h for help)");
 
        args.node = argv[optind];
 
@@ -253,11 +219,8 @@ int main(int argc, char * const argv[])
                return err;
 
        libubi = libubi_open();
-       if (!libubi) {
-               errmsg("cannot open libubi");
-               perror("libubi_open");
-               return -1;
-       }
+       if (!libubi)
+               return sys_errmsg("cannot open libubi");
 
        err = ubi_node_type(libubi, args.node);
        if (err == 2) {
@@ -271,9 +234,8 @@ int main(int argc, char * const argv[])
 
        err = ubi_get_dev_info(libubi, args.node, &dev_info);
        if (err) {
-               errmsg("cannot get information about UBI device \"%s\"",
-                      args.node);
-               perror("ubi_get_dev_info");
+               sys_errmsg("cannot get information about UBI device \"%s\"",
+                          args.node);
                goto out_libubi;
        }
 
@@ -296,8 +258,7 @@ int main(int argc, char * const argv[])
 
        err = ubi_mkvol(libubi, args.node, &req);
        if (err < 0) {
-               errmsg("cannot UBI create volume");
-               perror("ubi_mkvol");
+               sys_errmsg("cannot UBI create volume");
                goto out_libubi;
        }
 
@@ -306,8 +267,7 @@ int main(int argc, char * const argv[])
        /* Print information about the created device */
        err = ubi_get_vol_info1(libubi, dev_info.dev_num, args.vol_id, &vol_info);
        if (err) {
-               errmsg("cannot get information about newly created UBI volume");
-               perror("ubi_get_vol_info1");
+               sys_errmsg("cannot get information about newly created UBI volume");
                goto out_libubi;
        }
 
index 07b1790254e7010ae447c867a4ecf54c88542064..185caae9643b0e5438465c946427b96b95bcc530 100644 (file)
@@ -26,7 +26,6 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 
 #include <libubi.h>
 #include "common.h"
@@ -99,18 +98,14 @@ static int parse_opt(int argc, char * const argv[])
 
                case 'n':
                        args.vol_id = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args.vol_id < 0) {
-                               errmsg("bad volume ID: " "\"%s\"", optarg);
-                               return -1;
-                       }
+                       if (*endp != '\0' || endp == optarg || args.vol_id < 0)
+                               return errmsg("bad volume ID: " "\"%s\"", optarg);
                        break;
 
                case 'd':
                        args.devn = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args.devn < 0) {
-                               errmsg("bad UBI device number: \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (*endp != '\0' || endp == optarg || args.devn < 0)
+                               return errmsg("bad UBI device number: \"%s\"", optarg);
 
                        break;
 
@@ -125,8 +120,7 @@ static int parse_opt(int argc, char * const argv[])
                        exit(EXIT_SUCCESS);
 
                case ':':
-                       errmsg("parameter is missing");
-                       return -1;
+                       return errmsg("parameter is missing");
 
                default:
                        fprintf(stderr, "Use -h for help\n");
@@ -134,12 +128,10 @@ static int parse_opt(int argc, char * const argv[])
                }
        }
 
-       if (optind == argc - 1) {
+       if (optind == argc - 1)
                args.node = argv[optind];
-       } else if (optind < argc) {
-               errmsg("more then one UBI devices specified (use -h for help)");
-               return -1;
-       }
+       else if (optind < argc)
+               return errmsg("more then one UBI devices specified (use -h for help)");
 
        return 0;
 }
@@ -150,44 +142,30 @@ static int translate_dev(libubi_t libubi, const char *node)
 
        err = ubi_node_type(libubi, node);
        if (err == -1) {
-               if (errno) {
-                       errmsg("unrecognized device node \"%s\"", node);
-                       return -1;
-               }
-               errmsg("\"%s\" does not correspond to any UBI device or volume",
-                      node);
-               return -1;
+               if (errno)
+                       return errmsg("unrecognized device node \"%s\"", node);
+               return errmsg("\"%s\" does not correspond to any UBI device or volume", node);
        }
 
        if (err == 1) {
                struct ubi_dev_info dev_info;
 
                err = ubi_get_dev_info(libubi, node, &dev_info);
-               if (err) {
-                       errmsg("cannot get information about UBI device \"%s\"",
-                              node);
-                       perror("ubi_get_dev_info");
-                       return -1;
-               }
+               if (err)
+                       return sys_errmsg("cannot get information about UBI device \"%s\"", node);
 
                args.devn = dev_info.dev_num;
        } else {
                struct ubi_vol_info vol_info;
 
                err = ubi_get_vol_info(libubi, node, &vol_info);
-               if (err) {
-                       errmsg("cannot get information about UBI volume \"%s\"",
-                              node);
-                       perror("ubi_get_vol_info");
-                       return -1;
-               }
+               if (err)
+                       return sys_errmsg("cannot get information about UBI volume \"%s\"", node);
 
-               if (args.vol_id != -1) {
-                       errmsg("both volume character device node (\"%s\") and "
-                              "volume ID (%d) are specify, use only one of them"
-                              "(use -h for help)", node, args.vol_id);
-                       return -1;
-               }
+               if (args.vol_id != -1)
+                       return errmsg("both volume character device node (\"%s\") and "
+                                     "volume ID (%d) are specify, use only one of them"
+                                     "(use -h for help)", node, args.vol_id);
 
                args.devn = vol_info.dev_num;
                args.vol_id = vol_info.vol_id;
@@ -202,12 +180,9 @@ static int print_vol_info(libubi_t libubi, int dev_num, int vol_id)
        struct ubi_vol_info vol_info;
 
        err = ubi_get_vol_info1(libubi, dev_num, vol_id, &vol_info);
-       if (err) {
-               errmsg("cannot get information about UBI volume %d on ubi%d",
-                      vol_id, dev_num);
-               perror("ubi_get_vol_info1");
-               return -1;
-       }
+       if (err)
+               return sys_errmsg("cannot get information about UBI volume %d on ubi%d",
+                                 vol_id, dev_num);
 
        printf("Volume ID:   %d (on ubi%d)\n", vol_info.vol_id, vol_info.dev_num);
        printf("Type:        %s\n",
@@ -237,11 +212,8 @@ static int print_dev_info(libubi_t libubi, int dev_num, int all)
        struct ubi_vol_info vol_info;
 
        err = ubi_get_dev_info1(libubi, dev_num, &dev_info);
-       if (err) {
-               errmsg("cannot get information about UBI device %d", dev_num);
-               perror("ubi_get_dev_info1");
-               return -1;
-       }
+       if (err)
+               return sys_errmsg("cannot get information about UBI device %d", dev_num);
 
        printf("ubi%d:\n", dev_info.dev_num);
        printf("Volumes count:                           %d\n", dev_info.vol_count);
@@ -274,10 +246,8 @@ static int print_dev_info(libubi_t libubi, int dev_num, int all)
                        if (errno == ENOENT)
                                continue;
 
-                       errmsg("libubi failed to probe volume %d on ubi%d",
-                              i, dev_info.dev_num);
-                       perror("ubi_get_vol_info1");
-                       return -1;
+                       return sys_errmsg("libubi failed to probe volume %d on ubi%d",
+                                         i, dev_info.dev_num);
                }
 
                if (!first)
@@ -304,10 +274,8 @@ static int print_dev_info(libubi_t libubi, int dev_num, int all)
                        if (errno == ENOENT)
                                continue;
 
-                       errmsg("libubi failed to probe volume %d on ubi%d",
-                              i, dev_info.dev_num);
-                       perror("ubi_get_vol_info1");
-                       return -1;
+                       return sys_errmsg("libubi failed to probe volume %d on ubi%d",
+                                         i, dev_info.dev_num);
                }
                first = 0;
 
@@ -326,11 +294,8 @@ static int print_general_info(libubi_t libubi, int all)
        struct ubi_dev_info dev_info;
 
        err = ubi_get_info(libubi, &ubi_info);
-       if (err) {
-               errmsg("cannot get UBI information");
-               perror("ubi_get_info");
-               return -1;
-       }
+       if (err)
+               return sys_errmsg("cannot get UBI information");
 
        printf("UBI version:                    %d\n", ubi_info.version);
        printf("Count of UBI devices:           %d\n", ubi_info.dev_count);
@@ -351,9 +316,7 @@ static int print_general_info(libubi_t libubi, int all)
                        if (errno == ENOENT)
                                continue;
 
-                       errmsg("libubi failed to probe UBI device %d", i);
-                       perror("ubi_get_dev_info1");
-                       return -1;
+                       return sys_errmsg("libubi failed to probe UBI device %d", i);
                }
 
                if (!first)
@@ -380,9 +343,7 @@ static int print_general_info(libubi_t libubi, int all)
                        if (errno == ENOENT)
                                continue;
 
-                       errmsg("libubi failed to probe UBI device %d", i);
-                       perror("ubi_get_dev_info1");
-                       return -1;
+                       return sys_errmsg("libubi failed to probe UBI device %d", i);
                }
                first = 0;
 
@@ -402,17 +363,12 @@ int main(int argc, char * const argv[])
        if (err)
                return -1;
 
-       if (!args.node && args.devn != -1) {
-               errmsg("specify either device number or node file (use -h for help)");
-               return -1;
-       }
+       if (!args.node && args.devn != -1)
+               return errmsg("specify either device number or node file (use -h for help)");
 
        libubi = libubi_open();
-       if (libubi == NULL) {
-               errmsg("cannot open libubi");
-               perror("libubi_open");
-               return -1;
-       }
+       if (libubi == NULL)
+               return sys_errmsg("cannot open libubi");
 
        if (args.node) {
                /*
index 9660b09e1b1c0919007272e26fbac7c5e5960671..aaa89dfd477ae1cb573244eb652c8574426e1d7e 100644 (file)
@@ -28,7 +28,6 @@
 #include <getopt.h>
 #include <stdio.h>
 #include <string.h>
-#include <errno.h>
 #include <unistd.h>
 #include <sys/stat.h>
 
@@ -163,89 +162,45 @@ static int parse_opt(int argc, char * const argv[])
                switch (key) {
                case 'o':
                        args.fp_out = fopen(optarg, "wb");
-                       if (!args.fp_out) {
-                               errmsg("cannot open file \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (!args.fp_out)
+                               return errmsg("cannot open file \"%s\"", optarg);
                        args.f_out = optarg;
                        break;
 
                case 'p':
-                       args.peb_size = strtoull(optarg, &endp, 0);
-                       if (endp == optarg || args.peb_size <= 0) {
-                               errmsg("bad physical eraseblock size: \"%s\"", optarg);
-                               return -1;
-                       }
-                       if (*endp != '\0') {
-                               int mult = ubiutils_get_multiplier(endp);
-
-                               if (mult == -1) {
-                                       errmsg("bad size specifier: \"%s\" - "
-                                              "should be 'KiB', 'MiB' or 'GiB'", endp);
-                                       return -1;
-                               }
-                               args.peb_size *= mult;
-                       }
+                       args.peb_size = ubiutils_get_bytes(optarg);
+                       if (args.peb_size <= 0)
+                               return errmsg("bad physical eraseblock size: \"%s\"", optarg);
                        break;
 
                case 'm':
-                       args.min_io_size = strtoull(optarg, &endp, 0);
-                       if (endp == optarg || args.min_io_size <= 0) {
-                               errmsg("bad min. I/O unit size: \"%s\"", optarg);
-                               return -1;
-                       }
-                       if (*endp != '\0') {
-                               int mult = ubiutils_get_multiplier(endp);
-
-                               if (mult == -1) {
-                                       errmsg("bad size specifier: \"%s\" - "
-                                              "should be 'KiB', 'MiB' or 'GiB'", endp);
-                                       return -1;
-                               }
-                               args.min_io_size *= mult;
-                       }
+                       args.min_io_size = ubiutils_get_bytes(optarg);
+                       if (args.min_io_size <= 0)
+                               return errmsg("bad min. I/O unit size: \"%s\"", optarg);
                        break;
 
                case 's':
-                       args.subpage_size = strtoull(optarg, &endp, 0);
-                       if (endp == optarg || args.subpage_size <= 0) {
-                               errmsg("bad sub-page size: \"%s\"", optarg);
-                               return -1;
-                       }
-                       if (*endp != '\0') {
-                               int mult = ubiutils_get_multiplier(endp);
-
-                               if (mult == -1) {
-                                       errmsg("bad size specifier: \"%s\" - "
-                                              "should be 'KiB', 'MiB' or 'GiB'", endp);
-                                       return -1;
-                               }
-                               args.subpage_size *= mult;
-                       }
+                       args.subpage_size = ubiutils_get_bytes(optarg);
+                       if (args.subpage_size <= 0)
+                               return errmsg("bad sub-page size: \"%s\"", optarg);
                        break;
 
                case 'O':
                        args.vid_hdr_offs = strtoul(optarg, &endp, 0);
-                       if (endp == optarg || args.vid_hdr_offs < 0) {
-                               errmsg("bad VID header offset: \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (endp == optarg || args.vid_hdr_offs < 0)
+                               return errmsg("bad VID header offset: \"%s\"", optarg);
                        break;
 
                case 'e':
                        args.ec = strtoul(optarg, &endp, 0);
-                       if (endp == optarg || args.ec < 0) {
-                               errmsg("bad erase counter value: \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (endp == optarg || args.ec < 0)
+                               return errmsg("bad erase counter value: \"%s\"", optarg);
                        break;
 
                case 'x':
                        args.ubi_ver = strtoul(optarg, &endp, 0);
-                       if (endp == optarg || args.ubi_ver < 0) {
-                               errmsg("bad UBI version: \"%s\"", optarg);
-                               return -1;
-                       }
+                       if (endp == optarg || args.ubi_ver < 0)
+                               return errmsg("bad UBI version: \"%s\"", optarg);
                        break;
 
                case 'v':
@@ -269,27 +224,19 @@ static int parse_opt(int argc, char * const argv[])
                }
        }
 
-       if (optind == argc) {
-               errmsg("input PFI file was not specified (use -h for help)");
-               return -1;
-       }
+       if (optind == argc)
+               return errmsg("input PFI file was not specified (use -h for help)");
 
-       if (optind != argc - 1) {
-               errmsg("more then one input PFI file was specified (use -h for help)");
-               return -1;
-       }
+       if (optind != argc - 1)
+               return errmsg("more then one input PFI file was specified (use -h for help)");
 
        args.f_in = argv[optind];
 
-       if (args.peb_size < 0) {
-               errmsg("physical eraseblock size was not specified (use -h for help)");
-               return -1;
-       }
+       if (args.peb_size < 0)
+               return errmsg("physical eraseblock size was not specified (use -h for help)");
 
-       if (args.min_io_size < 0) {
-               errmsg("min. I/O unit size was not specified (use -h for help)");
-               return -1;
-       }
+       if (args.min_io_size < 0)
+               return errmsg("min. I/O unit size was not specified (use -h for help)");
 
        if (args.subpage_size < 0)
                args.subpage_size = args.min_io_size;
@@ -310,10 +257,8 @@ int read_section(const char *sname, struct ubigen_vol_info *vi,
 
        *img = NULL;
 
-       if (strlen(sname) > 128) {
-               errmsg("too long section name \"%s\"", sname);
-               return -1;
-       }
+       if (strlen(sname) > 128)
+               return errmsg("too long section name \"%s\"", sname);
 
        /* Make sure mode is UBI, otherwise ignore this section */
        sprintf(buf, "%s:mode", sname);
@@ -342,21 +287,14 @@ int read_section(const char *sname, struct ubigen_vol_info *vi,
        /* Fetch volume id */
        sprintf(buf, "%s:vol_id", sname);
        vi->id = iniparser_getint(args.dict, buf, -1);
-       if (vi->id == -1) {
-               errmsg("\"vol_id\" key not found in section  \"%s\"", sname);
-               return -1;
-       }
+       if (vi->id == -1)
+               return errmsg("\"vol_id\" key not found in section  \"%s\"", sname);
 
-       if (vi->id < 0) {
-               errmsg("negative volume ID %d", vi->id);
-               return -1;
-       }
+       if (vi->id < 0)
+               return errmsg("negative volume ID %d", vi->id);
 
-       if (vi->id >= UBI_MAX_VOLUMES) {
-               errmsg("too highe volume ID %d, max. is %d",
-                      vi->id, UBI_MAX_VOLUMES);
-               return -1;
-       }
+       if (vi->id >= UBI_MAX_VOLUMES)
+               return errmsg("too high volume ID %d, max. is %d", vi->id, UBI_MAX_VOLUMES);
 
        verbose(args.verbose, "volume ID: %d", vi->id);
 
@@ -364,48 +302,24 @@ int read_section(const char *sname, struct ubigen_vol_info *vi,
        sprintf(buf, "%s:vol_size", sname);
        p = iniparser_getstring(args.dict, buf, NULL);
        if (p) {
-               char *endp;
-
-               vi->bytes = strtoull((char *)p, &endp, 0);
-               if (endp == p || vi->bytes <= 0) {
-                       errmsg("bad \"vol_size\" key: \"%s\"", p);
-                       return -1;
-               }
-
-               if (*endp != '\0') {
-                       int mult = ubiutils_get_multiplier(endp);
-
-                       if (mult == -1) {
-                               errmsg("bad size specifier: \"%s\" - "
-                                      "should be 'KiB', 'MiB' or 'GiB'", endp);
-                               return -1;
-                       }
-                       vi->bytes *= mult;
-               }
+               vi->bytes = ubiutils_get_bytes(optarg);
+               if (vi->bytes <= 0)
+                       return errmsg("bad \"vol_size\" key: \"%s\"", p);
 
                verbose(args.verbose, "volume size: %lld bytes", vi->bytes);
        } else {
                struct stat st;
 
-               if (!*img) {
-                       errmsg("neither image file (\"image=\") nor volume size"
-                              " (\"vol_size=\") specified");
-                       return -1;
-               }
+               if (!*img)
+                       return errmsg("neither image file (\"image=\") nor volume size (\"vol_size=\") specified");
 
-               if (stat(*img, &st)) {
-                       errmsg("cannot stat \"%s\"", *img);
-                       perror("stat");
-                       return -1;
-               }
+               if (stat(*img, &st))
+                       return sys_errmsg("cannot stat \"%s\"", *img);
 
                vi->bytes = st.st_size;
 
-               if (vi->bytes == 0) {
-                       errmsg("file \"%s\" referred from section \"%s\" is empty",
-                              *img, sname);
-                       return -1;
-               }
+               if (vi->bytes == 0)
+                       return errmsg("file \"%s\" referred from section \"%s\" is empty", *img, sname);
 
                printf(PROGRAM_NAME ": volume size was not specified in"
                       "section \"%s\", assume ", sname);
@@ -425,10 +339,8 @@ int read_section(const char *sname, struct ubigen_vol_info *vi,
                        vi->type = UBI_VID_STATIC;
                else if (!strcmp(p, "dynamic"))
                        vi->type = UBI_VID_DYNAMIC;
-               else {
-                       errmsg("invalid volume type \"%s\"", p);
-                       return -1;
-               }
+               else
+                       return errmsg("invalid volume type \"%s\"", p);
        }
 
        verbose(args.verbose, "volume type: %s",
@@ -437,18 +349,14 @@ int read_section(const char *sname, struct ubigen_vol_info *vi,
        /* Fetch volume name */
        sprintf(buf, "%s:vol_name", sname);
        p = iniparser_getstring(args.dict, buf, NULL);
-       if (!p) {
-               errmsg("\"vol_name\" key not found in section  \"%s\"", sname);
-               return -1;
-       }
+       if (!p)
+               return errmsg("\"vol_name\" key not found in section  \"%s\"", sname);
 
        vi->name = p;
        vi->name_len = strlen(p);
-       if (vi->name_len > UBI_VOL_NAME_MAX) {
-               errmsg("too long volume name in section \"%s\", max. is "
-                      "%d characters", vi->name, UBI_VOL_NAME_MAX);
-               return -1;
-       }
+       if (vi->name_len > UBI_VOL_NAME_MAX)
+               return errmsg("too long volume name in section \"%s\", max. is %d characters",
+                             vi->name, UBI_VOL_NAME_MAX);
 
        verbose(args.verbose, "volume name: %s", p);
 
@@ -459,10 +367,8 @@ int read_section(const char *sname, struct ubigen_vol_info *vi,
                normsg("volume alignment was not specified in section "
                       "\"%s\", assume 1", sname);
                        vi->alignment = 1;
-       } else if (vi->id < 0) {
-               errmsg("negative volume alignement %d", vi->alignment);
-               return -1;
-       }
+       } else if (vi->id < 0)
+               return errmsg("negative volume alignement %d", vi->alignment);
 
        verbose(args.verbose, "volume alignment: %d", vi->alignment);
 
@@ -474,9 +380,7 @@ int read_section(const char *sname, struct ubigen_vol_info *vi,
                        verbose(args.verbose, "autoresize flags found");
                        vi->flags |= UBI_VTBL_AUTORESIZE_FLG;
                } else {
-                       errmsg("unknown flags \"%s\" in section \"%s\"",
-                              p, sname);
-                       return -1;
+                       return errmsg("unknown flags \"%s\" in section \"%s\"", p, sname);
                }
        }
 
@@ -580,8 +484,7 @@ int main(int argc, char * const argv[])
                        continue;
 
                if (stat(img, &st)) {
-                       errmsg("cannot stat \"%s\"", img);
-                       perror("stat");
+                       sys_errmsg("cannot stat \"%s\"", img);
                        goto out_dict;
                }
 
@@ -597,8 +500,7 @@ int main(int argc, char * const argv[])
 
                f = fopen(img, "r");
                if (!f) {
-                       errmsg("cannot open \"%s\"", img);
-                       perror("fopen");
+                       sys_errmsg("cannot open \"%s\"", img);
                        goto out_dict;
                }
 
index ca315a3799550ea780eb175ed1e7742843261965..c30446ad840ada4b60e7ada7d2d574094a1f666b 100644 (file)
@@ -28,7 +28,6 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 
 #include <libubi.h>
 #include "common.h"
@@ -143,11 +142,8 @@ int main(int argc, char * const argv[])
                return -1;
 
        libubi = libubi_open();
-       if (libubi == NULL) {
-               errmsg("cannot open libubi");
-               perror("libubi_open");
-               return -1;
-       }
+       if (libubi == NULL)
+               return sys_errmsg("cannot open libubi");
 
        err = ubi_node_type(libubi, args.node);
        if (err == 2) {
@@ -161,8 +157,7 @@ int main(int argc, char * const argv[])
 
        err = ubi_rmvol(libubi, args.node, args.vol_id);
        if (err) {
-               errmsg("cannot UBI remove volume");
-               perror("ubi_rmvol");
+               sys_errmsg("cannot UBI remove volume");
                goto out_libubi;
        }
 
index 6898119688d07d7614fa21e228cc2595705598ab..c17b3c5b6657169156f8e814f8ab92b54ac88a1a 100644 (file)
@@ -24,7 +24,6 @@
  *          Artem Bityutskiy
  */
 
-#include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdint.h>
@@ -100,8 +99,7 @@ static int parse_opt(int argc, char * const argv[])
                        exit(EXIT_SUCCESS);
 
                case ':':
-                       errmsg("parameter is missing");
-                       return -1;
+                       return errmsg("parameter is missing");
 
                default:
                        fprintf(stderr, "Use -h for help\n");
@@ -109,14 +107,11 @@ static int parse_opt(int argc, char * const argv[])
                }
        }
 
-       if (optind == argc) {
-               errmsg("UBI device name was not specified (use -h for help)");
-               return -1;
-       } else if (optind != argc - 2) {
-               errmsg("specify UBI device name and image file name as first 2 "
-                      "parameters (use -h for help)");
-               return -1;
-       }
+       if (optind == argc)
+               return errmsg("UBI device name was not specified (use -h for help)");
+       else if (optind != argc - 2)
+               return errmsg("specify UBI device name and image file name as first 2 "
+                             "parameters (use -h for help)");
 
        args.node = argv[optind];
        args.img  = argv[optind + 1];
@@ -129,16 +124,12 @@ static int truncate_volume(libubi_t libubi)
        int err, fd;
 
        fd = open(args.node, O_RDWR);
-       if (fd == -1) {
-               errmsg("cannot open \"%s\"", args.node);
-               perror("open");
-               return -1;
-       }
+       if (fd == -1)
+               return sys_errmsg("cannot open \"%s\"", args.node);
 
        err = ubi_update_start(libubi, fd, 0);
        if (err) {
-               errmsg("cannot truncate volume \"%s\"", args.node);
-               perror("ubi_update_start");
+               sys_errmsg("cannot truncate volume \"%s\"", args.node);
                close(fd);
                return -1;
        }
@@ -158,17 +149,12 @@ static int ubi_write(int fd, const void *buf, int len)
                                warnmsg("do not interrupt me!");
                                continue;
                        }
-                       errmsg("cannot write %d bytes to volume \"%s\"",
-                              len, args.node);
-                       perror("write");
-                       return -1;
+                       return sys_errmsg("cannot write %d bytes to volume \"%s\"",
+                                         len, args.node);
                }
 
-               if (ret == 0) {
-                       errmsg("cannot write %d bytes to volume \"%s\"",
-                              len, args.node);
-                       return -1;
-               }
+               if (ret == 0)
+                       return errmsg("cannot write %d bytes to volume \"%s\"", len, args.node);
 
                len -= ret;
                buf += ret;
@@ -185,10 +171,8 @@ static int update_volume(libubi_t libubi, struct ubi_vol_info *vol_info)
        char *buf;
 
        buf = malloc(vol_info->leb_size);
-       if (!buf) {
-               errmsg("cannot allocate %d bytes of memory", vol_info->leb_size);
-               return -1;
-       }
+       if (!buf)
+               return errmsg("cannot allocate %d bytes of memory", vol_info->leb_size);
 
        err = stat(args.img, &st);
        if (err < 0) {
@@ -205,22 +189,19 @@ static int update_volume(libubi_t libubi, struct ubi_vol_info *vol_info)
 
        fd = open(args.node, O_RDWR);
        if (fd == -1) {
-               errmsg("cannot open UBI volume \"%s\"", args.node);
-               perror("open");
+               sys_errmsg("cannot open UBI volume \"%s\"", args.node);
                goto out_free;
        }
 
        ifd = open(args.img, O_RDONLY);
        if (ifd == -1) {
-               errmsg("cannot open \"%s\"", args.img);
-               perror("open");
+               sys_errmsg("cannot open \"%s\"", args.img);
                goto out_close1;
        }
 
        err = ubi_update_start(libubi, fd, bytes);
        if (err) {
-               errmsg("cannot start volume \"%s\" update", args.node);
-               perror("ubi_update_start");
+               sys_errmsg("cannot start volume \"%s\" update", args.node);
                goto out_close;
        }
 
@@ -236,9 +217,8 @@ static int update_volume(libubi_t libubi, struct ubi_vol_info *vol_info)
                                warnmsg("do not interrupt me!");
                                continue;
                        } else {
-                               errmsg("cannot read %d bytes from \"%s\"",
-                                      tocopy, args.img);
-                               perror("read");
+                               sys_errmsg("cannot read %d bytes from \"%s\"",
+                                          tocopy, args.img);
                                goto out_close;
                        }
                }
@@ -273,14 +253,12 @@ int main(int argc, char * const argv[])
        if (err)
                return -1;
 
-       if (!args.img && !args.truncate) {
-               errmsg("incorrect arguments, use -h for help");
-               return -1;
-       }
+       if (!args.img && !args.truncate)
+               return errmsg("incorrect arguments, use -h for help");
 
        libubi = libubi_open();
        if (libubi == NULL) {
-               perror("Cannot open libubi");
+               sys_errmsg("cannot open libubi");
                goto out_libubi;
        }
 
@@ -296,9 +274,8 @@ int main(int argc, char * const argv[])
 
        err = ubi_get_vol_info(libubi, args.node, &vol_info);
        if (err) {
-               errmsg("cannot get information about UBI volume \"%s\"",
-                      args.node);
-               perror("ubi_get_dev_info");
+               sys_errmsg("cannot get information about UBI volume \"%s\"",
+                          args.node);
                goto out_libubi;
        }