From 579894bfd28ffb38f7dabc7862d4e7ebfade2865 Mon Sep 17 00:00:00 2001 From: Walter Lozano Date: Thu, 25 Nov 2021 13:11:32 -0300 Subject: [PATCH] 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 Signed-off-by: Denys Vlasenko --- editors/cmp.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/editors/cmp.c b/editors/cmp.c index e106d814e..9eaff2b8e 100644 --- a/editors/cmp.c +++ b/editors/cmp.c @@ -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;