---
 tests/xxd.test   |  2 ++
 toys/other/xxd.c | 19 +++++++++++++------
 2 files changed, 15 insertions(+), 6 deletions(-)
From 3967430df1899844534a90d556ea8b261ed0de6f Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Tue, 5 Jul 2016 15:05:09 -0700
Subject: [PATCH] Implement xxd -s.

---
 tests/xxd.test   |  2 ++
 toys/other/xxd.c | 19 +++++++++++++------
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/tests/xxd.test b/tests/xxd.test
index 187bda5..5f43b8b 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 0250f8f..99de2ee 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;
-- 
2.8.0.rc3.226.g39d4020

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to