diff --git a/list_same/list_same b/list_same/list_same index 9844607..8fa9c22 100755 --- a/list_same/list_same +++ b/list_same/list_same @@ -26,7 +26,7 @@ use Digest::MD5; use Getopt::Std; -getopts('s'); +getopts('es'); opendir(DIR, ".") or die "can't open . $!"; while (defined($file = readdir(DIR))) { @@ -40,6 +40,8 @@ foreach (@filelist) { } } +$start = $opt_s ? 1 : 0; + foreach $size (keys %sizes) { if (@{$sizes{$size}} > 1) { %md5s = (); @@ -47,16 +49,13 @@ foreach $size (keys %sizes) { push @{$md5s{&calc_md5($_)}}, $_; } foreach $key (keys %md5s) { - if ((@{$md5s{$key}} > 1) and !$opt_s ) { - foreach (@{$md5s{$key}}) { print "$_ "; } - print "\n"; - } - elsif ((@{$md5s{$key}} > 1) and $opt_s ) { + if ( @{$md5s{$key}} > 1 ) { @files = sort @{$md5s{$key}}; - for $i (1 .. $#files) { + for $i ($start .. $#files) { chomp $files[$i]; - print "$files[$i]\n"; + &output("$files[$i]\n"); } + unless($opt_s) { print "\n"; } } } } @@ -85,3 +84,17 @@ sub calc_md5($) { close(FILE); return $digest; } + +# escape output if necessary +sub output($) { + my $string = shift; + if ($opt_e) { + $string =~ s/\\/\\\\/g; + $string =~ s/ /\\ /g; + $string =~ s/'/\\'/g; + $string =~ s/"/\\"/g; + print "$string"; + } else { + print "$string"; + } +}