Skip to content

Commit 715e441

Browse files
authored
cmp: helpful EOF messages (briandfoy#766)
* It's trivial to provide extra descriptive output when files differ at EOF, as done by GNU cmp * Shell scripts running cmp would normally use the -s flag and check the exit code, i.e. the text description shouldn't be depended upon for portability %dd if=/dev/urandom bs=512 count=1 of=rnd %cp rnd rnd2 %echo >> rnd2 %touch empty %perl cmp rnd rnd2 # test1: non-empty files, eof on arg1 cmp: EOF on rnd after byte 512, in line 6 %perl cmp rnd2 rnd # test2: non-empty files, eof on arg2 cmp: EOF on rnd after byte 512, in line 6 %perl cmp empty rnd # test3: arg1 empty cmp: EOF on empty which is empty %perl cmp rnd empty # test4: arg2 empty cmp: EOF on empty which is empty
1 parent 437e404 commit 715e441

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

bin/cmp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ use constant EX_USAGE => 2;
4848
use constant ST_INO => 1;
4949
use constant ST_SIZE => 7;
5050

51-
my $Program = basename($0);
51+
use constant READSZ => 10_000;
5252

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

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

@@ -123,8 +123,8 @@ if (!$fh1 && !$fh2) {
123123
# with the behavior when skip >=
124124
# filesize.
125125
if ($volume) {
126-
warn "$Program: EOF on $file1\n" unless $stat1[ST_SIZE];
127-
warn "$Program: EOF on $file2\n" unless $stat2[ST_SIZE];
126+
warn "$Program: EOF on $file1 which is empty\n" unless $stat1[ST_SIZE];
127+
warn "$Program: EOF on $file2 which is empty\n" unless $stat2[ST_SIZE];
128128
}
129129
exit EX_DIFFERENT;
130130
}
@@ -167,8 +167,8 @@ if ($skip2) {
167167
}
168168
}
169169

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

174174
my $checklength = min($read_in1, $read_in2);
@@ -199,12 +199,14 @@ READ: while (defined ($read_in1 = sysread $fh1, $buffer1, $chunk_size)) {
199199
$lines_read += $buffer1 =~ tr[\n][\n];
200200
}
201201
$bytes_read += $checklength;
202+
my $nlines = $lines_read + 1;
203+
my $nbytes = $bytes_read + 1;
202204

203205
if ($read_in1 < $read_in2) {
204-
warn "$Program: EOF on $file1\n" unless $saw_difference or !$volume;
206+
warn "$Program: EOF on $file1 after byte $nbytes, in line $nlines\n" unless $saw_difference or !$volume;
205207
exit EX_DIFFERENT;
206208
} elsif ($read_in1 > $read_in2) {
207-
warn "$Program: EOF on $file2\n" unless $saw_difference or !$volume;
209+
warn "$Program: EOF on $file2 after byte $nbytes, in line $nlines\n" unless $saw_difference or !$volume;
208210
exit EX_DIFFERENT;
209211
} elsif ($read_in1 == 0) {
210212
exit EX_DIFFERENT;

0 commit comments

Comments
 (0)