From fc9d352b4cae7bd962723480d1323fb1b5f46e32 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 18 Jan 2026 02:38:16 +0100 Subject: [PATCH] cmp: fix -n causing all diffs to be output, implement size suffixes for SKIP function old new delta packed_usage 35802 35828 +26 cmp_main 616 595 -21 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/1 up/down: 26/-21) Total: 5 bytes Signed-off-by: Denys Vlasenko --- editors/cmp.c | 32 +++++++++++------ runtest | 2 ++ testsuite/cmp.tests | 54 ++++++++++++++++++++++++++++ testsuite/cmp/cmp-detects-difference | 9 ----- 4 files changed, 77 insertions(+), 20 deletions(-) create mode 100755 runtest create mode 100755 testsuite/cmp.tests delete mode 100644 testsuite/cmp/cmp-detects-difference diff --git a/editors/cmp.c b/editors/cmp.c index 332af6170..6afd4029b 100644 --- a/editors/cmp.c +++ b/editors/cmp.c @@ -18,13 +18,21 @@ //kbuild:lib-$(CONFIG_CMP) += cmp.o //usage:#define cmp_trivial_usage -//usage: "[-ls] [-n NUM] FILE1 [FILE2" IF_DESKTOP(" [SKIP1 [SKIP2]]") "]" +//usage: "[-l|s] [-n NUM] FILE1 [FILE2" IF_DESKTOP(" [SKIP1 [SKIP2]]") "]" //usage:#define cmp_full_usage "\n\n" //usage: "Compare FILE1 with FILE2 (or stdin)\n" -//usage: "\n -l Write the byte numbers (decimal) and values (octal)" -//usage: "\n for all differing bytes" +//usage: "\n -l Show decimal offset and octal byte value for differing bytes," +//usage: "\n don't stop on first mismatch" //usage: "\n -s Quiet" //usage: "\n -n NUM Compare at most NUM bytes" +//TODO? +// -b, --print-bytes print differing bytes +//Prints the first difference: +//FILE1 FILE2 differ: byte 5, line 2 is +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -b adds this +//and exits with exitcode 1. +// -i, --ignore-initial=SKIP1[:SKIP2] +//Same as SKIP1 [SKIP2] in argv, but here SKIP2 defaults to SKIP1, not to zero. /* BB_AUDIT SUSv3 (virtually) compliant -- uses nicer GNU format for -l. */ /* http://www.opengroup.org/onlinepubs/007904975/utilities/cmp.html */ @@ -32,9 +40,9 @@ #include "libbb.h" static const char fmt_eof[] ALIGN1 = "cmp: EOF on %s\n"; -static const char fmt_differ[] ALIGN1 = "%s %s differ: char %"OFF_FMT"u, line %u\n"; -// This fmt_l_opt uses gnu-isms. SUSv3 would be "%.0s%.0s%"OFF_FMT"u %o %o\n" -static const char fmt_l_opt[] ALIGN1 = "%.0s%.0s%"OFF_FMT"u %3o %3o\n"; +static const char fmt_differ[] ALIGN1 = "%s %s differ: byte %llu, line %u\n"; +// This fmt_l_opt is gnu-ism. SUSv3 is "%.0s%.0s%llu %o %o\n" +static const char fmt_l_opt[] ALIGN1 = "%.0s%.0s%llu %3o %3o\n"; #define OPT_STR "sln:+" #define CMP_OPT_s (1<<0) @@ -46,7 +54,7 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) { FILE *fp1, *fp2, *outfile = stdout; const char *filename1, *filename2 = "-"; - off_t skip1 = 0, skip2 = 0, char_pos = 0; + unsigned long long skip1 = 0, skip2 = 0, char_pos = 0; int line_pos = 1; /* Hopefully won't overflow... */ const char *fmt; int c1, c2; @@ -61,6 +69,7 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) IF_DESKTOP(":?4") IF_NOT_DESKTOP(":?2") ":l--s:s--l", +//TODO: -n MAXCOUNT should allow KMG suffixes &max_count ); #else @@ -86,9 +95,9 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) if (*++argv) { filename2 = *argv; if (ENABLE_DESKTOP && *++argv) { - skip1 = XATOOFF(*argv); + skip1 = xatoull_sfx(*argv, kmg_i_suffixes); if (*++argv) { - skip2 = XATOOFF(*argv); + skip2 = xatoull_sfx(*argv, kmg_i_suffixes); } } } @@ -146,8 +155,9 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) line_pos = c1; /* line_pos is unused in the -l case. */ } fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2); - if (opt) { /* This must be -l since not -s. */ - /* If we encountered an EOF, + if (opt & CMP_OPT_l) { + /* -l: do not stop on first mismatch. + * If we encountered an EOF, * the while check will catch it. */ continue; } diff --git a/runtest b/runtest new file mode 100755 index 000000000..1c8d4ce7d --- /dev/null +++ b/runtest @@ -0,0 +1,2 @@ +#!/bin/sh +cd testsuite && exec ./runtest "$@" diff --git a/testsuite/cmp.tests b/testsuite/cmp.tests new file mode 100755 index 000000000..6711dbede --- /dev/null +++ b/testsuite/cmp.tests @@ -0,0 +1,54 @@ +#!/bin/sh +# Copyright 2026 by Denys Vlasenko +# Licensed under GPLv2, see file LICENSE in this source tree. + +. ./testing.sh + +# testing "test name" "commands" "expected result" "file input" "stdin" + +testing "cmp -s" \ + 'cmp -s - input; echo $?' \ +"1 +" \ + "foo" \ + "bar" + +testing "cmp" \ + 'cmp - input; echo $?' \ +"- input differ: byte 1, line 1 +1 +" \ + "foo" \ + "bar" + +testing "cmp -n2" \ + 'cmp -n2 - input; echo $?' \ +"- input differ: byte 1, line 1 +1 +" \ + "foo" \ + "bar" + +testing "cmp -ln2" \ + 'cmp -ln2 - input; echo $?' \ +"\ +1 142 146 +2 141 157 +1 +" \ + "foo" \ + "bar" + +optional DESKTOP +testing "cmp -ln2 SKIP1 SKIP2 " \ + 'cmp -ln2 - input 1 1; echo $?' \ +"\ +1 141 157 +2 162 157 +1 +" \ + "foo" \ + "bar" +SKIP= + +exit $FAILCOUNT diff --git a/testsuite/cmp/cmp-detects-difference b/testsuite/cmp/cmp-detects-difference deleted file mode 100644 index b9bb628f1..000000000 --- a/testsuite/cmp/cmp-detects-difference +++ /dev/null @@ -1,9 +0,0 @@ -echo foo >foo -echo bar >bar -set +e -busybox cmp -s foo bar -if [ $? != 0 ] ; then - exit 0; -fi - -exit 1;