#!/usr/bin/perl # # lf - list the last modified nondirectory # # $Id$ # see also: ./lm use strict; use warnings; use Getopt::Std; use File::Find qw(find); my @opts = qw(h d e f n: o T r v); my %is_own_opt = map { /([^:]+)/ => 1 } @opts; my @alphanum = ( 'a' .. 'z', 'A' .. 'Z' ); my @ls_opts = grep { !$is_own_opt{$_} } @alphanum; # https://groups.google.com/d/msg/rec.humor.funny/Q-HG4LpW564/eRS-lylC2hkJ my %opt; getopts( join( '', @opts, @ls_opts ), \%opt ); # modifies @ARGV @ls_opts = map { "-$_" } grep { !$is_own_opt{$_} } keys %opt; defined( $opt{n} ) or $opt{n} = 1; $opt{n} =~ /\D/ and HELP_MESSAGE('the -n option requires a positive integer'); HELP_MESSAGE() if $opt{h}; warn join( ' ', @_ ), "\n\n" if @_; sub HELP_MESSAGE { print STDERR < 1 } @_; find( sub { $File::Find::prune = !$opt{r} && !$is_arg{$File::Find::name}; return if $visited{$File::Find::name}++; return if $opt{f} && !-f $_; return if $opt{T} && !-T $_; return if !$opt{d} && -d $_; warn "using $File::Find::name\n" if $opt{v}; my $mtime = ( lstat($_) )[9]; my $inserted = 0; foreach my $i ( 0 .. $#latest_files ) { next if ( $opt{o} ? $mtime >= $latest_mtimes[$i] : $mtime <= $latest_mtimes[$i] ); warn "$File::Find::name (mtime $mtime) is ", ( $opt{o} ? 'older' : 'newer' ), " than $latest_files[$i] (mtime $latest_mtimes[$i])\n" if $opt{v}; warn "adding at index $i: $File::Find::name\n" if $opt{v}; splice( @latest_files, $i, 0, $File::Find::name ); splice( @latest_mtimes, $i, 0, $mtime ); $inserted = 1; last; } my $l = scalar(@latest_files); if ( !$inserted && ( $opt{n} > $l ) ) { push( @latest_files, $File::Find::name ); push( @latest_mtimes, $mtime ); ++$l; warn "adding at end, index $l: $File::Find::name\n" if $opt{v}; } elsif ( $inserted && ( $opt{n} < $l ) ) { warn "removing at index $l: $latest_files[$l-1]\n" if $opt{v}; pop(@latest_files); pop(@latest_mtimes); } }, @e ); @latest_files; } sub mtime { ( lstat( $_[0] ) )[9]; # with stat(), may produce undefined values }