123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- #include "libbb.h"
- static const char fmt_eof[] ALIGN1 = "cmp: EOF on %s\n";
- static const char fmt_differ[] ALIGN1 = "%s %s differ: char %"OFF_FMT"u, line %u\n";
- static const char fmt_l_opt[] ALIGN1 = "%.0s%.0s%"OFF_FMT"u %3o %3o\n";
- #define OPT_STR "sl"
- #define CMP_OPT_s (1<<0)
- #define CMP_OPT_l (1<<1)
- int cmp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int cmp_main(int argc UNUSED_PARAM, char **argv)
- {
- FILE *fp1, *fp2, *outfile = stdout;
- const char *filename1, *filename2 = "-";
- off_t skip1 = 0, skip2 = 0, char_pos = 0;
- int line_pos = 1;
- const char *fmt;
- int c1, c2;
- unsigned opt;
- int retval = 0;
- opt = getopt32(argv, "^"
- OPT_STR
- "\0" "-1"
- IF_DESKTOP(":?4")
- IF_NOT_DESKTOP(":?2")
- ":l--s:s--l"
- );
- argv += optind;
- filename1 = *argv;
- if (*++argv) {
- filename2 = *argv;
- if (ENABLE_DESKTOP && *++argv) {
- skip1 = XATOOFF(*argv);
- if (*++argv) {
- skip2 = XATOOFF(*argv);
- }
- }
- }
- xfunc_error_retval = 2;
- if (opt & CMP_OPT_s)
- logmode = 0;
- fp1 = xfopen_stdin(filename1);
- fp2 = xfopen_stdin(filename2);
- if (fp1 == fp2) {
-
- return 0;
- }
- logmode = LOGMODE_STDIO;
- if (opt & CMP_OPT_l)
- fmt = fmt_l_opt;
- else
- fmt = fmt_differ;
- if (ENABLE_DESKTOP) {
- while (skip1) { getc(fp1); skip1--; }
- while (skip2) { getc(fp2); skip2--; }
- }
- do {
- c1 = getc(fp1);
- c2 = getc(fp2);
- ++char_pos;
- if (c1 != c2) {
- retval = 1;
- if (c2 == EOF) {
-
- fp1 = fp2;
- filename1 = filename2;
- c1 = c2;
- }
- if (c1 == EOF) {
- die_if_ferror(fp1, filename1);
- fmt = fmt_eof;
- outfile = stderr;
-
- fflush_all();
- }
- if (!(opt & CMP_OPT_s)) {
- if (opt & CMP_OPT_l) {
- line_pos = c1;
- }
- fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2);
- if (opt) {
-
- continue;
- }
- }
- break;
- }
- if (c1 == '\n') {
- ++line_pos;
- }
- } while (c1 != EOF);
- die_if_ferror(fp1, filename1);
- die_if_ferror(fp2, filename2);
- fflush_stdout_and_exit(retval);
- }
|