diff options
author | Elliott Hughes <enh@google.com> | 2016-07-05 15:05:09 -0700 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2016-07-05 17:48:17 -0500 |
commit | ff0e8cb15c39d59b0530ac0e0a1243a510ae5c06 (patch) | |
tree | 58c2d3349d50934f9180fee204e4fd6fd66542db | |
parent | 8a34cbb5666a80153ea06ba8d59f7406f311bb7a (diff) | |
download | toybox-ff0e8cb15c39d59b0530ac0e0a1243a510ae5c06.tar.gz |
Implement xxd -s.
-rw-r--r-- | tests/xxd.test | 2 | ||||
-rw-r--r-- | toys/other/xxd.c | 19 |
2 files changed, 15 insertions, 6 deletions
diff --git a/tests/xxd.test b/tests/xxd.test index 187bda51..5f43b8b5 100644 --- a/tests/xxd.test +++ b/tests/xxd.test @@ -27,6 +27,8 @@ testing "-c 8 -g 3 file1" "xxd -c 8 -g 3 file1" \ testing "-p" "xxd -p file1" "7468697320697320736f6d6520746578740a\n" "" "" +testing "-s" "xxd -s 13 file1" "0000000d: 7465 7874 0a text.\n" "" "" + testing "-r" "xxd file1 | xxd -r" "this is some text\n" "" "" testing "-r -p" "xxd -p file1 | xxd -r -p" "this is some text\n" "" "" diff --git a/toys/other/xxd.c b/toys/other/xxd.c index 0250f8f8..99de2ee2 100644 --- a/toys/other/xxd.c +++ b/toys/other/xxd.c @@ -4,17 +4,14 @@ * * No obvious standard, output looks like: * 0000000: 4c69 6e75 7820 7665 7273 696f 6e20 332e Linux version 3. - * - * TODO: support for reversing a hexdump back into the original data. - * TODO: -s seek -USE_XXD(NEWTOY(xxd, ">1c#<1>4096=16l#g#<1=2pr", TOYFLAG_USR|TOYFLAG_BIN)) +USE_XXD(NEWTOY(xxd, ">1c#<1>4096=16l#g#<1=2prs#[!rs]", TOYFLAG_USR|TOYFLAG_BIN)) config XXD bool "xxd" default y help - usage: xxd [-c n] [-g n] [-l n] [-p] [-r] [file] + usage: xxd [-c n] [-g n] [-l n] [-p] [-r] [-s n] [file] Hexdump a file to stdout. If no file is listed, copy from stdin. Filename "-" is a synonym for stdin. @@ -24,12 +21,14 @@ config XXD -l n Limit of n bytes before stopping (default is no limit). -p Plain hexdump (30 bytes/line, no grouping). -r Reverse operation: turn a hexdump into a binary file. + -s n Skip to offset n. */ #define FOR_xxd #include "toys.h" GLOBALS( + long s; long g; long l; long c; @@ -38,9 +37,17 @@ GLOBALS( static void do_xxd(int fd, char *name) { long long pos = 0; + long long limit = TT.l; int i, len, space; - while (0<(len = readall(fd, toybuf, (TT.l && TT.l-pos<TT.c)?TT.l-pos:TT.c))) { + if (toys.optflags&FLAG_s) { + xlseek(fd, TT.s, SEEK_SET); + pos = TT.s; + if (limit) limit += TT.s; + } + + while (0<(len = readall(fd, toybuf, + (limit && limit-pos<TT.c)?limit-pos:TT.c))) { if (!(toys.optflags&FLAG_p)) printf("%08llx: ", pos); pos += len; space = 2*TT.c+TT.c/TT.g+1; |