[git commit] xxd: '-p -c 0' outputs a single line
Denys Vlasenko
vda.linux at googlemail.com
Mon Oct 5 19:18:44 UTC 2026
commit: https://git.busybox.net/busybox/commit/?id=f15a204bb28f930e211e49821d191f55c7c41123
branch: https://git.busybox.net/busybox/log/?h=master
vim's xxd allows the combination of options '-p -c 0' to output
a single line of text [1]. A use case is handling cryptographic
material [2].
Add this feature to BusyBox xxd.
[1] https://github.com/vim/vim/pull/9524
[2] https://stackoverflow.com/questions/42192490/aes-cbc-incorrect-usage
function old new delta
xxd_main 1496 1526 +30
packed_usage 35515 35511 -4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 30/-4) Total: 26 bytes
Signed-off-by: Ron Yorston <rmy at pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
testsuite/xxd.tests | 8 ++++++++
util-linux/hexdump_xxd.c | 11 ++++++++---
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/testsuite/xxd.tests b/testsuite/xxd.tests
index a0a8dd1f1..f785b5754 100755
--- a/testsuite/xxd.tests
+++ b/testsuite/xxd.tests
@@ -79,4 +79,12 @@ testing 'xxd -p -r ignores the nibble with 2nd char bad' \
36
"
+testing 'xxd -p -c0 outputs a single line' \
+ 'xxd -p -c0' \
+ "\
+68656c6c6f20776f726c640a68656c6c6f20776f726c640a68656c6c6f20776f726c640a
+" \
+ '' \
+ 'hello world\nhello world\nhello world\n'
+
exit $FAILCOUNT
diff --git a/util-linux/hexdump_xxd.c b/util-linux/hexdump_xxd.c
index 783ca9b26..6961c88e4 100644
--- a/util-linux/hexdump_xxd.c
+++ b/util-linux/hexdump_xxd.c
@@ -50,7 +50,7 @@
//usage: "Hex dump FILE (or stdin)\n"
//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 -ps Show only hex bytes (-c 0: output as one line)"
//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"
@@ -238,6 +238,7 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
unsigned bytes = 2;
unsigned cols = 0;
unsigned opt;
+ int omit_newline = FALSE;
int r;
setup_common_bufsiz();
@@ -278,8 +279,11 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
}
if (opt & OPT_p) {
- if (cols == 0)
+ if (cols == 0) {
+ // '-p -c 0' prints as a single line
+ omit_newline = opt & OPT_c;
cols = 30;
+ }
bytes = cols; /* -p ignores -gN */
} else {
if (cols == 0)
@@ -329,7 +333,8 @@ int xxd_main(int argc UNUSED_PARAM, char **argv)
sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols); // " ASCII\n"
bb_dump_add(dumper, buf);
} else {
- bb_dump_add(dumper, "\"\n\"");
+ if (!omit_newline)
+ bb_dump_add(dumper, "\"\n\"");
dumper->xxd_eofstring = "\n";
}
More information about the busybox-cvs
mailing list