timestamp.pl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright (C) 2006 OpenWrt.org
  4. #
  5. # This is free software, licensed under the GNU General Public License v2.
  6. # See /LICENSE for more information.
  7. #
  8. use strict;
  9. sub get_ts($$) {
  10. my $path = shift;
  11. my $options = shift;
  12. my $ts = 0;
  13. my $fn = "";
  14. $path .= "/" if( -d $path);
  15. open FIND, "find $path -type f -and -not -path \\*/.svn\\* -and -not -path \\*CVS\\* $options 2>/dev/null |";
  16. while (<FIND>) {
  17. chomp;
  18. my $file = $_;
  19. next if -l $file;
  20. my $mt = (stat $file)[9];
  21. if ($mt > $ts) {
  22. $ts = $mt;
  23. $fn = $file;
  24. }
  25. }
  26. close FIND;
  27. return ($ts, $fn);
  28. }
  29. (@ARGV > 0) or push @ARGV, ".";
  30. my $ts = 0;
  31. my $n = ".";
  32. my %options;
  33. while (@ARGV > 0) {
  34. my $path = shift @ARGV;
  35. if ($path =~ /^-x/) {
  36. my $str = shift @ARGV;
  37. $options{"findopts"} .= " -and -not -path '".$str."'"
  38. } elsif ($path =~ /^-f/) {
  39. $options{"findopts"} .= " -follow";
  40. } elsif ($path =~ /^-n/) {
  41. my $arg = $ARGV[0];
  42. $options{$path} = $arg;
  43. } elsif ($path =~ /^-/) {
  44. $options{$path} = 1;
  45. } else {
  46. my ($tmp, $fname) = get_ts($path, $options{"findopts"});
  47. if ($tmp > $ts) {
  48. if ($options{'-F'}) {
  49. $n = $fname;
  50. } else {
  51. $n = $path;
  52. }
  53. $ts = $tmp;
  54. }
  55. }
  56. }
  57. if ($options{"-n"}) {
  58. exit ($n eq $options{"-n"} ? 0 : 1);
  59. } elsif ($options{"-p"}) {
  60. print "$n\n";
  61. } elsif ($options{"-t"}) {
  62. print "$ts\n";
  63. } else {
  64. print "$n\t$ts\n";
  65. }