- c1 = fgetc(fp1);
- c2 = fgetc(fp2);
- if (c1 != c2) {
- if (silent)
- return EXIT_FAILURE;
- if (c1 == EOF)
- printf("EOF on %s\n", filename1);
- else if (c2 == EOF)
- printf("EOF on %s\n", filename2);
- else
- printf("%s %s differ: char %d, line %d\n", filename1, filename2,
- char_pos, line_pos);
- return EXIT_FAILURE;
+ c1 = getc(fp1);
+ c2 = getc(fp2);
+ ++char_pos;
+ if (c1 != c2) { /* Remember -- a read error may have occurred. */
+ exit_val = 1; /* But assume the files are different for now. */
+ if (c2 == EOF) {
+ /* We know that fp1 isn't at EOF or in an error state. But to
+ * save space below, things are setup to expect an EOF in fp1
+ * if an EOF occurred. So, swap things around.
+ */
+ fp1 = fp2;
+ filename1 = filename2;
+ c1 = c2;
+ }
+ if (c1 == EOF) {
+ bb_xferror(fp1, filename1);
+ fmt = fmt_eof; /* Well, no error, so it must really be EOF. */
+ outfile = stderr;
+ /* There may have been output to stdout (option -l), so
+ * make sure we fflush before writing to stderr. */
+ bb_xfflush_stdout();
+ }
+ if (opt_flags != OPT_s) {
+ if (opt_flags == OPT_l) {
+ line_pos = c1; /* line_pos is unused in the -l case. */
+ }
+ bb_fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2);
+ if (opt_flags) { /* This must be -l since not -s. */
+ /* If we encountered and EOF, the while check will catch it. */
+ continue;
+ }
+ }
+ break;
+ }
+ if (c1 == '\n') {
+ ++line_pos;