← Commit list

fc9d352b4

Denys Vlasenko
Denys Vlasenko
2026-01-18
SUCCESS Duration: 146s

Subject

cmp: fix -n causing all diffs to be output, implement size suffixes for SKIP

Binary

No binary (build failed)

Architecture & Compiler Flags

Architecture: x86_64-linux-gnu Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
gcc -Wp,-MD,applets/.usage.d -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer      -Iinclude -Iinclude -o applets/usage applets/usage.c

Build Log

(log yok)

Commit Diff GitHub →

editors/cmp.c+21 -11
@@ -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 <OCTAL1> <char1> <OCTAL2> <char2>
+// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -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;
}
runtest+2 -0
@@ -0,0 +1,2 @@
+#!/bin/sh
+cd testsuite && exec ./runtest "$@"
testsuite/cmp.tests+54 -0
@@ -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
testsuite/cmp/cmp-detects-difference+0 -9
@@ -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;