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

Signed-off-by: Walter Lozano <[email protected]>
---
 editors/cmp.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/editors/cmp.c b/editors/cmp.c
index e106d814e..e219f589a 100644
--- a/editors/cmp.c
+++ b/editors/cmp.c
@@ -35,9 +35,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 +51,16 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
        int c1, c2;
        unsigned opt;
        int retval = 0;
+       char * n_arg = NULL;
+       int max_count = -1;
 
        opt = getopt32(argv, "^"
                        OPT_STR
                        "\0" "-1"
                        IF_DESKTOP(":?4")
                        IF_NOT_DESKTOP(":?2")
-                       ":l--s:s--l"
+                       ":l--s:s--l",
+                       &n_arg
        );
        argv += optind;
 
@@ -90,6 +94,9 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
        else
                fmt = fmt_differ;
 
+       if (opt & CMP_OPT_n)
+               max_count = atoi(n_arg);
+
        if (ENABLE_DESKTOP) {
                while (skip1) { getc(fp1); skip1--; }
                while (skip2) { getc(fp2); skip2--; }
@@ -133,7 +140,7 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
                if (c1 == '\n') {
                        ++line_pos;
                }
-       } while (c1 != EOF);
+       } while (c1 != EOF && (max_count == -1 || max_count >= 0 && char_pos < 
max_count));
 
        die_if_ferror(fp1, filename1);
        die_if_ferror(fp2, filename2);
-- 
2.30.2

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to