diff options
| author | Walter Lozano <walter.lozano@collabora.com> | 2021-11-25 13:11:32 -0300 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-12-17 22:07:06 +0100 |
| commit | 579894bfd28ffb38f7dabc7862d4e7ebfade2865 (patch) | |
| tree | c2a0e343265356be27641a2a137473698455afac | |
| parent | 00d10cb6eb47e73bd88ab7e884562b555462815f (diff) | |
| download | busybox-w32-579894bfd28ffb38f7dabc7862d4e7ebfade2865.tar.gz busybox-w32-579894bfd28ffb38f7dabc7862d4e7ebfade2865.tar.bz2 busybox-w32-579894bfd28ffb38f7dabc7862d4e7ebfade2865.zip | |
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>
| -rw-r--r-- | editors/cmp.c | 14 |
1 files 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 @@ | |||
| 18 | //kbuild:lib-$(CONFIG_CMP) += cmp.o | 18 | //kbuild:lib-$(CONFIG_CMP) += cmp.o |
| 19 | 19 | ||
| 20 | //usage:#define cmp_trivial_usage | 20 | //usage:#define cmp_trivial_usage |
| 21 | //usage: "[-ls] FILE1 [FILE2" IF_DESKTOP(" [SKIP1 [SKIP2]]") "]" | 21 | //usage: "[-ls] [-n NUM] FILE1 [FILE2" IF_DESKTOP(" [SKIP1 [SKIP2]]") "]" |
| 22 | //usage:#define cmp_full_usage "\n\n" | 22 | //usage:#define cmp_full_usage "\n\n" |
| 23 | //usage: "Compare FILE1 with FILE2 (or stdin)\n" | 23 | //usage: "Compare FILE1 with FILE2 (or stdin)\n" |
| 24 | //usage: "\n -l Write the byte numbers (decimal) and values (octal)" | 24 | //usage: "\n -l Write the byte numbers (decimal) and values (octal)" |
| 25 | //usage: "\n for all differing bytes" | 25 | //usage: "\n for all differing bytes" |
| 26 | //usage: "\n -s Quiet" | 26 | //usage: "\n -s Quiet" |
| 27 | //usage: "\n -n NUM Compare at most NUM bytes" | ||
| 27 | 28 | ||
| 28 | /* BB_AUDIT SUSv3 (virtually) compliant -- uses nicer GNU format for -l. */ | 29 | /* BB_AUDIT SUSv3 (virtually) compliant -- uses nicer GNU format for -l. */ |
| 29 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/cmp.html */ | 30 | /* 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 | |||
| 35 | // This fmt_l_opt uses gnu-isms. SUSv3 would be "%.0s%.0s%"OFF_FMT"u %o %o\n" | 36 | // This fmt_l_opt uses gnu-isms. SUSv3 would be "%.0s%.0s%"OFF_FMT"u %o %o\n" |
| 36 | static const char fmt_l_opt[] ALIGN1 = "%.0s%.0s%"OFF_FMT"u %3o %3o\n"; | 37 | static const char fmt_l_opt[] ALIGN1 = "%.0s%.0s%"OFF_FMT"u %3o %3o\n"; |
| 37 | 38 | ||
| 38 | #define OPT_STR "sl" | 39 | #define OPT_STR "sln:" |
| 39 | #define CMP_OPT_s (1<<0) | 40 | #define CMP_OPT_s (1<<0) |
| 40 | #define CMP_OPT_l (1<<1) | 41 | #define CMP_OPT_l (1<<1) |
| 42 | #define CMP_OPT_n (1<<2) | ||
| 41 | 43 | ||
| 42 | int cmp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 44 | int cmp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 43 | int cmp_main(int argc UNUSED_PARAM, char **argv) | 45 | int cmp_main(int argc UNUSED_PARAM, char **argv) |
| @@ -50,13 +52,15 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) | |||
| 50 | int c1, c2; | 52 | int c1, c2; |
| 51 | unsigned opt; | 53 | unsigned opt; |
| 52 | int retval = 0; | 54 | int retval = 0; |
| 55 | int max_count = -1; | ||
| 53 | 56 | ||
| 54 | opt = getopt32(argv, "^" | 57 | opt = getopt32(argv, "^" |
| 55 | OPT_STR | 58 | OPT_STR |
| 56 | "\0" "-1" | 59 | "\0" "-1:n+" |
| 57 | IF_DESKTOP(":?4") | 60 | IF_DESKTOP(":?4") |
| 58 | IF_NOT_DESKTOP(":?2") | 61 | IF_NOT_DESKTOP(":?2") |
| 59 | ":l--s:s--l" | 62 | ":l--s:s--l", |
| 63 | &max_count | ||
| 60 | ); | 64 | ); |
| 61 | argv += optind; | 65 | argv += optind; |
| 62 | 66 | ||
| @@ -95,6 +99,8 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) | |||
| 95 | while (skip2) { getc(fp2); skip2--; } | 99 | while (skip2) { getc(fp2); skip2--; } |
| 96 | } | 100 | } |
| 97 | do { | 101 | do { |
| 102 | if (max_count >= 0 && --max_count < 0) | ||
| 103 | break; | ||
| 98 | c1 = getc(fp1); | 104 | c1 = getc(fp1); |
| 99 | c2 = getc(fp2); | 105 | c2 = getc(fp2); |
| 100 | ++char_pos; | 106 | ++char_pos; |
