diff --git a/list_same/list_same b/list_same/list_same index 342f99f..e92af10 100755 --- a/list_same/list_same +++ b/list_same/list_same @@ -17,9 +17,9 @@ use Digest::MD5; use Getopt::Std; my %opts = (); -getopts('ehms', \%opts); +my @dirs; -if ( $opts{h} ) { &help; } +&cmdline; my @filelist = &getdir; if ( $opts{m} ) { @@ -32,15 +32,27 @@ if ( $opts{m} ) { ################################# # functions +sub cmdline { + getopts('ehms', \%opts); + if ( $opts{h} ) { &help; } + + map { if ( -d $_ ) { s/\/$//; push @dirs, $_; } else { die "$_ not a directory" } } @ARGV; + if ( scalar @dirs == 0 ) { + push @dirs, "."; + } +} + sub getdir { my ( $file, @filelist ); - opendir(DIR, ".") or die "can't open . $!"; - while (defined($file = readdir(DIR))) { - if ( -f $file ) { - push @filelist, $file; + foreach ( @dirs ) { + opendir(DIR, $_) or die "can't open $_: $!"; + while (defined($file = readdir(DIR))) { + if ( -f "$_/$file" ) { + push @filelist, "$_/$file"; + } } + closedir(DIR); } - closedir(DIR); return @filelist; } @@ -156,7 +168,7 @@ sub output_doubles(@) { foreach $key (keys %md5s) { if ( @{$md5s{$key}} > 1 ) { - @files = sort @{$md5s{$key}}; + @files = sort dirsort @{$md5s{$key}}; for $i ($start .. $#files) { chomp $files[$i]; &output("$files[$i]\n"); @@ -175,6 +187,34 @@ sub output($) { print "$string"; } +sub dirsort { + my $firstdir = $dirs[0]; + if ( $a =~ /^$firstdir\/[^\/]*$/ && $b =~ /^$firstdir\/[^\/]*$/ ) { + return &wardsort; + } elsif ( ( $a =~ /^$firstdir\/[^\/]*$/ ) && ! ( $b =~ /^$firstdir\/[^\/]*$/ ) ) { + return -1; + } elsif ( ! ( $a =~ /^$firstdir\/[^\/]*$/ ) && ( $b =~ /^$firstdir\/[^\/]*$/ ) ) { + return 1; + } else { + return &wardsort; + } +} + +sub wardsort { + my @a = split(/([0-9]+)/,$a); + my @b = split(/([0-9]+)/,$b); + + foreach my $x (@a) + { + my $y = shift @b; + my $r = ($x =~ /^[0-9]+$/ && $y =~ /^[0-9]+$/) ? + ($x <=> $y) : ($x cmp $y); + $r && return $r; + } + return -1 if (@b); + 0; +} + sub help { my $name = $0; $name =~ s/.*\///;