timestamp.pl 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/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. open FIND, "find $path -type f -and -not -path \\*.svn\\* -and -not -path \\*CVS\\* $options 2>/dev/null |";
  15. while (<FIND>) {
  16. chomp;
  17. my $file = $_;
  18. next if -l $file;
  19. my @stat = stat $file;
  20. if ($stat[9] > $ts) {
  21. $ts = $stat[9];
  22. $fn = $file;
  23. }
  24. }
  25. close FIND;
  26. return ($ts, $fn);
  27. }
  28. (@ARGV > 0) or push @ARGV, ".";
  29. my $ts = 0;
  30. my $n = ".";
  31. my %options;
  32. while (@ARGV > 0) {
  33. my $path = shift @ARGV;
  34. if ($path =~ /^-x/) {
  35. my $str = shift @ARGV;
  36. $options{"findopts"} .= " -and -not -path '".$str."'"
  37. } elsif ($path =~ /^-f/) {
  38. $options{"findopts"} .= " -follow";
  39. } elsif ($path =~ /^-n/) {
  40. my $arg = $ARGV[0];
  41. $options{$path} = $arg;
  42. } elsif ($path =~ /^-/) {
  43. $options{$path} = 1;
  44. } else {
  45. my ($tmp, $fname) = get_ts($path, $options{"findopts"});
  46. if ($tmp > $ts) {
  47. if ($options{'-F'}) {
  48. $n = $fname;
  49. } else {
  50. $n = $path;
  51. }
  52. $ts = $tmp;
  53. }
  54. }
  55. }
  56. if ($options{"-n"}) {
  57. exit ($n eq $options{"-n"} ? 0 : 1);
  58. } elsif ($options{"-p"}) {
  59. print "$n\n";
  60. } elsif ($options{"-t"}) {
  61. print "$ts\n";
  62. } else {
  63. print "$n\t$ts\n";
  64. }