[git commit] xxd: fix -pANYTHING handling (-pc50 does not mean -p -c50)

Denys Vlasenko vda.linux at googlemail.com
Sun Nov 30 05:13:15 UTC 2025


commit: https://git.busybox.net/busybox/commit/?id=d755846906800ac4156ef4f83430ba8e69edb4df
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
packed_usage                                       35768   35802     +34
xxd_main                                            1447    1449      +2
.rodata                                           106438  106439      +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 37/0)               Total: 37 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 testsuite/xxd.tests      | 19 +++++++++++++++++++
 util-linux/hexdump_xxd.c | 19 ++++++++++++-------
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/testsuite/xxd.tests b/testsuite/xxd.tests
index 2c740abc8..a0a8dd1f1 100755
--- a/testsuite/xxd.tests
+++ b/testsuite/xxd.tests
@@ -31,6 +31,25 @@ testing 'xxd -p with 31 NULs' \
 	'' \
 	'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
 
+testing 'xxd -pc15 with 31 NULs' \
+	'xxd -pc15' \
+	"\
+000000000000000000000000000000000000000000000000000000000000
+00
+" \
+	'' \
+	'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
+
+testing 'xxd -p -c15 with 31 NULs' \
+	'xxd -p -c15' \
+	"\
+000000000000000000000000000000
+000000000000000000000000000000
+00
+" \
+	'' \
+	'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
+
 testing 'xxd -p -r' \
 	'xxd -p -r' \
 	'01234567765432100123456776543210' \
diff --git a/util-linux/hexdump_xxd.c b/util-linux/hexdump_xxd.c
index 1e8ecb6e6..783ca9b26 100644
--- a/util-linux/hexdump_xxd.c
+++ b/util-linux/hexdump_xxd.c
@@ -40,13 +40,17 @@
 //    -s [+][-]seek  start at <seek> bytes abs. (or +: rel.) infile offset.
 //    -u          use upper case hex letters.
 
+//xxd V1.10 accepts and ignores ANY string after -p (including "").
+//Essentially, the syntax is as if -p has an optional parameter.
+//Therefore: -pc50 != -p -c50
+
 //usage:#define xxd_trivial_usage
-//usage:       "[-pri] [-g N] [-c N] [-l LEN] [-s OFS] [-o OFS] [FILE]"
+//usage:       "[-ri] [-ps] [-g N] [-c N] [-l 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"
-//usage:     "\n	-c N		Bytes per line"
-//usage:     "\n	-p		Show only hex bytes, assumes -c30"
+//usage:     "\n	-g N		Bytes per group (default 2)"
+//usage:     "\n	-c N		Bytes per line (default:16, -ps:30, -i:12)"
+//usage:     "\n	-ps		Show only hex bytes (no offset/spaces)"
 //usage:     "\n	-i		C include file style"
 // exactly the same help text lines in hexdump and xxd:
 //usage:     "\n	-l LENGTH	Show only first LENGTH bytes"
@@ -240,8 +244,8 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
 
 	dumper = alloc_dumper();
 
-	opt = getopt32(argv, "^" "l:s:apirg:+c:+o:" "\0" "?1" /* 1 argument max */,
-			&opt_l, &opt_s, &bytes, &cols, &opt_o
+	opt = getopt32(argv, "^" "l:s:ap::irg:+c:+o:" "\0" "?1" /* 1 argument max */,
+			&opt_l, &opt_s, NULL, &bytes, &cols, &opt_o
 	);
 	argv += optind;
 
@@ -331,7 +335,8 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
 
 	if ((opt & OPT_i) && argv[0]) {
 		print_C_style(argv[0], "unsigned char %s");
-		printf("[] = {\n");
+		puts("[] = {");
+//TODO: if file does not exist, shouldn't print the above lines
 	}
 	r = bb_dump_dump(dumper, argv);
 	if (r == 0 && (opt & OPT_i) && argv[0]) {


More information about the busybox-cvs mailing list