xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/File/Compare.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage File::Compare;
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateuse 5.006;
4*0Sstevel@tonic-gateuse strict;
5*0Sstevel@tonic-gateuse warnings;
6*0Sstevel@tonic-gateour($VERSION, @ISA, @EXPORT, @EXPORT_OK, $Too_Big);
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gaterequire Exporter;
9*0Sstevel@tonic-gateuse Carp;
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gate$VERSION = '1.1003';
12*0Sstevel@tonic-gate@ISA = qw(Exporter);
13*0Sstevel@tonic-gate@EXPORT = qw(compare);
14*0Sstevel@tonic-gate@EXPORT_OK = qw(cmp compare_text);
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gate$Too_Big = 1024 * 1024 * 2;
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gatesub compare {
19*0Sstevel@tonic-gate    croak("Usage: compare( file1, file2 [, buffersize]) ")
20*0Sstevel@tonic-gate      unless(@_ == 2 || @_ == 3);
21*0Sstevel@tonic-gate
22*0Sstevel@tonic-gate    my ($from,$to,$size) = @_;
23*0Sstevel@tonic-gate    my $text_mode = defined($size) && (ref($size) eq 'CODE' || $size < 0);
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate    my ($fromsize,$closefrom,$closeto);
26*0Sstevel@tonic-gate    local (*FROM, *TO);
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate    croak("from undefined") unless (defined $from);
29*0Sstevel@tonic-gate    croak("to undefined") unless (defined $to);
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate    if (ref($from) &&
32*0Sstevel@tonic-gate        (UNIVERSAL::isa($from,'GLOB') || UNIVERSAL::isa($from,'IO::Handle'))) {
33*0Sstevel@tonic-gate	*FROM = *$from;
34*0Sstevel@tonic-gate    } elsif (ref(\$from) eq 'GLOB') {
35*0Sstevel@tonic-gate	*FROM = $from;
36*0Sstevel@tonic-gate    } else {
37*0Sstevel@tonic-gate	open(FROM,"<$from") or goto fail_open1;
38*0Sstevel@tonic-gate	unless ($text_mode) {
39*0Sstevel@tonic-gate	    binmode FROM;
40*0Sstevel@tonic-gate	    $fromsize = -s FROM;
41*0Sstevel@tonic-gate	}
42*0Sstevel@tonic-gate	$closefrom = 1;
43*0Sstevel@tonic-gate    }
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate    if (ref($to) &&
46*0Sstevel@tonic-gate        (UNIVERSAL::isa($to,'GLOB') || UNIVERSAL::isa($to,'IO::Handle'))) {
47*0Sstevel@tonic-gate	*TO = *$to;
48*0Sstevel@tonic-gate    } elsif (ref(\$to) eq 'GLOB') {
49*0Sstevel@tonic-gate	*TO = $to;
50*0Sstevel@tonic-gate    } else {
51*0Sstevel@tonic-gate	open(TO,"<$to") or goto fail_open2;
52*0Sstevel@tonic-gate	binmode TO unless $text_mode;
53*0Sstevel@tonic-gate	$closeto = 1;
54*0Sstevel@tonic-gate    }
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate    if (!$text_mode && $closefrom && $closeto) {
57*0Sstevel@tonic-gate	# If both are opened files we know they differ if their size differ
58*0Sstevel@tonic-gate	goto fail_inner if $fromsize != -s TO;
59*0Sstevel@tonic-gate    }
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate    if ($text_mode) {
62*0Sstevel@tonic-gate	local $/ = "\n";
63*0Sstevel@tonic-gate	my ($fline,$tline);
64*0Sstevel@tonic-gate	while (defined($fline = <FROM>)) {
65*0Sstevel@tonic-gate	    goto fail_inner unless defined($tline = <TO>);
66*0Sstevel@tonic-gate	    if (ref $size) {
67*0Sstevel@tonic-gate		# $size contains ref to comparison function
68*0Sstevel@tonic-gate		goto fail_inner if &$size($fline, $tline);
69*0Sstevel@tonic-gate	    } else {
70*0Sstevel@tonic-gate		goto fail_inner if $fline ne $tline;
71*0Sstevel@tonic-gate	    }
72*0Sstevel@tonic-gate	}
73*0Sstevel@tonic-gate	goto fail_inner if defined($tline = <TO>);
74*0Sstevel@tonic-gate    }
75*0Sstevel@tonic-gate    else {
76*0Sstevel@tonic-gate	unless (defined($size) && $size > 0) {
77*0Sstevel@tonic-gate	    $size = $fromsize || -s TO || 0;
78*0Sstevel@tonic-gate	    $size = 1024 if $size < 512;
79*0Sstevel@tonic-gate	    $size = $Too_Big if $size > $Too_Big;
80*0Sstevel@tonic-gate	}
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate	my ($fr,$tr,$fbuf,$tbuf);
83*0Sstevel@tonic-gate	$fbuf = $tbuf = '';
84*0Sstevel@tonic-gate	while(defined($fr = read(FROM,$fbuf,$size)) && $fr > 0) {
85*0Sstevel@tonic-gate	    unless (defined($tr = read(TO,$tbuf,$fr)) && $tbuf eq $fbuf) {
86*0Sstevel@tonic-gate		goto fail_inner;
87*0Sstevel@tonic-gate	    }
88*0Sstevel@tonic-gate	}
89*0Sstevel@tonic-gate	goto fail_inner if defined($tr = read(TO,$tbuf,$size)) && $tr > 0;
90*0Sstevel@tonic-gate    }
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate    close(TO) || goto fail_open2 if $closeto;
93*0Sstevel@tonic-gate    close(FROM) || goto fail_open1 if $closefrom;
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate    return 0;
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate  # All of these contortions try to preserve error messages...
98*0Sstevel@tonic-gate  fail_inner:
99*0Sstevel@tonic-gate    close(TO) || goto fail_open2 if $closeto;
100*0Sstevel@tonic-gate    close(FROM) || goto fail_open1 if $closefrom;
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate    return 1;
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate  fail_open2:
105*0Sstevel@tonic-gate    if ($closefrom) {
106*0Sstevel@tonic-gate	my $status = $!;
107*0Sstevel@tonic-gate	$! = 0;
108*0Sstevel@tonic-gate	close FROM;
109*0Sstevel@tonic-gate	$! = $status unless $!;
110*0Sstevel@tonic-gate    }
111*0Sstevel@tonic-gate  fail_open1:
112*0Sstevel@tonic-gate    return -1;
113*0Sstevel@tonic-gate}
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gatesub cmp;
116*0Sstevel@tonic-gate*cmp = \&compare;
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gatesub compare_text {
119*0Sstevel@tonic-gate    my ($from,$to,$cmp) = @_;
120*0Sstevel@tonic-gate    croak("Usage: compare_text( file1, file2 [, cmp-function])")
121*0Sstevel@tonic-gate	unless @_ == 2 || @_ == 3;
122*0Sstevel@tonic-gate    croak("Third arg to compare_text() function must be a code reference")
123*0Sstevel@tonic-gate	if @_ == 3 && ref($cmp) ne 'CODE';
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate    # Using a negative buffer size puts compare into text_mode too
126*0Sstevel@tonic-gate    $cmp = -1 unless defined $cmp;
127*0Sstevel@tonic-gate    compare($from, $to, $cmp);
128*0Sstevel@tonic-gate}
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate1;
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate__END__
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate=head1 NAME
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gateFile::Compare - Compare files or filehandles
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate=head1 SYNOPSIS
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate  	use File::Compare;
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate	if (compare("file1","file2") == 0) {
143*0Sstevel@tonic-gate	    print "They're equal\n";
144*0Sstevel@tonic-gate	}
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate=head1 DESCRIPTION
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gateThe File::Compare::compare function compares the contents of two
149*0Sstevel@tonic-gatesources, each of which can be a file or a file handle.  It is exported
150*0Sstevel@tonic-gatefrom File::Compare by default.
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gateFile::Compare::cmp is a synonym for File::Compare::compare.  It is
153*0Sstevel@tonic-gateexported from File::Compare only by request.
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gateFile::Compare::compare_text does a line by line comparison of the two
156*0Sstevel@tonic-gatefiles. It stops as soon as a difference is detected. compare_text()
157*0Sstevel@tonic-gateaccepts an optional third argument: This must be a CODE reference to
158*0Sstevel@tonic-gatea line comparison function, which returns 0 when both lines are considered
159*0Sstevel@tonic-gateequal. For example:
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate    compare_text($file1, $file2)
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gateis basically equivalent to
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate    compare_text($file1, $file2, sub {$_[0] ne $_[1]} )
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate=head1 RETURN
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gateFile::Compare::compare and its sibling functions return 0 if the files
170*0Sstevel@tonic-gateare equal, 1 if the files are unequal, or -1 if an error was encountered.
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate=head1 AUTHOR
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gateFile::Compare was written by Nick Ing-Simmons.
175*0Sstevel@tonic-gateIts original documentation was written by Chip Salzenberg.
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate=cut
178*0Sstevel@tonic-gate
179