aboutsummaryrefslogtreecommitdiff
path: root/util-linux/hexdump_xxd.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-01-25 16:50:30 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2017-01-25 16:50:30 +0100
commit62a9b18547462a657ffb18cf03d5cfdcf774c905 (patch)
tree5c6a660cd8066b1ed5944a4b9cb2717e89573be7 /util-linux/hexdump_xxd.c
parentcb8e84e65a254dcba4939e237b93d03c46ae56aa (diff)
downloadbusybox-w32-62a9b18547462a657ffb18cf03d5cfdcf774c905.tar.gz
busybox-w32-62a9b18547462a657ffb18cf03d5cfdcf774c905.tar.bz2
busybox-w32-62a9b18547462a657ffb18cf03d5cfdcf774c905.zip
xxd: implement -p
While at it, tweaked hexdump --help function old new delta xxd_main 364 414 +50 packed_usage 31097 31114 +17 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'util-linux/hexdump_xxd.c')
-rw-r--r--util-linux/hexdump_xxd.c30
1 files changed, 21 insertions, 9 deletions
diff --git a/util-linux/hexdump_xxd.c b/util-linux/hexdump_xxd.c
index 813e7fe57..cc34ea649 100644
--- a/util-linux/hexdump_xxd.c
+++ b/util-linux/hexdump_xxd.c
@@ -46,9 +46,11 @@
46//usage: "Hex dump FILE (or stdin)\n" 46//usage: "Hex dump FILE (or stdin)\n"
47//usage: "\n -g N Bytes per group" 47//usage: "\n -g N Bytes per group"
48//usage: "\n -c N Bytes per line" 48//usage: "\n -c N Bytes per line"
49//usage: "\n -p Show only hex bytes, assumes -c30"
49// exactly the same help text lines in hexdump and xxd: 50// exactly the same help text lines in hexdump and xxd:
50//usage: "\n -l LENGTH Interpret only LENGTH bytes of input" 51//usage: "\n -l LENGTH Show only first LENGTH bytes"
51//usage: "\n -s OFFSET Skip OFFSET bytes" 52//usage: "\n -s OFFSET Skip OFFSET bytes"
53// TODO: implement -r (see hexdump -R)
52 54
53#include "libbb.h" 55#include "libbb.h"
54#include "dump.h" 56#include "dump.h"
@@ -70,11 +72,12 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
70#define OPT_l (1 << 0) 72#define OPT_l (1 << 0)
71#define OPT_s (1 << 1) 73#define OPT_s (1 << 1)
72#define OPT_a (1 << 2) 74#define OPT_a (1 << 2)
75#define OPT_p (1 << 3)
73 opt_complementary = "?1"; /* 1 argument max */ 76 opt_complementary = "?1"; /* 1 argument max */
74 opt = getopt32(argv, "l:s:ag:+c:+", &opt_l, &opt_s, &bytes, &cols); 77 opt = getopt32(argv, "l:s:apg:+c:+", &opt_l, &opt_s, &bytes, &cols);
75 argv += optind; 78 argv += optind;
76 79
77// dumper->dump_vflag = ALL; // default 80 dumper->dump_vflag = ALL;
78// if (opt & OPT_a) 81// if (opt & OPT_a)
79// dumper->dump_vflag = SKIPNUL; ..does not exist 82// dumper->dump_vflag = SKIPNUL; ..does not exist
80 if (opt & OPT_l) { 83 if (opt & OPT_l) {
@@ -93,9 +96,16 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
93 //BUGGY for /proc/version (unseekable?) 96 //BUGGY for /proc/version (unseekable?)
94 } 97 }
95 98
96 bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: " 99 if (opt & OPT_p) {
97 if (cols == 0) 100 if (cols == 0)
98 cols = 16; 101 cols = 30;
102 bytes = cols; /* -p ignores -gN */
103 } else {
104 if (cols == 0)
105 cols = 16;
106 bb_dump_add(dumper, "\"%08.8_ax: \""); // "address: "
107 }
108
99 if (bytes < 1 || bytes >= cols) { 109 if (bytes < 1 || bytes >= cols) {
100 sprintf(buf, "%u/1 \"%%02x\"", cols); // cols * "xx" 110 sprintf(buf, "%u/1 \"%%02x\"", cols); // cols * "xx"
101 bb_dump_add(dumper, buf); 111 bb_dump_add(dumper, buf);
@@ -109,7 +119,7 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
109#define BS "/1 \"%02x \"" 119#define BS "/1 \"%02x \""
110#define B "/1 \"%02x\"" 120#define B "/1 \"%02x\""
111 unsigned i; 121 unsigned i;
112 char *bigbuf = xmalloc(1 + cols * (sizeof(BS)-1)); 122 char *bigbuf = xmalloc(cols * (sizeof(BS)-1));
113 char *p = bigbuf; 123 char *p = bigbuf;
114 for (i = 1; i <= cols; i++) { 124 for (i = 1; i <= cols; i++) {
115 if (i == cols || i % bytes) 125 if (i == cols || i % bytes)
@@ -125,8 +135,10 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
125 free(bigbuf); 135 free(bigbuf);
126 } 136 }
127 137
128 sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n" 138 if (!(opt & OPT_p)) {
129 bb_dump_add(dumper, buf); 139 sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n"
140 bb_dump_add(dumper, buf);
141 }
130 142
131 return bb_dump_dump(dumper, argv); 143 return bb_dump_dump(dumper, argv);
132} 144}