[git commit] cmp: add KMG suffix for cmp -n MAXCOUNT

Denys Vlasenko vda.linux at googlemail.com
Fri Jul 3 06:14:23 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=71f9960b33f2faddd8b1c9cd1681208c6222e57e
branch: https://git.busybox.net/busybox/log/?h=master

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                                             595     631     +36
.rodata                                           107121  107120      -1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 36/-1)              Total: 35 bytes

Signed-off-by: Anubhav Kokane <dev.anubhavk at gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 editors/cmp.c       | 15 +++++++++------
 testsuite/cmp.tests | 27 +++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/editors/cmp.c b/editors/cmp.c
index 6afd4029b..ab2733400 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)
@@ -55,12 +55,13 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
 	FILE *fp1, *fp2, *outfile = stdout;
 	const char *filename1, *filename2 = "-";
 	unsigned long long skip1 = 0, skip2 = 0, char_pos = 0;
+	unsigned long long max_count = max_count;
 	int line_pos = 1; /* Hopefully won't overflow... */
 	const char *fmt;
 	int c1, c2;
 	unsigned opt;
 	int retval = 0;
-	int max_count = -1;
+	char *max_count_str;
 
 #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;
@@ -102,6 +102,9 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
 		}
 	}
 
+	if (opt & CMP_OPT_n)
+		max_count = xatoull_sfx(max_count_str, kmg_i_suffixes);
+
 	xfunc_error_retval = 2;  /* missing file results in exitcode 2 */
 	if (opt & CMP_OPT_s)
 		logmode = 0;  /* -s suppresses open error messages */
@@ -126,7 +129,7 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
 		while (skip2) { if (getc(fp2) == EOF) break; skip2--; }
 	}
 	do {
-		if (max_count >= 0 && --max_count < 0)
+		if ((opt & CMP_OPT_n) && max_count-- == 0)
 			break;
 		c1 = getc(fp1);
 		c2 = getc(fp2);
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 $?' \


More information about the busybox-cvs mailing list