auto/share/dist/AnyEvent-FTP/ppt/ls.pl untested

filter: Statementstmt 0.0% 0 / 268 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 44 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 439 SCAR 78.4
CC
50
Cov
0%
EntryFormat
342.0
List
240.0
Order
132.0
CRAP 2550.0
1 =begin metadata
2  
3 Name: ls
4 Description: list file/directory information
5 Author: Mark Leighton Fisher, fisherm@tce.com
6 License: perl
7  
8 =end metadata
9  
10 =cut
11  
12  
13 # Perl Power Tool - ls(1)
14  
15 use File::stat;
16 use Getopt::Std;
17 use File::Spec;
18  
19 # ------ partial inline of Stat::lsMode v0.50 code
20 # (see http://www.plover.com/~mjd/perl/lsMode/
21 # for the complete module)
22  
23 #
24 #
25 # Stat::lsMode
26 #
27 # Copyright 1998 M-J. Dominus
28 # (mjd-perl-lsmode@plover.com)
29 #
30 # You may distribute this module under the same terms as Perl itself.
31  
32 my @perms = qw(--- --x -w- -wx r-- r-x rw- rwx);
33 my @ftype = qw(. p c ? d ? b ? - ? l ? s ? ? ?);
34 $ftype[0] = '';
35  
36 sub format_mode {
37 my $mode = shift;
38 my %opts = @_;
39  
40 my $setids = ($mode & 07000)>>9;
41 my @permstrs = @perms[($mode&0700)>>6, ($mode&0070)>>3, $mode&0007];
42 my $ftype = $ftype[($mode & 0170000)>>12];
43  
44 if ($setids) {
45 if ($setids & 01) { # Sticky bit
46 $permstrs[2] =~ s/([-x])$/$1 eq 'x' ? 't' : 'T'/e;
47 }
48 if ($setids & 04) { # Setuid bit
49 $permstrs[0] =~ s/([-x])$/$1 eq 'x' ? 's' : 'S'/e;
50 }
51 if ($setids & 02) { # Setgid bit
52 $permstrs[1] =~ s/([-x])$/$1 eq 'x' ? 's' : 'S'/e;
53 }
54 }
55  
56 join '', $ftype, @permstrs;
57 }
58  
59 # ------ define variables
60 my $Arg = ""; # file/directory name argument from @ARGV
61 my $ArgCount = 0; # file/directory argument count
62 my $Attributes = ""; # File::stat from STDOUT (isatty() kludge)
63 my %Attributes = (); # File::stat directory entry attributes
64 my %DirEntries = (); # hash of dir entries and stat attributes
65 my $Getgrgid = ""; # getgrgid() for this platform
66 my $Getpwuid = ""; # getpwuid() for this platform
67 my @Dirs = (); # directories in ARGV
68 my @Files = (); # non-directories in ARGV
69 my $First = 1; # first directory entry on command line
70 my $Maxlen = 1; # longest string we've seen
71 my $Now = time; # time we were invoked
72 my %Options = (); # option/flag arguments
73 my $SixMonths = # long listing time if < 6 months, else year
74 60*60*24*(365/2);
75 my $VERSION = '0.70'; # because we're V7-compatible :)
76 my $WinSize = "\0" x 8; # window size buffer
77 my $TIOCGWINSZ = # get window size via ioctl()
78 0x40087468; # should be require sys/ioctl.pl,
79 # but that won't exist on all platforms
80 my $WinCols = 0; # window columns of output
81 my $WinRows = 0; # window rows of output
82 my $Xpixel = 0; # window start X
83 my $Ypixel = 0; # window start Y
84  
85 # ------ compensate for lack of getpwuid/getgrgid on some platforms
86 eval { my $dummy = ""; $dummy = (getpwuid(0))[0] };
87 if ($@) {
88 $Getpwuid = sub { return ($_[0], 0); };
89 $Getgrgid = sub { return ($_[0], 0); };
90 } else {
91 $Getpwuid = sub { return getpwuid($_[0]); };
92 $Getgrgid = sub { return getgrgid($_[0]); };
93 }
94  
95 # ------ functions
96  
97 # ------ get directory entries
98 sub DirEntries {
99 my $Options = shift; # option arguments hashref
100 local *DH; # directory handle
101 my %Attributes = (); # entry/attributes hash
102 my @Entries = (); # entries in original order
103 my $Name = ""; # entry name
104  
105 if (!opendir(DH, $_[0]) || exists($Options{'d'})) {
106 if (-e $_[0]) {
107 closedir(DH) if (defined(DH));
108 push(@Entries, $_[0]);
109 $Attributes{$_[0]} = stat($_[0]);
110 push(@Entries, \%Attributes);
111 return @Entries;
112 }
113 print "pls: can't access '$_[0]': $!\n";
114 return ();
115 }
116 while ($Name = readdir(DH)) {
117 next if (!exists($Options->{'a'}) &&
118 $Name =~ m/^\./o);
119 push(@Entries, $Name);
120 $Attributes{$Name} = stat( File::Spec->catfile( $_[0], $Name ) );
121 }
122 closedir(DH);
123  
124 # ------ return list with %Attributes ref at end
125 push(@Entries, \%Attributes);
126 return @Entries;
127 }
128  
129 # ------ format directory entry
130 sub EntryFormat {
131 my $Options = shift; # ls option arguments
132 my $Attributes = shift; # entry attributes hashref
133 my $Entry = shift; # directory entry name
134 my $Blocks = 0; # block size when otherwise unknown
135 my $BlockSize = # block size in 512-byte units
136 exists($Options->{'k'}) ? 2 : 1;
137 my $DateStr = ""; # time/date string
138 my $Gid = -1; # group ID number
139 my $Mode = ""; # file mode
140 my @Month = ( # file time month abbrev.
141 "Jan",
142 "Feb",
143 "Mar",
144 "Apr",
145 "May",
146 "Jun",
147 "Jul",
148 "Aug",
149 "Sep",
150 "Oct",
151 "Nov",
152 "Dec"
153 );
154 my $Time = 0; # file time
155 my $Uid = -1; # user ID number
156  
157 # ------ for localtime()
158 my $sec = 0;
159 my $min = 0;
160 my $hour = 0;
161 my $mday = 0;
162 my $mon = 0;
163 my $year = 0;
164 my $wday = 0;
165 my $yday = 0;
166 my $isdst = 0;
167  
168 if (exists($Options->{'i'})) {
169 if (defined($Attributes->{$Entry})) {
170 # 1 2 3 4 5 6 7
171 #23456789*123456789*123456789*123456789*123456789*123456789*123456789*
172 printf("%10d ", $Attributes->{$Entry}->ino);
173 } else {
174 print "_________ ";
175 }
176 }
177 if (exists($Options->{'s'})) {
178 if (defined($Attributes->{$Entry})) {
179 $Blocks = $Attributes->{$Entry}->blocks;
180 if ($Blocks eq '') {
181 $Blocks = 0;
182 }
183 printf("%4d ",
184 $Blocks / $BlockSize +
185 (($Blocks % $BlockSize)
186 > 0));
187 } else {
188 print "____ ";
189 }
190 }
191 if (!exists($Options->{'l'})) {
192 print "$Entry\n";
193 } else {
194 if (!defined($Attributes->{$Entry})) {
195 print <<UNDEFSTAT;
196 __________ ___ ________ ________ ________ ___ __ _____
197 UNDEFSTAT
198 } else {
199 $Mode =
200 format_mode($Attributes->{$Entry}->mode);
201 print "$Mode ";
202 #printf("%8o ",
203 # $Attributes->{$Entry}->mode);
204 printf("%3d ",
205 $Attributes->{$Entry}->nlink);
206 if (exists($Options->{'n'})) {
207 printf("%-8d ",
208 $Attributes->{$Entry}->uid);
209 } else {
210 $Uid =
211 &$Getpwuid($Attributes->{$Entry}->uid);
212 if (defined($Uid)) {
213 printf("%-8s ", $Uid);
214 } else {
215 printf("%-8d ",
216 $Attributes->{$Entry}->uid);
217 }
218 }
219 if (exists($Options->{'n'})) {
220 printf("%-8d ",
221 $Attributes->{$Entry}->gid);
222 } else {
223 $Gid =
224 &$Getgrgid($Attributes->{$Entry}->gid);
225 if (defined($Gid)) {
226 printf("%-8s ", $Gid);
227 } else {
228 printf("%-8d ",
229 $Attributes->{$Entry}->gid);
230 }
231 }
232 if ($Attributes->{$Entry}->mode & 0140000) {
233 printf("%9d ",
234 $Attributes->{$Entry}->size);
235 } else {
236 printf("%4x,%4x ",
237 (($Attributes->{$Entry}->dev
238 & 0xFFFF000) > 16),
239 $Attributes->{$Entry}->dev
240 & 0xFFFF);
241 }
242 $Time = $Attributes->{$Entry}->mtime;
243 if (exists($Options->{'c'})) {
244 $Time = $Attributes->{$Entry}->ctime;
245 }
246 if (exists($Options->{'u'})) {
247 $Time = $Attributes->{$Entry}->atime;
248 }
249 ($sec,$min,$hour,$mday,$mon,$year,
250 $wday,$yday,$isdst) =
251 localtime($Time);
252 print $Month[$mon];
253 if ($mday < 10) {
254 print " $mday ";
255 } else {
256 print " $mday ";
257 }
258 if ($Now - $Time <= $SixMonths) {
259 printf("%02d:%02d", $hour, $min);
260 } else {
261 printf(" %04d", $year + 1900);
262 }
263 }
264 print " $Entry\n";
265 }
266 }
267  
268 # ------ list directory entries, breadth-first
269 sub List {
270 my $Name = shift; # directory name
271 my $Options = shift; # options/flags hashref
272 my $Expand = shift; # do 1 level of dir expansion,
273 # for "ls DIRNAME"
274 my $Attributes = ""; # entry attributes hashref
275 my $BlockSize = # block size in 512-byte units
276 exists($Options->{'k'}) ? 2 : 1;
277 my $Cols = 0; # output columns for this List()
278 my $Entry = ""; # directory entry
279 my @Dirs = (); # directories from -R and DirEntries
280 my $Mask = ""; # sprintf() format/mask
281 my $Mylen = 0; # current entry length
282 my $Path = ""; # path for subdirectories
283 my $Piece = ""; # piece of entry list
284 my @SortedEntries = (); # sorted entry list
285 my $Rows = 0; # output rows for this List()
286 my $Target = 0; # target element index
287 my $TotalBlocks = 0; # total directory size in blocks
288 my $elt = 0; # element index
289  
290 # ------ get directory entries attributes
291 $Attributes = pop(@_);
292  
293 # ------ precompute max entry length and total size
294 foreach (@_) {
295 $TotalBlocks +=
296 (!defined($Attributes->{$_}) ||
297 ($Attributes->{$_}->blocks eq '')) ?
298 0: $Attributes->{$_}->blocks;
299 $Mylen = length($_);
300 if ($Mylen > $Maxlen) {
301 $Maxlen = $Mylen;
302 }
303 }
304 $Maxlen += 1; # account for spaces
305  
306 # ------ print directory name if -R
307 if (exists($Options->{'R'})) {
308 print "$Name:\n";
309 }
310  
311 # ----- print total in blocks if -s or -l
312 if (exists($Options->{'l'}) || exists($Options->{'s'})) {
313 print "total $TotalBlocks\n";
314 }
315  
316 # ------ sort entry list
317 @SortedEntries = Order(\%Options, $Attributes, @_);
318  
319 # ------ user requested 1 entry/line, long, size, or inode
320 if (defined($Options->{'1'}) ||
321 exists($Options->{'l'}) ||
322 exists($Options->{'s'}) ||
323 exists($Options->{'i'})) {
324 foreach $Entry (@SortedEntries) {
325 EntryFormat(\%Options,
326 $Attributes, $Entry);
327 }
328  
329 # ------ multi-column output
330 } else {
331  
332 # ------ compute rows, columns, width mask
333 $Cols = int($WinCols / $Maxlen) || 1;
334 $Rows = int(($#_+$Cols) / $Cols);
335 $Mask = sprintf("%%-%ds ", $Maxlen);
336  
337 for ($elt = 0; $elt < $Rows * $Cols; $elt++) {
338 $Target = ($elt % $Cols) * $Rows +
339 int(($elt / $Cols));
340 $Piece = sprintf($Mask,
341 $Target < ($#SortedEntries + 1) ?
342 $SortedEntries[$Target] : "");
343 # don't blank pad to eol of line
344 $Piece =~ s/\s+$//
345 if (($elt+1) % $Cols == 0);
346 print $Piece;
347 print "\n" if (($elt+1) % $Cols == 0);
348 }
349 print "\n" if (($elt+1) % $Cols == 0);
350 }
351  
352 # ------ print blank line if -R
353 if (exists($Options->{'R'})) {
354 print "\n";
355 }
356  
357 # ------ list subdirectories of this directory
358 if (!exists($Options{'d'}) &&
359 ($Expand || exists($Options->{'R'}))) {
360 foreach $Entry (Order(\%Options, $Attributes, @_)) {
361 next if ($Entry eq "." || $Entry eq "..");
362 if (defined($Attributes->{$Entry}) &&
363 $Attributes->{$Entry}->mode & 0040000) {
364 $Path = File::Spec->canonpath(File::Spec->catdir($Name,$Entry));
365 @Dirs = DirEntries(\%Options, $Path);
366 List($Path, \%Options, 0, @Dirs);
367 }
368 }
369 }
370 }
371  
372 # ------ sort file list based on %Options
373 sub Order {
374 my $Options = shift; # parsed option/flag arguments
375 my $Attributes = shift; # File::stat attributes hashref
376 my @Entries = @_; # directory entry names
377  
378 # ------ sort by size, largest first
379 if (exists($Options->{'S'})) {
380 if (exists($Options->{'r'})) {
381 @Entries = sort
382 { $Attributes->{$a}->size <=>
383 $Attributes->{$b}->size }
384 @Entries;
385 } else {
386 @Entries = sort
387 { $Attributes->{$b}->size <=>
388 $Attributes->{$a}->size }
389 @Entries;
390 }
391  
392 # ------ sort by time, most recent first
393 } elsif (exists($Options->{'t'}) ||
394 exists($Options->{'c'}) ||
395 exists($Options->{'u'})) {
396 if (exists($Options->{'r'})) {
397 if (exists($Options->{'u'})) {
398 @Entries = sort
399 { $Attributes->{$a}->atime <=>
400 $Attributes->{$b}->atime }
401 @Entries;
402 } elsif (exists($Options->{'c'})) {
403 @Entries = sort
404 { $Attributes->{$a}->ctime <=>
405 $Attributes->{$b}->ctime }
406 @Entries;
407 } else {
408 @Entries = sort
409 { $Attributes->{$a}->mtime <=>
410 $Attributes->{$b}->mtime }
411 @Entries;
412 }
413 } else {
414 if (exists($Options->{'u'})) {
415 @Entries = sort
416 { $Attributes->{$b}->atime <=>
417 $Attributes->{$a}->atime }
418 @Entries;
419 } elsif (exists($Options->{'c'})) {
420 @Entries = sort
421 { $Attributes->{$b}->ctime <=>
422 $Attributes->{$a}->ctime }
423 @Entries;
424 } else {
425 @Entries = sort
426 { $Attributes->{$b}->mtime <=>
427 $Attributes->{$a}->mtime }
428 @Entries;
429 }
430 }
431  
432 # ------ sort by name
433 } elsif (!exists($Options->{'f'})) {
434 if (exists($Options->{'r'})) {
435 @Entries = sort { $b cmp $a } @Entries;
436 } else {
437 @Entries = sort { $a cmp $b } @Entries;
438 }
439 }
440  
441 # ------ return list sorted by options (or unsorted if -f)
442 return @Entries;
443 }
444  
445 # ------ process arguments
446 getopts('1ACFLRSTWacdfgiklmnopqrstux', \%Options);
447  
448 # ------ get (or guess) window size
449 if (ioctl(STDOUT, $TIOCGWINSZ, $WinSize)) {
450 ($WinRows, $WinCols, $Xpixel, $Ypixel) = unpack('S4', $WinSize);
451 } else {
452 $WinCols = 80;
453 }
454 $Attributes = stat(STDOUT);
455 if ($Attributes->mode & 0140000) {
456 $Options{'1'} = '1';
457 }
458  
459 # ------ current directory if no arguments
460 if ($#ARGV < 0) {
461 List('.', \%Options, 0, DirEntries(\%Options, "."));
462  
463 # ------ named files/directories if arguments
464 } else {
465 $ArgCount = -1;
466 foreach $Arg (@ARGV) {
467 if (!exists($Options{'d'}) && -d $Arg) {
468 $ArgCount++;
469 push(@Dirs, $Arg);
470 } else {
471 $ArgCount += 2;
472 push(@Files, $Arg);
473 }
474 }
475 foreach $Arg (@Files) {
476 $Attributes{$Arg} = stat($Arg);
477 }
478 foreach $Arg (Order(\%Options, \%Attributes, @Files)) {
479 $First = 0;
480 List($Arg, \%Options, 0,
481 DirEntries(\%Options, $Arg));
482 }
483 foreach $Arg (@Dirs) {
484 $Attributes{$Arg} = stat($Arg);
485 }
486 foreach $Arg (Order(\%Options, \%Attributes, @Dirs)) {
487 if (!exists($Options{'R'})) {
488 print "\n" if (!$First);
489 $First = 0;
490 print "$Arg:\n" if ($ArgCount > 0);
491 }
492 List($Arg, \%Options, 0,
493 DirEntries(\%Options, $Arg));
494 }
495 }
496  
497 __END__