summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2016-09-12 15:09:35 -0400
committerJune McEnroe <june@causal.agency>2016-09-12 15:09:35 -0400
commitcba96de2f384af168635abef06f3f106d51cbbc0 (patch)
treee3039c9befc8a24b4d7b47680589963b839e3974
parentAdd -g option to xx (diff)
downloadsrc-cba96de2f384af168635abef06f3f106d51cbbc0.tar.gz
src-cba96de2f384af168635abef06f3f106d51cbbc0.zip
Add -o flag to xx
-rwxr-xr-x.bin/xx.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/.bin/xx.c b/.bin/xx.c
index 3aac12f6..f0bc15af 100755
--- a/.bin/xx.c
+++ b/.bin/xx.c
@@ -7,18 +7,25 @@ exec cc -Weverything -Wno-vla -o ~/.bin/xx $0
 #include <stdlib.h>
 #include <unistd.h>
 
+enum {
+    OFFSETS = 1,
+};
+
 int main(int argc, char **argv)
 {
     size_t cols = 16;
     size_t group = 8;
+    uint8_t flags = OFFSETS;
     char *path = NULL;
 
-    while (getopt(argc, argv, "c:g:") > 0)
+    while (getopt(argc, argv, "c:g:o") > 0)
         if (optopt == 'c') {
             cols = (size_t) strtol(optarg, NULL, 10);
             if (!cols) return EXIT_FAILURE;
         } else if (optopt == 'g') {
             group = (size_t) strtol(optarg, NULL, 10);
+        } else if (optopt == 'o') {
+            flags ^= OFFSETS;
         } else return EXIT_FAILURE;
     if (argc > optind)
         path = argv[optind];
@@ -30,15 +37,19 @@ int main(int argc, char **argv)
     }
 
     uint8_t buf[cols];
+    size_t offset = 0, n, i;
     for (;;) {
-        size_t n = fread(buf, 1, sizeof(buf), file);
+        n = fread(buf, 1, sizeof(buf), file);
 
-        for (size_t i = 0; i < n; ++i) {
+        if (flags & OFFSETS)
+            printf("%08zx:  ", offset);
+        for (i = 0; i < n; ++i) {
             if (group && i && !(i % group)) printf(" ");
             printf("%02x ", buf[i]);
         }
 
         printf("\n");
+        offset += n;
         if (n < sizeof(buf)) break;
     }
     if (ferror(file)) {