[PATCH] cmp: add KMG suffix for cmp -n MAXCOUNT
Anubhav Kokane via busybox <[email protected]> Thu, 2 Jul 2026 17:32:29 +0000
| Newsgroups | gmane.linux.busybox |
|---|---|
| Message-ID | <[email protected]> |
Change input to getopt32 from int to char * so that max_count is parsed as string. Use xatol_sfx to convert string to long. function old new delta cmp_main 616 661 +45 xatoll_sfx - 32 +32 .rodata 101897 101896 -1 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 1/1 up/down: 77/-1) Total: 76 bytes Signed-off-by: Anubhav Kokane <[email protected]> --- editors/cmp.c | 13 ++++++++----- testsuite/cmp.tests | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/editors/cmp.c b/editors/cmp.c index 6afd4029b..e57a60efa 100644 --- a/editors/cmp.c +++ b/editors/cmp.c @@ -44,7 +44,7 @@ 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 OPT_STR "sln:" #define CMP_OPT_s (1<<0) #define CMP_OPT_l (1<<1) #define CMP_OPT_n (1<<2) @@ -60,7 +60,8 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) int c1, c2; unsigned opt; int retval = 0; - int max_count = -1; + long int max_count = -1; + char *max_count_str = NULL; #if !ENABLE_LONG_OPTS opt = getopt32(argv, "^" @@ -69,8 +70,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 + &max_count_str ); #else static const char cmp_longopts[] ALIGN1 = @@ -86,7 +86,7 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) IF_NOT_DESKTOP(":?2") ":l--s:s--l", cmp_longopts, - &max_count + &max_count_str ); #endif argv += optind; @@ -101,6 +101,9 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) } } } + + if (opt & CMP_OPT_n) + max_count = xatol_sfx(max_count_str, kmg_i_suffixes); xfunc_error_retval = 2; /* missing file results in exitcode 2 */ if (opt & CMP_OPT_s) diff --git a/testsuite/cmp.tests b/testsuite/cmp.tests index 6711dbede..42122a4df 100755 --- a/testsuite/cmp.tests +++ b/testsuite/cmp.tests @@ -29,6 +29,22 @@ testing "cmp -n2" \ "foo" \ "bar" +testing "cmp -n2k" \ + 'cmp -n2k - input; echo $?' \ +"- input differ: byte 1, line 1 +1 +" \ + "foo" \ + "bar" + +testing "cmp -n 1g" \ + 'cmp -n 1g - input; echo $?' \ +"- input differ: byte 1, line 1 +1 +" \ + "foo" \ + "bar" + testing "cmp -ln2" \ 'cmp -ln2 - input; echo $?' \ "\ @@ -39,6 +55,17 @@ testing "cmp -ln2" \ "foo" \ "bar" +testing "cmp -ln2M" \ + 'cmp -ln2M - input; echo $?' \ +"\ +1 142 146 +2 141 157 +3 162 157 +1 +" \ + "foo" \ + "bar" + optional DESKTOP testing "cmp -ln2 SKIP1 SKIP2 " \ 'cmp -ln2 - input 1 1; echo $?' \ -- 2.43.0