| 1 | # -*- Perl -*- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # File::Cmp - compare two files character by character like cmp(1) | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | package File::Cmp; | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use 5.008000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Scalar::Util qw/reftype/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | our @EXPORT_OK = qw/&fcmp/; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = '1.09'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # XXX 'skip' and 'limit' might be good parameters to add, to skip X | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # initial bytes, limit work to Y bytes of data to check | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 13 | ▶ | sub fcmp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fcmp: called CC=34 SCAR=35.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 13 | ▶ | croak 'fcmp needs two files' if @_ < 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 12 | my @files = splice @_, 0, 2; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 12 | ▶ | my $param = ( @_ == 1 and ref $_[0] eq 'HASH' ) ? $_[0] : {@_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 12 | ▶ | $param->{sizecheck} = 1 unless exists $param->{sizecheck}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 12 | ▶ | $param->{sizecheck} = 0 if exists $param->{tells}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 12 | ▶ | if ( $param->{fscheck} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | my @statbuf; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | for my $f (@files) { | |||||||||||||||||||||||||||||||||||||||||
| 33 | # stat has the handy property of chasing symlinks for us | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 7 | ▶ | my @devino = ( stat $f )[ 0, 1 ] or croak "could not stat: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 6 | push @statbuf, \@devino; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | if ( $statbuf[0][0] == $statbuf[1][0] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$statbuf[0][0] == $statbuf[1][0] and $statbuf[0][1] == $statbuf[1][1]
MC/DC50%1/2
$statbuf[0][0] == $statbuf[1][0] and $statbuf[0][1] == $statbuf[1][1]
$statbuf[0][0] == $statbuf[1][0]
$statbuf[0][1] == $statbuf[1][1]
Truth table66%2/3
A and B
A= $statbuf[0][0] == $statbuf[1][0]B= $statbuf[0][1] == $statbuf[1][1]
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | and $statbuf[0][1] == $statbuf[1][1] ) { | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | ${ $param->{reason} } = 'fscheck' if exists $param->{reason}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | return 1; # assume files identical as both dev and inode match | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # The files are probably not identical if they differ in size; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # however, offer means to turn this check off if -s for some reason is | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # incorrect (or if 'tells' is on so we need to find roughly where the | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # difference is in the files). | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 9 | ▶ | if ( $param->{sizecheck} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition71%5/7
$param->{"sizecheck"} and (-s $files[0] // -1) != (-s $files[1] // -2)
-s $files[0] // -1
-s $files[1] // -2
MC/DC50%1/2
$param->{"sizecheck"} and (-s $files[0] // -1) != (-s $files[1] // -2)
$param->{"sizecheck"}
-s $files[0]
MC/DC0%0/1
-s $files[1] // -2
-s $files[1]
Truth table66%2/3
A and B
A= $param->{"sizecheck"}B= -s $files[0]
Truth table50%1/2
A
A= -s $files[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | and ( ( -s $files[0] ) // -1 ) != ( ( -s $files[1] ) // -2 ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | ▶ | ${ $param->{reason} } = 'size' if exists $param->{reason}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 5 | my @fhs; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 5 | for my $f (@files) { | |||||||||||||||||||||||||||||||||||||||||
| 56 | 8 | ▶ | if ( !defined reftype $f) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 7 | ▶ | open my $fh, '<', $f or croak "could not open $f: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 6 | push @fhs, $fh; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # Assume is a GLOB or something can readline on, XXX might want to | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # better check this | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | push @fhs, $f; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | ▶ | if ( exists $param->{binmode} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | binmode $fhs[-1], $param->{binmode} or croak "binmode failed: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | ▶ | local $/ = $param->{RS} if exists $param->{RS}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 72 | 18 | my $eof1 = eof $fhs[0]; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 18 | my $eof2 = eof $fhs[1]; | |||||||||||||||||||||||||||||||||||||||||
| 74 | # Done if both files are at EOF; otherwise assume they differ if one | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # completes before the other (this second case would normally be | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # optimized away by the -s test, above). | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 18 | ▶ | last if $eof1 and $eof2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$eof1 and $eof2
MC/DC50%1/2
$eof1 and $eof2
$eof1
$eof2
Truth table66%2/3
A and B
A= $eof1B= $eof2
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 17 | ▶ | if ( $eof1 xor $eof2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$eof1 xor $eof2
MC/DC50%1/2
$eof1 xor $eof2
$eof1
$eof2
Truth table50%2/4
A xor B
A= $eof1B= $eof2
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | ▶ | ${ $param->{reason} } = 'eof' if exists $param->{reason}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | @{ $param->{tells} } = ( tell $fhs[0], tell $fhs[1] ) | |||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | ▶ | if exists $param->{tells}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 16 | my $this = readline $fhs[0]; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 16 | ▶ | croak "error reading from first file: $!" if !defined $this; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 16 | my $that = readline $fhs[1]; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 16 | ▶ | croak "error reading from second file: $!" if !defined $that; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 16 | ▶ | if ( $this ne $that ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | @{ $param->{tells} } = ( tell $fhs[0], tell $fhs[1] ) | |||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | if exists $param->{tells}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | ${ $param->{reason} } = 'diff' if exists $param->{reason}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | return 1; # assume files identical if get this far | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||||||||||||||||