summary refs log tree commit diff
path: root/.bin/xx.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-x.bin/xx.c72
1 files changed, 38 insertions, 34 deletions
diff --git a/.bin/xx.c b/.bin/xx.c
index b81f0d79..733a1bda 100755
--- a/.bin/xx.c
+++ b/.bin/xx.c
@@ -1,5 +1,5 @@
 #if 0
-exec cc -Weverything -Wno-vla -o ~/.bin/xx $0
+exec ${CC:-cc} -Weverything -Wno-vla $CFLAGS -o $(dirname $0)/xx $0
 #endif
 
 #include <ctype.h>
@@ -9,39 +9,41 @@ exec cc -Weverything -Wno-vla -o ~/.bin/xx $0
 #include <stdlib.h>
 #include <unistd.h>
 
-static bool zero(const uint8_t *buf, size_t len)
-{
+static bool zero(const uint8_t *buf, size_t len) {
     for (size_t i = 0; i < len; ++i)
         if (buf[i]) return false;
     return true;
 }
 
 enum {
-    ASCII = 1,
-    OFFSETS = 2,
-    SKIP = 4,
+    FLAG_ASCII = 1,
+    FLAG_OFFSET = 2,
+    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 = ASCII | OFFSETS;
+    uint8_t flags = FLAG_ASCII | FLAG_OFFSET;
     char *path = NULL;
 
-    while (getopt(argc, argv, "ac:g:os") > 0)
-        if (optopt == 'a') {
-            flags ^= ASCII;
-        } else if (optopt == 'c') {
+    while (getopt(argc, argv, "ac:fg:hk") > 0) {
+        if (optopt == 'a')
+            flags ^= FLAG_ASCII;
+        else if (optopt == 'c')
             cols = (size_t) strtol(optarg, NULL, 10);
-            if (!cols) return EXIT_FAILURE;
-        } else if (optopt == 'g') {
+        else if (optopt == 'f')
+            flags ^= FLAG_OFFSET;
+        else if (optopt == 'g')
             group = (size_t) strtol(optarg, NULL, 10);
-        } else if (optopt == 'o') {
-            flags ^= OFFSETS;
-        } else if (optopt == 's') {
-            flags ^= SKIP;
-        } else return EXIT_FAILURE;
+        else if (optopt == 'k')
+            flags ^= FLAG_SKIP;
+        else {
+            printf("usage: xx [-afk] [-c N] [-g N] [FILE]\n");
+            return (optopt == 'h') ? EXIT_SUCCESS : EXIT_FAILURE;
+        }
+    }
+    if (!cols) return EXIT_FAILURE;
     if (argc > optind)
         path = argv[optind];
 
@@ -52,42 +54,44 @@ int main(int argc, char **argv)
     }
 
     uint8_t buf[cols];
-    size_t offset = 0, n = 0, i;
-    bool skip = false;
+    size_t offset = 0, len = 0, i;
     for (;;) {
-        offset += n;
-        n = fread(buf, 1, sizeof(buf), file);
+        offset += len;
+        len = fread(buf, 1, sizeof(buf), file);
 
-        if ((flags & SKIP) && n == sizeof(buf)) {
-            if (zero(buf, n)) {
+        if ((flags & FLAG_SKIP) && len == sizeof(buf)) {
+            static bool skip = false;
+            if (zero(buf, len)) {
                 if (!skip) printf("*\n");
                 skip = true;
                 continue;
-            } else skip = false;
+            }
+            skip = false;
         }
 
-        if (flags & OFFSETS)
+        if (flags & FLAG_OFFSET)
             printf("%08zx:  ", offset);
-        for (i = 0; i < n; ++i) {
+
+        for (i = 0; i < len; ++i) {
             if (group && i && !(i % group)) printf(" ");
             printf("%02x ", buf[i]);
         }
 
-        if (flags & ASCII) {
-            for (i = n; i < cols; ++i)
+        if (flags & FLAG_ASCII) {
+            for (i = len; i < cols; ++i)
                 printf((group && !(i % group)) ? "    " : "   ");
             printf(" ");
-            for (i = 0; i < n; ++i)
+            for (i = 0; i < len; ++i)
                 printf("%c", isprint(buf[i]) ? buf[i] : '.');
         }
 
         printf("\n");
-        if (n < sizeof(buf)) break;
+        if (len < sizeof(buf)) break;
     }
+
     if (ferror(file)) {
         perror(path);
         return EXIT_FAILURE;
     }
-
     return EXIT_SUCCESS;
 }