summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2016-09-12 15:55:12 -0400
committerJune McEnroe <june@causal.agency>2016-09-12 15:55:12 -0400
commit47fb8cdd572f355f5e5c77d2ff2e9709d50ceead (patch)
tree6b7567a1e88bb4bcba2a307c0e7f040b0ab41260
parentAdd -a flag to xx (diff)
downloadsrc-47fb8cdd572f355f5e5c77d2ff2e9709d50ceead.tar.gz
src-47fb8cdd572f355f5e5c77d2ff2e9709d50ceead.zip
Add -s flag to xx
-rwxr-xr-x.bin/xx.c27
1 files changed, 24 insertions, 3 deletions
diff --git a/.bin/xx.c b/.bin/xx.c
index ad71ac98..30bf1708 100755
--- a/.bin/xx.c
+++ b/.bin/xx.c
@@ -3,14 +3,23 @@ exec cc -Weverything -Wno-vla -o ~/.bin/xx $0
 #endif
 
 #include <ctype.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 
+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,
 };
 
 int main(int argc, char **argv)
@@ -20,7 +29,7 @@ int main(int argc, char **argv)
     uint8_t flags = ASCII | OFFSETS;
     char *path = NULL;
 
-    while (getopt(argc, argv, "ac:g:o") > 0)
+    while (getopt(argc, argv, "ac:g:os") > 0)
         if (optopt == 'a') {
             flags ^= ASCII;
         } else if (optopt == 'c') {
@@ -30,6 +39,8 @@ int main(int argc, char **argv)
             group = (size_t) strtol(optarg, NULL, 10);
         } else if (optopt == 'o') {
             flags ^= OFFSETS;
+        } else if (optopt == 's') {
+            flags ^= SKIP;
         } else return EXIT_FAILURE;
     if (argc > optind)
         path = argv[optind];
@@ -41,10 +52,20 @@ int main(int argc, char **argv)
     }
 
     uint8_t buf[cols];
-    size_t offset = 0, n, i;
+    size_t offset = 0, n = 0, i;
+    bool skip = false;
     for (;;) {
+        offset += n;
         n = fread(buf, 1, sizeof(buf), file);
 
+        if ((flags & SKIP) && n == sizeof(buf)) {
+            if (zero(buf, n)) {
+                if (!skip) printf("*\n");
+                skip = true;
+                continue;
+            } else skip = false;
+        }
+
         if (flags & OFFSETS)
             printf("%08zx:  ", offset);
         for (i = 0; i < n; ++i) {
@@ -53,6 +74,7 @@ int main(int argc, char **argv)
         }
 
         if (flags & ASCII) {
+            // TODO: Fix alignment with group.
             for (i = n; i < cols; ++i)
                 printf("   ");
             printf(" ");
@@ -61,7 +83,6 @@ int main(int argc, char **argv)
         }
 
         printf("\n");
-        offset += n;
         if (n < sizeof(buf)) break;
     }
     if (ferror(file)) {