diff options
author | June McEnroe <june@causal.agency> | 2016-10-16 15:43:26 -0400 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2016-10-16 15:43:26 -0400 |
commit | 1dee873f295c387a581b7ed1ebd85cd189bcdd39 (patch) | |
tree | 1c752595f4c0206d00690facbaea24bce160177e | |
parent | Remove rpn.c (diff) | |
download | src-1dee873f295c387a581b7ed1ebd85cd189bcdd39.tar.gz src-1dee873f295c387a581b7ed1ebd85cd189bcdd39.zip |
Update xx.c style
-rwxr-xr-x | .bin/xx.c | 40 |
1 files changed, 18 insertions, 22 deletions
diff --git a/.bin/xx.c b/.bin/xx.c index 00b55391..9ffd0b9b 100755 --- a/.bin/xx.c +++ b/.bin/xx.c @@ -1,5 +1,5 @@ #if 0 -exec clang -Weverything -Wno-vla $@ -o $(dirname $0)/xx $0 +exec cc -Wall -Wextra -pedantic $@ -o $(dirname $0)/xx $0 #endif #include <ctype.h> @@ -11,7 +11,7 @@ exec clang -Weverything -Wno-vla $@ -o $(dirname $0)/xx $0 #include <sysexits.h> #include <unistd.h> -static bool zero(const uint8_t *buf, size_t len) { +static bool allZero(const uint8_t *buf, size_t len) { for (size_t i = 0; i < len; ++i) if (buf[i]) return false; return true; @@ -23,7 +23,7 @@ enum { FLAG_SKIP = 4, }; -int main(int argc, char **argv) { +int main(int argc, char *argv[]) { size_t cols = 16; size_t group = 8; uint8_t flags = FLAG_ASCII | FLAG_OFFSET; @@ -31,19 +31,15 @@ int main(int argc, char **argv) { int opt; while ((opt = getopt(argc, argv, "ac:fg:hk")) > 0) { - if (opt == 'a') - flags ^= FLAG_ASCII; - else if (opt == 'c') - cols = strtoul(optarg, NULL, 10); - else if (opt == 'f') - flags ^= FLAG_OFFSET; - else if (opt == 'g') - group = strtoul(optarg, NULL, 10); - else if (opt == 'k') - flags ^= FLAG_SKIP; - else { - fprintf(stderr, "usage: xx [-afk] [-c N] [-g N] [FILE]\n"); - return (opt == 'h') ? EX_OK : EX_USAGE; + switch (opt) { + case 'a': flags ^= FLAG_ASCII; break; + case 'f': flags ^= FLAG_OFFSET; break; + case 'k': flags ^= FLAG_SKIP; break; + case 'c': cols = strtoul(optarg, NULL, 10); break; + case 'g': group = strtoul(optarg, NULL, 10); break; + default: + fprintf(stderr, "usage: xx [-afk] [-c cols] [-g group] [file]\n"); + return (opt == 'h') ? EX_OK : EX_USAGE; } } if (!cols) return EX_USAGE; @@ -54,15 +50,15 @@ int main(int argc, char **argv) { if (!file) err(EX_NOINPUT, "%s", path); uint8_t buf[cols]; - size_t offset = 0, len = 0, i; + size_t offset = 0, len = 0; for (;;) { offset += len; len = fread(buf, 1, sizeof(buf), file); if (!len) break; if ((flags & FLAG_SKIP) && len == sizeof(buf)) { - static bool skip = false; - if (zero(buf, len)) { + static bool skip; + if (allZero(buf, len)) { if (!skip) printf("*\n"); skip = true; continue; @@ -73,16 +69,16 @@ int main(int argc, char **argv) { if (flags & FLAG_OFFSET) printf("%08zx: ", offset); - for (i = 0; i < len; ++i) { + for (size_t i = 0; i < len; ++i) { if (group && i && !(i % group)) printf(" "); printf("%02x ", buf[i]); } if (flags & FLAG_ASCII) { - for (i = len; i < cols; ++i) + for (size_t i = len; i < cols; ++i) printf((group && !(i % group)) ? " " : " "); printf(" "); - for (i = 0; i < len; ++i) + for (size_t i = 0; i < len; ++i) printf("%c", isprint(buf[i]) ? buf[i] : '.'); } |