1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
#! /usr/bin/perl
#
# Copyright (C) Heinz-Josef Claes (2000,2004)
# hjclaes@web.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
push @VERSION, '$Id: llt 335 2004-07-23 03:56:18Z hjc $ ';
use strict;
sub libPath
{
my $file = shift;
my $dir;
# Falls Datei selbst ein symlink ist, solange folgen, bis aufgelst
if (-f $file)
{
while (-l $file)
{
my $link = readlink($file);
if (substr($link, 0, 1) ne "/")
{
$file =~ s/[^\/]+$/$link/;
}
else
{
$file = $link;
}
}
($dir, $file) = &splitFileDir($file);
$file = "/$file";
}
else
{
print STDERR "<$file> does not exist!\n";
exit 1;
}
$dir .= "/../lib"; # Pfad zu den Bibliotheken
my $oldDir = `/bin/pwd`;
chomp $oldDir;
if (chdir $dir)
{
my $absDir = `/bin/pwd`;
chop $absDir;
chdir $oldDir;
return (&splitFileDir("$absDir$file"));
}
else
{
print STDERR "<$dir> does not exist, exiting\n";
}
}
sub splitFileDir
{
my $name = shift;
return ('.', $name) unless ($name =~/\//); # nur einfacher Dateiname
my ($dir, $file) = $name =~ /^(.*)\/(.*)$/s;
$dir = '/' if ($dir eq ''); # gilt, falls z.B. /filename
return ($dir, $file);
}
my ($req, $prog) = &libPath($0);
push @INC, "$req";
require 'checkParam.pl';
require 'version.pl';
my $Help =<<EOH;
list files with create, access and modification times
usage:
$0 [-r] [-i] [-a|-m|-c] [files] [dirs]
--help -h show this help
--reverse -r sort in reverse order
--insensitive -i case-insensitively (not with -a, -m or -c)
--access -a sort according to access time
--modification -m sort according to access time
--creation -c sort according to access time
--unixTime -u show unix time (unsigned integer)
Sorting without [-a|-m|-c] is sorting according to file names.
-V print version(s)
Copyright (c) 2000,2004 by Heinz-Josef Claes
Published under the GNU General Public License
EOH
&printVersions(\@ARGV, '-V');
my $CheckPar =
CheckParam->new(
'-allowLists' => 'yes',
'-list' => [
Option->new('-option' => '-i',
'-alias' => '-insensitive',
'-only_if' =>
'not [-a] and not [-m] and not [-c]'
),
Option->new('-option' => '-a',
'-alias' => '--access',
'-only_if' =>
'not [-m] and not [-c]'
),
Option->new('-option' => '-m',
'-alias' => '--modification',
'-only_if' =>
'not [-a] and not [-c]'
),
Option->new('-option' => '-c',
'-alias' => '--creation',
'-only_if' =>
'not [-a] and not [-m]'
),
Option->new('-option' => '-r',
'-alias' => '--reverse'
),
Option->new('-option' => '-u',
'-alias' => '--unixTime'),
Option->new('-option' => '-h',
'-alias' => '--help'
),
]
);
$CheckPar->check('-argv' => \@ARGV,
'-help' => $Help
);
if ($CheckPar->getOptWithoutPar('-h'))
{
print "$Help";
exit 0;
}
my $sort = 'name'; # Default: Alphabetisch sortieren
$sort = 'atime' if ($CheckPar->getOptWithoutPar('-a'));
$sort = 'mtime' if ($CheckPar->getOptWithoutPar('-m'));
$sort = 'ctime' if ($CheckPar->getOptWithoutPar('-c'));
my $reverse = 1 if ($CheckPar->getOptWithoutPar('-r'));
my $insensitive = 1 if ($CheckPar->getOptWithoutPar('-i'));
my $unixTime = $CheckPar->getOptWithoutPar('-u');
my (@all) = ($CheckPar->getListPar() == 0) ? ('.') : ($CheckPar->getListPar());
my (@files);
my $f;
foreach $f (@all)
{
if (-d $f) # wenn Directory
{
opendir(DIR, $f) or print STDERR "cannot open <$f>\n";
my $f1;
foreach $f1 (readdir(DIR))
{
push @files, "$f/$f1";
}
closedir(DIR);
}
else # Datei
{
if (-f $f)
{
push @files, $f;
}
else
{
print "Cannot open file <$f>\n";
}
}
}
# Ermitteln der Zeiten
my (@f);
foreach $f (@files)
{
my ($atime, $mtime, $ctime) = (lstat($f))[8,9,10];
my %h = ('name' => $f,
'atime' => $atime,
'mtime' => $mtime,
'ctime' => $ctime
);
push @f, \%h;
}
# Sortieren
my (@sf);
if ($sort eq 'name')
{
if ($insensitive)
{
@sf = $reverse ? sort { uc($b->{$sort}) cmp uc($a->{$sort}) } @f
: sort { uc($a->{$sort}) cmp uc($b->{$sort}) } @f;
}
else
{
@sf = $reverse ? sort { $b->{$sort} cmp $a->{$sort} } @f
: sort { $a->{$sort} cmp $b->{$sort} } @f;
}
}
else
{
@sf = $reverse ?
sort { $b->{$sort} <=> $a->{$sort} } @f :
sort { $a->{$sort} <=> $b->{$sort} } @f;
}
# Ausgabe
my $len = $unixTime ? 11 : 19;
printf "%-${len}s %-${len}s %-${len}s [Time]\n", "access",
"modification", "creation";
foreach $f (@sf)
{
my $t;
foreach $t ($f->{'atime'}, $f->{'mtime'}, $f->{'ctime'})
{
if ($unixTime)
{
print "$t ",
}
else
{
print &getTime($t), " ";
}
}
print $f->{'name'}, "\n";
}
exit 0;
sub getTime
{
my $t = shift;
my ($sec,$min,$hour,$mday,$mon,$year) = localtime($t);
return sprintf("%4d.%02d.%02d %02d:%02d:%02d",
$year+1900, $mon+1, $mday,
$hour, $min, $sec);
}
|