]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
blkreport, blkreset: small code clean ups
authorSami Kerola <kerolasa@iki.fi>
Sat, 11 Feb 2017 14:10:50 +0000 (14:10 +0000)
committerKarel Zak <kzak@redhat.com>
Mon, 13 Feb 2017 13:14:28 +0000 (14:14 +0100)
Set variables read-only where possible.  Fix few code style issues, mostly
with spacing.  Avoid initializing variables if they are never read before
next update.  Remove "ERR: %d -> %s" message that repeated system error
three times, twice in that message and once at end of main that is the only
of these three left in place.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
sys-utils/blkreport.c
sys-utils/blkreset.c

index f3b8ebbda10adfa0f42eccaf7f8e88afbeebe80f..c8d0a63acef89f02357ab94cfc953270fce55757 100644 (file)
 #include "closestream.h"
 #include "blkdev.h"
 
-static const char * type_text[] = {
+static const char *type_text[] = {
        "RESERVED",
        "CONVENTIONAL",
        "SEQ_WRITE_REQUIRED",
        "SEQ_WRITE_PREFERRED",
 };
 
-#define ARRAY_COUNT(x) (sizeof((x))/sizeof((*x)))
-
-const char * condition_str[] = {
+static const char *condition_str[] = {
        "cv", /* conventional zone */
        "e0", /* empty */
        "Oi", /* open implicit */
@@ -66,22 +64,17 @@ const char * condition_str[] = {
        "ro", /* read only */
        "fu", /* full */
        "OL"  /* offline */
-       };
-
-static const char * zone_condition_str(uint8_t cond)
-{
-       return condition_str[cond & 0x0f];
-}
+};
 
-static void print_zones(struct blk_zone *info, uint32_t count)
+static void print_zones(const struct blk_zone *info, const uint32_t count)
 {
        uint32_t iter;
 
        printf(_("Zones returned: %u\n"), count);
 
-       for (iter = 0; iter < count; iter++ ) {
-               struct blk_zone * entry = &info[iter];
-               unsigned int type  = entry->type;
+       for (iter = 0; iter < count; iter++) {
+               const struct blk_zone *entry = &info[iter];
+               unsigned int type = entry->type;
                uint64_t start = entry->start;
                uint64_t wp = entry->wp;
                uint8_t cond = entry->cond;
@@ -94,14 +87,14 @@ static void print_zones(struct blk_zone *info, uint32_t count)
                         " reset:%u non-seq:%u, zcond:%2u(%s) [type: %u(%s)]\n"),
                        start, len, wp - start,
                        entry->reset, entry->non_seq,
-                       cond, zone_condition_str(cond),
+                       cond, condition_str[cond & ARRAY_SIZE(condition_str)],
                        type, type_text[type]);
        }
 }
 
-static int do_report(int fd, uint64_t lba, uint32_t len, int verbose)
+static int do_report(const int fd, const uint64_t lba, uint32_t len, const int verbose)
 {
-       int rc = -4;
+       int rc;
        struct blk_zone_report *zi;
 
        zi = xmalloc(sizeof(struct blk_zone_report) + (len * sizeof(struct blk_zone)));
@@ -112,8 +105,6 @@ static int do_report(int fd, uint64_t lba, uint32_t len, int verbose)
                if (verbose)
                        printf(_("Found %d zones\n"), zi->nr_zones);
                print_zones(zi->zones, zi->nr_zones);
-       } else {
-               warn(_("ERR: %d -> %s"), errno, strerror(errno));
        }
        free(zi);
 
@@ -155,12 +146,12 @@ int main(int argc, char **argv)
        uint64_t offset = 0ul;
        uint32_t length = DEF_REPORT_LEN;
        static const struct option longopts[] = {
-           { "help",      0, 0, 'h' },
-           { "version",   0, 0, 'V' },
-           { "zone",      1, 0, 'z' }, /* starting LBA */
-           { "count",     1, 0, 'c' }, /* max #of zones (entries) for result */
-           { "verbose",   0, 0, 'v' },
-           { NULL,        0, 0, 0 }
+           { "help",    no_argument,       NULL, 'h' },
+           { "version", no_argument,       NULL, 'V' },
+           { "zone",    required_argument, NULL, 'z' }, /* starting LBA */
+           { "count",   required_argument, NULL, 'c' }, /* max #of zones (entries) for result */
+           { "verbose", no_argument,       NULL, 'v' },
+           { NULL, 0, NULL, 0 }
        };
 
        setlocale(LC_ALL, "");
@@ -169,7 +160,7 @@ int main(int argc, char **argv)
        atexit(close_stdout);
 
        while ((c = getopt_long(argc, argv, "hc:z:vV", longopts, NULL)) != -1) {
-               switch(c) {
+               switch (c) {
                case 'h':
                        usage(stdout);
                        break;
index 76df5f6a52e73a219a16219177cc78be6beb8077..9a19c33c7d8d138283c6cef1c76ecae3141bc3aa 100644 (file)
@@ -94,7 +94,6 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
        exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
-
 int main(int argc, char **argv)
 {
        char *path;
@@ -103,17 +102,17 @@ int main(int argc, char **argv)
        struct stat sb;
        struct blk_zone_range za;
        uint64_t zsector = 0;
-       uint64_t zlen = 0;
+       uint64_t zlen;
        uint64_t zcount = 1;
        unsigned long zsize;
-       int rc = 0;
+       int rc;
 
        static const struct option longopts[] = {
-           { "help",      0, 0, 'h' },
-           { "version",   0, 0, 'V' },
-           { "zone",      1, 0, 'z' },
-           { "count",     1, 0, 'c' },
-           { NULL,        0, 0, 0 }
+           { "help",    no_argument,       NULL, 'h' },
+           { "version", no_argument,       NULL, 'V' },
+           { "zone",    required_argument, NULL, 'z' },
+           { "count",   required_argument, NULL, 'c' },
+           { NULL, 0, NULL, 0 }
        };
 
        setlocale(LC_ALL, "");
@@ -122,7 +121,7 @@ int main(int argc, char **argv)
        atexit(close_stdout);
 
        while ((c = getopt_long(argc, argv, "hVz:c:", longopts, NULL)) != -1) {
-               switch(c) {
+               switch (c) {
                case 'h':
                        usage(stdout);
                        break;