cmp: add support for -n

Add support to for "-n" to cmp in order to compare at most n bytes.

function                                             old     new   delta
cmp_main                                             552     589     +37
.rodata                                           104198  104203      +5
packed_usage                                       34102   34074     -28
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 42/-28)             Total: 14 bytes

Signed-off-by: Walter Lozano <walter.lozano@collabora.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Walter Lozano 2021-11-25 13:11:32 -03:00 committed by Denys Vlasenko
parent 00d10cb6eb
commit 579894bfd2

View File

@ -18,12 +18,13 @@
//kbuild:lib-$(CONFIG_CMP) += cmp.o
//usage:#define cmp_trivial_usage
//usage: "[-ls] FILE1 [FILE2" IF_DESKTOP(" [SKIP1 [SKIP2]]") "]"
//usage: "[-ls] [-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 -s Quiet"
//usage: "\n -n NUM Compare at most NUM bytes"
/* BB_AUDIT SUSv3 (virtually) compliant -- uses nicer GNU format for -l. */
/* http://www.opengroup.org/onlinepubs/007904975/utilities/cmp.html */
@ -35,9 +36,10 @@ static const char fmt_differ[] ALIGN1 = "%s %s differ: char %"OFF_FMT"u, line %u
// 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";
#define OPT_STR "sl"
#define OPT_STR "sln:"
#define CMP_OPT_s (1<<0)
#define CMP_OPT_l (1<<1)
#define CMP_OPT_n (1<<2)
int cmp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int cmp_main(int argc UNUSED_PARAM, char **argv)
@ -50,13 +52,15 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
int c1, c2;
unsigned opt;
int retval = 0;
int max_count = -1;
opt = getopt32(argv, "^"
OPT_STR
"\0" "-1"
"\0" "-1:n+"
IF_DESKTOP(":?4")
IF_NOT_DESKTOP(":?2")
":l--s:s--l"
":l--s:s--l",
&max_count
);
argv += optind;
@ -95,6 +99,8 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
while (skip2) { getc(fp2); skip2--; }
}
do {
if (max_count >= 0 && --max_count < 0)
break;
c1 = getc(fp1);
c2 = getc(fp2);
++char_pos;