Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmp: helpful EOF messages #766

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions bin/cmp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ use constant EX_USAGE => 2;
use constant ST_INO => 1;
use constant ST_SIZE => 7;

my $Program = basename($0);
use constant READSZ => 10_000;

my $chunk_size = 10_000; # how many bytes in a gulp
my $Program = basename($0);

my $volume=1; # controlled by -s and -l

Expand Down Expand Up @@ -123,8 +123,8 @@ if (!$fh1 && !$fh2) {
# with the behavior when skip >=
# filesize.
if ($volume) {
warn "$Program: EOF on $file1\n" unless $stat1[ST_SIZE];
warn "$Program: EOF on $file2\n" unless $stat2[ST_SIZE];
warn "$Program: EOF on $file1 which is empty\n" unless $stat1[ST_SIZE];
warn "$Program: EOF on $file2 which is empty\n" unless $stat2[ST_SIZE];
}
exit EX_DIFFERENT;
}
Expand Down Expand Up @@ -167,8 +167,8 @@ if ($skip2) {
}
}

READ: while (defined ($read_in1 = sysread $fh1, $buffer1, $chunk_size)) {
$read_in2 = sysread $fh2, $buffer2, $chunk_size;
READ: while (defined ($read_in1 = sysread $fh1, $buffer1, READSZ)) {
$read_in2 = sysread $fh2, $buffer2, READSZ;
$read_in2 = 0 unless defined $read_in2; # sysread failed

my $checklength = min($read_in1, $read_in2);
Expand Down Expand Up @@ -199,12 +199,14 @@ READ: while (defined ($read_in1 = sysread $fh1, $buffer1, $chunk_size)) {
$lines_read += $buffer1 =~ tr[\n][\n];
}
$bytes_read += $checklength;
my $nlines = $lines_read + 1;
my $nbytes = $bytes_read + 1;

if ($read_in1 < $read_in2) {
warn "$Program: EOF on $file1\n" unless $saw_difference or !$volume;
warn "$Program: EOF on $file1 after byte $nbytes, in line $nlines\n" unless $saw_difference or !$volume;
exit EX_DIFFERENT;
} elsif ($read_in1 > $read_in2) {
warn "$Program: EOF on $file2\n" unless $saw_difference or !$volume;
warn "$Program: EOF on $file2 after byte $nbytes, in line $nlines\n" unless $saw_difference or !$volume;
exit EX_DIFFERENT;
} elsif ($read_in1 == 0) {
exit EX_DIFFERENT;
Expand Down
Loading