From 88fb25f0176b094da91385c02122aaf0497fe710 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 19 Sep 2022 09:51:53 -0500 Subject: [PATCH] Make -c 0 disable columns and -g 0 disable groups. Add a test that circa 1998 xxd doesn't pass, but newer ones should. Skip on host for now, fix when regularly testing on a distro that passes. --- tests/xxd.test | 6 ++++++ toys/other/xxd.c | 25 +++++++++++++------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/tests/xxd.test b/tests/xxd.test index d4ae10b8..d4862784 100755 --- a/tests/xxd.test +++ b/tests/xxd.test @@ -30,6 +30,12 @@ testing "-o 0x8000" "xxd -o 0x8000 file1" "00008000: 7468 6973 2069 7320 736f 6d testing "-p" "xxd -p file1" "7468697320697320736f6d6520746578740a\n" "" "" +# TODO: remove toyonly when distro catches up +toyonly testing "-pc0" "xxd -pc0" \ + "73686f77203830206865782064696769747320776974686f757420776f72647772617070696e670a\n" \ + "" "show 80 hex digits without wordwrapping\n" +toyonly testing "-pc0 long" "xxd -pc0 | wc -c" "97787\n" "" "$(seq 1 10000)" + testing "-s" "xxd -s 13 file1" "0000000d: 7465 7874 0a text.\n" "" "" testing "-r" "echo -e ' 00000000: 7468 6973 2069 7320 736f 6d65 2074 6578 this is some tex\n00000010: 740a t.' | xxd -r" "this is some text\n" "" "" diff --git a/toys/other/xxd.c b/toys/other/xxd.c index 1809a6db..bb0a8e26 100644 --- a/toys/other/xxd.c +++ b/toys/other/xxd.c @@ -10,7 +10,7 @@ * xxd -p "plain" output: * "4c696e75782076657273696f6e20342e392e302d342d616d643634202864" -USE_XXD(NEWTOY(xxd, ">1c#<0>256l#o#g#<1=2iprs#[!rs]", TOYFLAG_USR|TOYFLAG_BIN)) +USE_XXD(NEWTOY(xxd, ">1c#<0>256l#o#g#<0=2iprs#[!rs]", TOYFLAG_USR|TOYFLAG_BIN)) config XXD bool "xxd" @@ -26,7 +26,7 @@ config XXD -i Output include file (CSV hex bytes, plus C header/footer if not stdin) -l n Limit of n bytes before stopping (default is no limit) -o n Add n to display offset - -p Plain hexdump (30 bytes/line, no grouping) + -p Plain hexdump (30 bytes/line, no grouping. With -c 0 no wrap/group) -r Reverse operation: turn a hexdump into a binary file -s n Skip to offset n */ @@ -42,7 +42,7 @@ static void do_xxd(int fd, char *name) { long long pos = 0; long long limit = TT.l; - int i, len, space; + int i, len, space, c = TT.c ? : sizeof(toybuf); if (FLAG(s)) { xlseek(fd, TT.s, SEEK_SET); @@ -51,15 +51,16 @@ static void do_xxd(int fd, char *name) } while (0<(len = readall(fd, toybuf, - (limit && limit-pos=' ' && toybuf[i]<='~') ? toybuf[i] : '.'); } - putchar('\n'); + if (TT.c || !FLAG(p)) putchar('\n'); } + if (!TT.c && FLAG(p)) putchar('\n'); if (len<0) perror_exit("read"); } @@ -125,7 +127,7 @@ static void do_xxd_reverse(int fd, char *name) // A plain hexdump can have as many bytes per line as you like, // but a non-plain hexdump assumes garbage after it's seen the // specified number of bytes. - while (FLAG(p) || col < TT.c) { + while (FLAG(p) || !TT.c || col < TT.c) { int n1, n2; // If we're at EOF or EOL or we read some non-hex... @@ -155,10 +157,9 @@ static void do_xxd_reverse(int fd, char *name) void xxd_main(void) { - if (!TT.c) TT.c = FLAG(i) ? 12 : 16; - // Plain style is 30 bytes/line, no grouping. - if (FLAG(p)) TT.c = TT.g = 30; + if (!FLAG(c)) TT.c = FLAG(p) ? 30 : FLAG(i) ? 12 : 16; + if (FLAG(p) && !FLAG(g)) TT.g = TT.c; loopfiles(toys.optargs, FLAG(r) ? do_xxd_reverse : (FLAG(i) ? do_xxd_include : do_xxd)); -- 2.39.2