replace find call with perl code
[openwrt.git] / scripts / timestamp.pl
1 #!/usr/bin/perl
2 use strict;
3 use File::stat;
4
5 sub crawl($$) {
6         my $path = shift;
7         my $options = shift;
8         my @results = $path;
9         opendir(DIR,$path);
10         foreach my $file (readdir(DIR)) {
11                 if ($file !~m/^(\.(svn|\.?)|CVS$options)$/) {
12                         push @results, crawl("$path/$file",$options);
13                 }
14         }
15         closedir(DIR);
16         return @results; 
17 }
18
19 sub get_ts($$) {
20         my $path = shift;
21         my $options = shift;
22         my $ts = 0;
23         my $fn = "";
24         my @search = crawl($path,$options);
25         while (@search) {
26                 my $file = shift @search;
27                 my $mtime = stat($file)->mtime;
28                 if ($mtime > $ts) {
29                         $ts = $mtime;
30                         $fn = $file;
31                 }
32         }
33         return ($ts, $fn);
34 }
35
36 (@ARGV > 0) or push @ARGV, ".";
37 my $ts = 0;
38 my $n = ".";
39 my %options;
40 while (@ARGV > 0) {
41         my $path = shift @ARGV;
42         if ($path =~ /^-x/) {
43                 my $str = shift @ARGV;
44                 $options{"-x"} .= "|".$str;
45         } elsif ($path =~ /^-/) {
46                 $options{$path} = 1;
47         } else {
48                 my ($tmp, $fname) = get_ts($path, $options{"-x"});
49                 if ($tmp > $ts) {
50                         if ($options{'-f'}) {
51                                 $n = $fname;
52                         } else {
53                                 $n = $path;
54                         }
55                         $ts = $tmp;
56                 }
57         }
58 }
59
60 if ($options{"-p"}) {
61         print "$n\n";
62 } elsif ($options{"-t"}) {
63         print "$ts\n";
64 } else {
65         print "$n\t$ts\n";
66 }