summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2016-09-12 15:20:15 -0400
committerJune McEnroe <june@causal.agency>2016-09-12 15:20:15 -0400
commitce538aaa20c285c82b7a7fc090a131237261ea09 (patch)
tree2a61330867b7c58b02a526d2cae569fbf5d9941b
parentAdd -o flag to xx (diff)
downloadsrc-ce538aaa20c285c82b7a7fc090a131237261ea09.tar.gz
src-ce538aaa20c285c82b7a7fc090a131237261ea09.zip
Add -a flag to xx
Diffstat (limited to '')
-rwxr-xr-x.bin/xx.c20
1 files changed, 16 insertions, 4 deletions
diff --git a/.bin/xx.c b/.bin/xx.c
index f0bc15af..ad71ac98 100755
--- a/.bin/xx.c
+++ b/.bin/xx.c
@@ -2,24 +2,28 @@
 exec cc -Weverything -Wno-vla -o ~/.bin/xx $0
 #endif
 
+#include <ctype.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 
 enum {
-    OFFSETS = 1,
+    ASCII = 1,
+    OFFSETS = 2,
 };
 
 int main(int argc, char **argv)
 {
     size_t cols = 16;
     size_t group = 8;
-    uint8_t flags = OFFSETS;
+    uint8_t flags = ASCII | OFFSETS;
     char *path = NULL;
 
-    while (getopt(argc, argv, "c:g:o") > 0)
-        if (optopt == 'c') {
+    while (getopt(argc, argv, "ac:g:o") > 0)
+        if (optopt == 'a') {
+            flags ^= ASCII;
+        } else if (optopt == 'c') {
             cols = (size_t) strtol(optarg, NULL, 10);
             if (!cols) return EXIT_FAILURE;
         } else if (optopt == 'g') {
@@ -48,6 +52,14 @@ int main(int argc, char **argv)
             printf("%02x ", buf[i]);
         }
 
+        if (flags & ASCII) {
+            for (i = n; i < cols; ++i)
+                printf("   ");
+            printf(" ");
+            for (i = 0; i < n; ++i)
+                printf("%c", isprint(buf[i]) ? buf[i] : '.');
+        }
+
         printf("\n");
         offset += n;
         if (n < sizeof(buf)) break;