xxd: implement -o DISPLAYOFFSET

function                                             old     new   delta
xxd_main                                             680     710     +30
xstrtoll                                               -      30     +30
bb_dump_dump                                        1511    1531     +20
rewrite                                              941     951     +10
packed_usage                                       33629   33639     +10
.rodata                                           103250  103252      +2
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 5/0 up/down: 102/0)             Total: 102 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2021-06-17 23:53:30 +02:00
parent a0f8076d19
commit 4d16161797
3 changed files with 22 additions and 11 deletions

View File

@@ -41,7 +41,7 @@
// -u use upper case hex letters.
//usage:#define xxd_trivial_usage
//usage: "[-pr] [-g N] [-c N] [-n LEN] [-s OFS] [FILE]"
//usage: "[-pr] [-g N] [-c N] [-n LEN] [-s OFS] [-o OFS] [FILE]"
//usage:#define xxd_full_usage "\n\n"
//usage: "Hex dump FILE (or stdin)\n"
//usage: "\n -g N Bytes per group"
@@ -50,6 +50,7 @@
// exactly the same help text lines in hexdump and xxd:
//usage: "\n -l LENGTH Show only first LENGTH bytes"
//usage: "\n -s OFFSET Skip OFFSET bytes"
//usage: "\n -o OFFSET Add OFFSET to displayed offset"
//usage: "\n -r Reverse (with -p, assumes no offsets in input)"
#include "libbb.h"
@@ -62,6 +63,9 @@
#define OPT_a (1 << 2)
#define OPT_p (1 << 3)
#define OPT_r (1 << 4)
#define OPT_g (1 << 5)
#define OPT_c (1 << 6)
#define OPT_o (1 << 7)
static void reverse(unsigned opt, unsigned cols, const char *filename)
{
@@ -127,15 +131,15 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
{
char buf[80];
dumper_t *dumper;
char *opt_l, *opt_s;
char *opt_l, *opt_s, *opt_o;
unsigned bytes = 2;
unsigned cols = 0;
unsigned opt;
dumper = alloc_dumper();
opt = getopt32(argv, "^" "l:s:aprg:+c:+" "\0" "?1" /* 1 argument max */,
&opt_l, &opt_s, &bytes, &cols
opt = getopt32(argv, "^" "l:s:aprg:+c:+o:" "\0" "?1" /* 1 argument max */,
&opt_l, &opt_s, &bytes, &cols, &opt_o
);
argv += optind;
@@ -158,6 +162,11 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
//BUGGY for /proc/version (unseekable?)
}
if (opt & OPT_o) {
/* -o accepts negative numbers too */
dumper->xxd_displayoff = xstrtoll(opt_o, /*base:*/ 0);
}
if (opt & OPT_p) {
if (cols == 0)
cols = 30;
@@ -206,7 +215,7 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
bb_dump_add(dumper, buf);
} else {
bb_dump_add(dumper, "\"\n\"");
dumper->eofstring = "\n";
dumper->xxd_eofstring = "\n";
}
return bb_dump_dump(dumper, argv);