From f74d384e241e9285cbdee67d9e238de1a3dc41ee Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thomas=20Wei=C3=9Fschuh?= Date: Sat, 12 Nov 2022 05:32:33 +0100 Subject: [PATCH] rev: allow zero-byte as separator Fixes #1868 --- tests/expected/misc/rev | 3 ++- tests/ts/misc/rev | 3 +++ text-utils/rev.1.adoc | 3 +++ text-utils/rev.c | 6 +++++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/expected/misc/rev b/tests/expected/misc/rev index 6387476379..bd8f465987 100644 --- a/tests/expected/misc/rev +++ b/tests/expected/misc/rev @@ -1,4 +1,5 @@ 53bbf0d98205319cee2ba589e205c68b 35484965b7a2fd45a471c0d80cb9752c cba -321 \ No newline at end of file +321 +cba|321 diff --git a/tests/ts/misc/rev b/tests/ts/misc/rev index 0b3b64c09d..5e849f14ac 100755 --- a/tests/ts/misc/rev +++ b/tests/ts/misc/rev @@ -27,5 +27,8 @@ for I in {0..512}; do printf "%s " {a..z}; done | \ $TS_CMD_REV | "$TS_HELPER_MD5" >> $TS_OUTPUT 2>> $TS_ERRLOG printf "abc\n123" | $TS_CMD_REV >> $TS_OUTPUT 2>> $TS_ERRLOG +echo >> $TS_OUTPUT +printf "abc\000123" | $TS_CMD_REV -0 | tr '\0' '|' >> $TS_OUTPUT 2>> $TS_ERRLOG +echo >> $TS_OUTPUT ts_finalize diff --git a/text-utils/rev.1.adoc b/text-utils/rev.1.adoc index 9076a7d374..3d40273900 100644 --- a/text-utils/rev.1.adoc +++ b/text-utils/rev.1.adoc @@ -58,6 +58,9 @@ This utility is a line-oriented tool and it uses in-memory allocated buffer for include::man-common/help-version.adoc[] +*-0*, *--zero*:: +_Zero termination_. Use the byte '\0' as line separator. + == SEE ALSO *tac*(1) diff --git a/text-utils/rev.c b/text-utils/rev.c index f69e47ef7b..fbf04d1a64 100644 --- a/text-utils/rev.c +++ b/text-utils/rev.c @@ -127,6 +127,7 @@ int main(int argc, char *argv[]) uintmax_t line; static const struct option longopts[] = { + { "zero", no_argument, NULL, '0' }, { "version", no_argument, NULL, 'V' }, { "help", no_argument, NULL, 'h' }, { NULL, 0, NULL, 0 } @@ -140,8 +141,11 @@ int main(int argc, char *argv[]) signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); - while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) + while ((ch = getopt_long(argc, argv, "Vh0", longopts, NULL)) != -1) switch(ch) { + case '0': + sep = L'\0'; + break; case 'V': print_version(EXIT_SUCCESS); case 'h': -- 2.47.3