From cba96de2f384af168635abef06f3f106d51cbbc0 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Mon, 12 Sep 2016 15:09:35 -0400 Subject: Add -o flag to xx --- .bin/xx.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to '.bin/xx.c') 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 #include +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)) { -- cgit 1.4.1