diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-01-25 16:50:30 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-01-25 16:50:30 +0100 |
commit | 62a9b18547462a657ffb18cf03d5cfdcf774c905 (patch) | |
tree | 5c6a660cd8066b1ed5944a4b9cb2717e89573be7 | |
parent | cb8e84e65a254dcba4939e237b93d03c46ae56aa (diff) | |
download | busybox-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>
-rw-r--r-- | util-linux/hexdump.c | 19 | ||||
-rw-r--r-- | util-linux/hexdump_xxd.c | 30 |
2 files changed, 31 insertions, 18 deletions
diff --git a/util-linux/hexdump.c b/util-linux/hexdump.c index a4483a114..25f771201 100644 --- a/util-linux/hexdump.c +++ b/util-linux/hexdump.c | |||
@@ -41,20 +41,21 @@ | |||
41 | //usage: "[-bcCdefnosvx" IF_FEATURE_HEXDUMP_REVERSE("R") "] [FILE]..." | 41 | //usage: "[-bcCdefnosvx" IF_FEATURE_HEXDUMP_REVERSE("R") "] [FILE]..." |
42 | //usage:#define hexdump_full_usage "\n\n" | 42 | //usage:#define hexdump_full_usage "\n\n" |
43 | //usage: "Display FILEs (or stdin) in a user specified format\n" | 43 | //usage: "Display FILEs (or stdin) in a user specified format\n" |
44 | //usage: "\n -b One-byte octal display" | 44 | //usage: "\n -b 1-byte octal display" |
45 | //usage: "\n -c One-byte character display" | 45 | //usage: "\n -c 1-byte character display" |
46 | //usage: "\n -C Canonical hex+ASCII, 16 bytes per line" | 46 | //usage: "\n -d 2-byte decimal display" |
47 | //usage: "\n -d Two-byte decimal display" | 47 | //usage: "\n -o 2-byte octal display" |
48 | //usage: "\n -e FORMAT_STRING" | 48 | //usage: "\n -x 2-byte hex display" |
49 | //usage: "\n -C hex+ASCII 16 bytes per line" | ||
50 | //usage: "\n -v Show all (no dup folding)" | ||
51 | //usage: "\n -e FORMAT_STR Example: '16/1 \"%02x|\"\"\\n\"'" | ||
49 | //usage: "\n -f FORMAT_FILE" | 52 | //usage: "\n -f FORMAT_FILE" |
50 | // exactly the same help text lines in hexdump and xxd: | 53 | // exactly the same help text lines in hexdump and xxd: |
51 | //usage: "\n -n LENGTH Interpret only LENGTH bytes of input" | 54 | //usage: "\n -n LENGTH Show only first LENGTH bytes" |
52 | //usage: "\n -o Two-byte octal display" | ||
53 | //usage: "\n -s OFFSET Skip OFFSET bytes" | 55 | //usage: "\n -s OFFSET Skip OFFSET bytes" |
54 | //usage: "\n -v Display all input data" | ||
55 | //usage: "\n -x Two-byte hexadecimal display" | ||
56 | //usage: IF_FEATURE_HEXDUMP_REVERSE( | 56 | //usage: IF_FEATURE_HEXDUMP_REVERSE( |
57 | //usage: "\n -R Reverse of 'hexdump -Cv'") | 57 | //usage: "\n -R Reverse of 'hexdump -Cv'") |
58 | // TODO: NONCOMPAT!!! move -R to xxd -r | ||
58 | //usage: | 59 | //usage: |
59 | //usage:#define hd_trivial_usage | 60 | //usage:#define hd_trivial_usage |
60 | //usage: "FILE..." | 61 | //usage: "FILE..." |
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 | } |