Debian Codegraph @

bin/ff

raw
1 #!/usr/bin/perl -w
2 #
3 # l / ll / ff / ffl - better replacement of the classic ls command
4 #
5 # Author: Ulli Horlacher <[email protected]>
6 #
7 # Perl Artistic License
8
9 # l is a better replacement for the classic ls program.
10 #
11 # l lists the content of a directory only if you add a trailing / to its name.
12 #
13 # l -A flags POSIX file capabilities with a '*' instead of a '-' in the first
14 # output column. To show these capabilities, use getcap or getfacl.
15 #
16 # If the last argument is "-" then l reads more arguments from STDIN.
17 #
18 # If called "ll", then l acts like "l -l"
19 # If called "lll", then l acts like "l -liaS"
20 # If called "ff", then l acts like "l -F"
21 # If called "ffl", then l acts like "l -Fl"
22
23 # GNU alternative:
24 # alias ll='ls -l --color=never --quoting-style=shell --time-style=+"%Y-%m-%d %H:%M:%S"'
25
26 # History:
27 #
28 # 199?-??-?? initial version
29 # 2016-12-12 lots of changes and bug fixes
30 # 2016-12-12 added file capabilities detection
31 # 2016-12-15 added option -V : show version (-v: and history)
32 # 2018-01-07 lf renamed to ff (find files)
33 # 2018-01-07 added find hard links with: ff directory/file
34 # 2019-02-20 add leading space to filename if not in ''
35 # 2019-03-18 implict -P when STDOUT is no tty
36 # 2019-07-26 fixed bug ff ignores option -l
37 # 2019-07-27 more squeeze
38
39 use Cwd qw'abs_path';
40 use File::Basename;
41 use Getopt::Std;
42
43 # the name of the game
44 $prg = $0;
45 $prg =~ s:.*/::;
46
47 $ENV{LC_ALL} = 'C';
48
49 $version = 0;
50
51 unless ($version) {
52 my @d = localtime((stat $0)[9]);
53 $version = sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3])
54 }
55
56 # read files arguments from STDIN
57 if (@ARGV and $ARGV[-1] eq '-') {
58 pop @ARGV;
59 push @ARGV,grep { chomp } <STDIN>;
60 }
61
62 # parse CLI arguments
63 $opt_l = $opt_i = $opt_t = $opt_s = $opt_a = $opt_r = $opt_d = $opt_n = 0;
64 $opt_L = $opt_N = $opt_c = $opt_u = $opt_S = $opt_R = $opt_z = $opt_h = 0;
65 $opt_U = $opt_A = $opt_x = $opt_E = $opt_P = $opt_H = $opt_V = $opt_v = 0;
66 $opt_m = $opt_f = $opt_F = $opt_D = '';
67 getopts('-hHvVdnlLNitcuarsxAUSREPzm:f:D:F:') or usage(1);
68 usage(0) if $opt_h;
69 $opt_P = 1 unless -t 1;
70 $opt_z = 1 unless $opt_R;
71 $opt_l = 1 if $prg eq 'll';
72 $opt_l = $opt_i = $opt_S = 1 if $prg eq 'lll';
73
74 if ($opt_H) {
75 open $0,$0 or die "l: $0 - $!\n";
76 while (<$0>) {
77 $help = $_ if /^# l is/;
78 if ($help) {
79 last if /^\s*$/ or /^#\s*\d\d\d\d-\d\d-\d\d/;
80 print;
81 }
82 }
83 exit;
84 }
85
86
87 if ($opt_E) {
88 &examples;
89 exit;
90 }
91
92 if ($opt_V) {
93 warn "$prg: version $version\n";
94 if ($opt_v) {
95 open $0,$0 or die "$prg: $0 - $!\n";
96 while (<$0>) {
97 print if s/^#\s*(\d\d..-)/$1/;
98 }
99 }
100 exit;
101 }
102
103 if ($prg eq 'ff' or $prg eq 'ffl') {
104 $opt_l ||= $prg eq 'ffl';
105 if (scalar(@ARGV) == 0) {
106 die usage(1);
107 } elsif (scalar(@ARGV) == 1) {
108 $opt_F = shift @ARGV;
109 if ($opt_F eq '.') {
110 $opt_R = $opt_F;
111 } elsif (-f $opt_F and not -l $opt_F and $opt_F =~ /\//) {
112 # find hard links
113 my @s = stat $opt_F or die "$prg: $opt_F - $!\n";
114 if ($s[3] == 1) {
115 @ARGV = ($opt_F);
116 } else {
117 @ARGV = ();
118 my $inode = $s[1];
119 my $q = shellquote($opt_F);
120 my $mp = `stat -c %m -- $q`;
121 chomp $mp;
122 unless (length $mp) {
123 die "$prg: cannot find filesystem root for $opt_F\n";
124 }
125 $mp = shellquote($mp);
126 my $find = "find $mp -xdev -inum $inode";
127 open $find,"$find 2>/dev/null|" or die "$prg: cannot run $find - $!\n";
128 while (<$find>) {
129 chomp;
130 push @ARGV,$_;
131 }
132 }
133 $opt_F = '';
134 }
135 } elsif (-d $ARGV[-1]) {
136 $opt_R = pop(@ARGV);
137 $opt_F = join('|',@ARGV);
138 } else {
139 $opt_F = join('|',@ARGV);
140 }
141 if ($opt_F) {
142 if (scalar(@ARGV > 1) and $opt_F =~ m:/:) {
143 usage(1);
144 }
145 @ARGV = ();
146 if (-d $opt_R) {
147 @ARGV = ($opt_R);
148 $rsnapshot = (abs_path($opt_R)."/$opt_F") =~ /\.snapshot/;
149 }
150 }
151 }
152
153 $postsort = $opt_t||$opt_s;
154 $postproc = $postsort||$opt_z;
155
156 # mark for squeeze operation
157 $z = $opt_z ? "\0" : '';
158
159 # default sorting methode
160 if ($opt_U) { $lcsort = sub { return @_ } }
161 elsif ($opt_r) { $lcsort = sub { sort { lc $b cmp lc $a } @_ } }
162 else { $lcsort = sub { sort { lc $a cmp lc $b } @_ } }
163
164 if ($opt_m) {
165 if ($opt_a) {
166 $opt_a = 0;
167 warn "$prg: ignoring option -a\n";
168 }
169 $opt_m =~ s/^^?/^/;
170 $opt_m =~ s/\$?$/\$/;
171 } else {
172 # default: list only files not beginning with a dot
173 if ($opt_a) { $opt_m = '.' }
174 else { $opt_m = '^[^\.]' }
175 }
176
177 $older = $newer = 0;
178
179 if ($opt_D) {
180 if ($opt_D =~ /:(\d+)([mhd]?)/) {
181 $older = $1;
182 my $z = $2 || 'd';
183 if ($z =~ /m/) { $older *= 60 }
184 elsif ($z =~ /h/) { $older *= 60*60 }
185 elsif ($z =~ /d/) { $older *= 60*60*24 }
186 } elsif ($opt_D =~ /:(\d\d\d\d-\d\d-\d\d)$/) {
187 $older = $1;
188 }
189 if ($opt_D =~ /(\d+)([mhd]?)(:|$)/) {
190 $newer = $1;
191 my $z = $2 || 'd';
192 if ($z =~ /m/) { $newer *= 60 }
193 elsif ($z =~ /h/) { $newer *= 60*60 }
194 elsif ($z =~ /d/) { $newer *= 60*60*24 }
195 } elsif ($opt_D =~ /^(\d\d\d\d-\d\d-\d\d)(:|$)/) {
196 $newer = $1;
197 }
198 }
199
200 # preselect date field number
201 if ($opt_c) { $sdf = 'c' }
202 elsif ($opt_u) { $sdf = 'a' }
203 else { $sdf = 'm' }
204
205 # any arguments?
206 if (@ARGV) {
207 getaclc(@ARGV) if $opt_A;
208 collect(&$lcsort(@ARGV));
209 } else {
210 # getfiles() has getaclc()
211 collect(getfiles('.'));
212 }
213
214 # post process files list?
215 # remark: if no postprocessing, files list has been already printed in list()
216 if (@LIST && $postproc) {
217
218 # on -t or -s option sort list on date or size
219 # and then strip of leading sorting pre-string
220 @LIST = grep { s/.{21}// } reverse sort @LIST if $postsort;
221
222 if ($opt_z and not $opt_f) {
223 if ($opt_l) {
224 # squeeze user and group fields (= remove unnecessary spaces)
225 my $ul = 1;
226 my $gl = 1;
227 foreach (@LIST) {
228 my @L = split;
229 my $s1 = length($L[1]);
230 my $s2 = length($L[2]);
231 $ul = $s1 if $s1 > $ul;
232 $gl = $s2 if $s2 > $gl;
233 }
234 map { s/(\s+)(\S+)\s+(\S+)\s+/sprintf("%s%-${ul}s %-${gl}s ",$1,$2,$3)/e } @LIST;
235 }
236 # squeeze size field (= remove unnecessary spaces)
237 map { s/\0 *([,\d]+|-) /sprintf("%${opt_z}s ",$1)/e } @LIST;
238 my $s = 1;
239 # squeeze double spaces
240 foreach (@LIST) { $s = 0 unless /^\S+ / }
241 map { s/^(\S+ ) /$1/ } @LIST if $s;
242 $s = 1;
243 foreach (@LIST) { $s = 0 unless /\d\d:\d\d ./ }
244 map { s/(\d\d:\d\d ) /$1/ } @LIST if $s;
245 }
246
247 @LIST = reverse @LIST if $opt_r;
248
249 if (not ($opt_t or $opt_U) and grep /^d[rR-][wW-][xX-]/,@LIST) {
250 foreach (@LIST) { print if /^d/ }
251 foreach (@LIST) { print unless /^d/ }
252 } else {
253 print @LIST;
254 }
255 }
256
257 # print statistics summary?
258 if ($opt_S && $SS) {
259 print "$SS file(s):";
260 printf " r=%d (%s Bytes)",$SS{'-'},&d3($Ss) if $SS{'-'};
261 delete $SS{'-'};
262 foreach my $type (qw(l d c b p s ?)) {
263 printf " %s=%d",$type,$SS{$type} if $SS{$type};
264 delete $SS{$type};
265 }
266 foreach my $type (keys %SS) { printf " %s=%d",$type,$SS{$type} }
267 print "\n";
268 }
269
270 exit ($found ? 0 : 1);
271
272
273 # collect files and build (or show) file lists
274 #
275 # INPUT: filenames
276 sub collect {
277 my @files = @_;
278 my $f;
279
280 # loop over all argument files/directories
281 foreach $f (@files) {
282
283 # recursive?
284 if ($opt_R) {
285
286 if ($opt_L) {
287 unless (-e $f) {
288 warn "$prg: dangling symlink $f\n";
289 next;
290 }
291 $f = abs_path($f);
292 }
293 list($f);
294
295 # traverse real subdirs
296 if (-d $f and not -l $f) {
297 $f =~ s:/*$::;
298 $f = '/' if $f eq '';
299 # skip other file systems on -x
300 if ($opt_x) {
301 my @pd = stat(dirname($f)) or die "$prg: cannot stat $f - $!\n";
302 my @sd = stat($f) or die "$prg: cannot stat $f - $!\n";
303 next if $pd[0] ne $sd[0];
304 }
305 collect(getfiles("$f/"));
306 }
307
308 } else {
309
310 # suppress trailing / on -d option
311 $f =~ s:/$:: if $opt_d;
312
313 # on trailing / list subdirs, too
314 if ($f =~ m:/$:) { list(getfiles($f)) }
315 elsif ($f eq '') { list('/') }
316 else {
317 if ($opt_L) {
318 unless (-e $f) {
319 warn "$prg: dangling symlink $f\n";
320 next;
321 }
322 $f = abs_path($f);
323 }
324 list($f);
325 }
326
327 }
328 }
329 }
330
331
332 # list file(s) or collect them for postprocessing
333 #
334 # INPUT: filenames
335 #
336 # GLOBAL: @LIST (filenames-list)
337 sub list {
338 my @files = @_;
339 my ($file,$line,$linkname,$inode,$links,$size,$mode,$uid,$gid,$date,%dates);
340 my ($day);
341
342 $modemod = sub {
343 my $file = shift;
344 # local $_ = uc(substr($mode,4,3));
345 # s/-/*/g;
346 # substr($mode,4,3) = $_;
347 if ($opt_l) {
348 if ($ACL{$file}) { $mode .= '+' }
349 else { $mode .= ' ' }
350 }
351 $mode =~ s/^-/*/ if $CAP{$file};
352 };
353
354 foreach $file (@files) {
355
356 next if $opt_F and not fmatch($file);
357 next if $opt_N and (not -f $file or -l $file);
358
359 # get file information
360 # if ($opt_L and stat $file or not $opt_L and lstat $file) {
361 if (lstat $file) {
362 ($linkname,$inode,$links,$size,$mode,$uid,$gid,$date,%dates)
363 = &info($file);
364 } elsif ($! eq "Permission denied") {
365 $linkname = $file;
366 $inode = $links = $size = $uid = $gid = '?';
367 $mode = $opt_l ? '?---------' : '?---';
368 $date = '????-??-?? ??:??:??';
369 %dates = ('m' => 0, 'a' => 0, 'c' => 0);
370 } else {
371 warn "$prg: ".quote($file)." - $!\n";
372 next;
373 }
374
375 $day = $date;
376 $day =~ s/\s.*//;
377
378 if ($older) {
379 next if $older =~ /-/ and $day gt $older;
380 next if $older !~ /-/ and $dates{m} > time-$older;
381 }
382 if ($newer) {
383 next if $newer =~ /-/ and $day lt $newer;
384 next if $newer !~ /-/ and $dates{m} < time-$newer;
385 }
386
387 if (defined $linkname) {
388
389 # prepend sorting string
390 $line = '';
391 $line = sprintf '%21s',$date if $opt_t;
392 $line = sprintf '%21s',$size if $opt_s;
393
394 unless ($opt_n) {
395 $uid = substr($uid,0,8);
396 $gid = substr($gid,0,8);
397 }
398
399 # user defined format?
400 if ($opt_f) {
401 foreach my $i (split '',$opt_f) {
402 if ($opt_n) {
403 $i =~ tr/AD/ad/;
404 if ($i eq 'm') { $line .= sprintf '%06o ', $mode }
405 elsif ($i eq 'u') { $line .= sprintf '%6d ', $uid }
406 elsif ($i eq 'g') { $line .= sprintf '%6d ', $gid }
407 elsif ($i eq 's') { $line .= sprintf "$z%16s ",$size }
408 elsif ($i eq 'l') { $line .= sprintf '%3s ', $links }
409 elsif ($i eq 'i') { $line .= sprintf '%14s ', $inode }
410 elsif ($i eq 'd') { $line .= sprintf '%10s ', $date }
411 elsif ($i eq 'a') { $line .= sprintf '%10s %10s %10s ',
412 $dates{'a'},$dates{'m'},$dates{'c'} }
413 } else {
414 &$modemod($file);
415 if ($i eq 'm') { $line .= $mode.' ' }
416 elsif ($i eq 'u') { $line .= sprintf '%-8s ', $uid }
417 elsif ($i eq 'g') { $line .= sprintf '%-8s ', $gid }
418 elsif ($i eq 's') { $line .= sprintf "$z%19s ",$size }
419 elsif ($i eq 'l') { $line .= sprintf '%3s ', $links }
420 elsif ($i eq 'i') { $line .= sprintf '%14s ', $inode }
421 elsif ($i eq 'd') { $line .= $date.' ' }
422 elsif ($i eq 'D') { $line .= $date.' ' }
423 elsif ($i eq 'a') { $line .= &isodate($dates{'a'}).' '.
424 &isodate($dates{'m'}).' '.
425 &isodate($dates{'c'}).' ' }
426 elsif ($i eq 'A') { $line .= &isodate($dates{'a'}).' '.
427 &isodate($dates{'m'}).' '.
428 &isodate($dates{'c'}).' ' }
429 }
430 }
431
432 # predefined formats
433 } else {
434
435 if ($opt_n) {
436 if ($opt_l) {
437 $line .= sprintf "%06o %6d %6d $z%15s %10d ",
438 $mode,$uid,$gid,$size,$date;
439 } else {
440 $line .= sprintf "%06o $z%15s %10d ",$mode,$size,$date;
441 }
442 } else {
443 &$modemod($file);
444 if ($opt_l) {
445 $line .= sprintf "%s %-8s %-8s $z%19s %s ",
446 $mode,$uid,$gid,$size,$date;
447 } else {
448 $line .= sprintf "%s $z%19s %s ",$mode,$size,substr($date,0,-3);
449 }
450 }
451
452 if ($opt_i) { $line .= sprintf '%3s %10s ',$links,$inode }
453 }
454
455 if ($linkname =~ /^\'/ or $opt_P) {
456 $line .= $linkname."\n";
457 } else {
458 $line .= ' '.$linkname."\n";
459 }
460
461 if ($postproc) {
462 push @LIST,$line;
463 } else {
464 $line =~ s/\0//;
465 print $line;
466 }
467 $found++;
468
469 } else {
470 lstat $file;
471 warn "$prg: cannot get dir-info for ".quote($file)." - $!\n";
472 }
473
474 }
475 }
476
477 # get file information
478 #
479 # INPUT: file name
480 #
481 # OUTPUT: filename with linkname, inode, hard link count, size, mode string,
482 # UID, GID, isodate
483 sub info {
484 my $file = shift;
485 my ($linkname,$links,$mode,$bmode,$uid,$gid,$date,%dates,@stat);
486 my $size = '-';
487 my $inode = '?';
488 my @rwx = qw/--- --x -w- -wx r-- r-x rw- rwx/;
489 my $type;
490
491 if ($opt_L) { @stat = stat $file }
492 else { @stat = lstat $file }
493
494 if (@stat) {
495
496 $inode = $stat[1];
497 $bmode = $stat[2];
498 $links = $stat[3];
499 %dates = ('m' => $stat[9],
500 'a' => $stat[8],
501 'c' => $stat[10]);
502
503 if ($opt_n) {
504 $uid = $stat[4];
505 $gid = $stat[5];
506 $date = $dates{$sdf};
507 } else {
508 $uid = getpwuid($stat[4]) || $stat[4];
509 $gid = getgrgid($stat[5]) || $stat[5];
510 $date = &isodate($dates{$sdf});
511 }
512
513 if (-f _) { $type = '-'; $size = $stat[7]; }
514 elsif (!$opt_L && -l _) { $type = 'l'; }
515 elsif (-d _) { $type = 'd'; }
516 elsif (-c _) { $type = 'c'; $size = &nodes($stat[6]); }
517 elsif (-b _) { $type = 'b'; $size = &nodes($stat[6]); }
518 elsif (-p _) { $type = 'p'; }
519 elsif (-S _) { $type = 's'; }
520 else { $type = '?'; }
521
522 if ($opt_n) {
523 $mode = $stat[2];
524 $size = $stat[7] if $size eq '-';
525 } else {
526 if ($opt_l) {
527 $mode = $rwx[$bmode & 7];
528 $bmode >>= 3;
529 $mode = $rwx[$bmode & 7] . $mode;
530 $bmode >>= 3;
531 $mode = $rwx[$bmode & 7] . $mode;
532 substr($mode,2,1) =~ tr/-x/Ss/ if -u _;
533 substr($mode,5,1) =~ tr/-x/Ss/ if -g _;
534 substr($mode,8,1) =~ tr/-x/Tt/ if -k _;
535 $mode = $type.$mode;
536 } else {
537 # with short list display only effective file access modes
538 # respect ACLs ==> cannot use pseudofile _
539 # filetest.pm not available on SLES
540 eval "use filetest 'access'";
541 $mode = $type
542 . (-r $file ? 'R' : '-')
543 . (-w $file ? 'W' : '-')
544 . (-x $file ? 'X' : '-');
545 substr($mode,2,1) =~ tr/-x/Ss/ if -u $file or -g $file;
546 substr($mode,3,1) =~ tr/-x/Tt/ if -k $file;
547 }
548 }
549
550 # fall back to ls command if perl lstat failed
551 } else {
552 if ($opt_L) {
553 return;
554 } else {
555 ($mode,$links,$uid,$gid,$size) = split /\s+/,`ls -ld $file 2>/dev/null`;
556 return undef unless defined $mode;
557 $type = substr($mode,0,1);
558 # for (my $i=0;$i<3;$i++) { push @dates,'????-??-?? ??:??:??' }
559 # $date = `gfind $dir -maxdepth 1 -name $file -printf '%Ty-%Tm-%Td %TT\n'`;
560 }
561 }
562
563 # summarize statistics
564 if ($opt_S) {
565 $SS++;
566 $SS{$type}++;
567 $Ss += $size if $type eq '-';
568 }
569
570 $size = &d3($size);
571
572 # determine longest size field
573 if ($opt_z) {
574 my $x = length $size;
575 $opt_z = $x if $x>$opt_z;
576 }
577 $linkname = $opt_P ? $file : quote($file) ;
578 if ($type eq 'l' and $opt_f !~ /n/) {
579 my $link = readlink($file);
580 if (defined $link) {
581 $linkname .= ' -> ' . ($opt_P ? $link : quote($link));
582 }
583 }
584 $mode =~ s/\+$//;
585 # $mode .= ' ' unless $mode =~ /\+$/;
586
587 return ($linkname,$inode,$links,$size,$mode,$uid,$gid,$date,%dates);
588 }
589
590
591 # get ACLs and POSIX capabilities
592 #
593 # INPUT: filenames
594 #
595 # GLOBAL: @ACL @CAP
596 sub getaclc {
597 my @files;
598
599 return if $opt_n;
600 $getfacl ||= pathsearch('getfacl');
601 $getcap ||= pathsearch('getcap');
602 foreach my $file (@_) { push @files,$file if exist($file) }
603 return unless @files;
604 @files = map { quotemeta } @files;
605 if ($getfacl and $opt_l) {
606 if (open my $acl,"$getfacl -Pps @files 2>/dev/null|") {
607 # warn "$getfacl,'-Pps',@files";
608 while (<$acl>) { $ACL{$1} = $1 if /^# file: (.+)/ }
609 close $acl;
610 }
611 }
612 if ($getcap) {
613 if (open my $cap,"$getcap @files 2>/dev/null|") {
614 while (<$cap>) { $CAP{$1} = $1 if /^(.*?) =../ }
615 close $cap;
616 }
617 }
618 }
619
620
621 # reformat integer into 3-digit doted format
622 # (when non-numerical mode is set)
623 #
624 # INPUT: integer or '-'
625 #
626 # OUTPUT: d3-string
627 sub d3 {
628 local $_ = shift;
629 if ($opt_n) { s/-/0/ }
630 else { while (s/(\d)(\d\d\d\b)/$1,$2/) {} }
631 return $_;
632 }
633
634
635 # get all files matching pattern $opt_m
636 #
637 # INPUT: directory to scan with trailing /
638 #
639 # OUTPUT: files which match (sorted, directories first)
640 sub getfiles {
641 my $dir = shift;
642 my @files = ();
643 my @dirs = ();
644 my $f;
645 local *D;
646
647 $dir =~ s:/*$:/:;
648
649 if (opendir D,$dir) {
650 $dir = '' if $dir eq './';
651
652 while (defined($f = readdir D)) {
653
654 next if $f eq '.' or $f eq '..';
655 # skip snapper or waffle snapshot dirs
656 next if $f =~ /^\.snapshots?$/ and $opt_R and not $rsnapshot;
657
658 if ($f =~ /$opt_m/) {
659 my $x = $dir.$f;
660 if (not -l $x and -d $x and not ($opt_R or $postsort or $opt_U)) {
661 push @dirs,$x;
662 } else {
663 push @files,$x;
664 }
665 }
666 }
667 closedir D;
668 unless ($postsort) {
669 @files = &$lcsort(@files);
670 @dirs = &$lcsort(@dirs);
671 }
672 } else {
673 warn "$prg: cannot read $dir : $!\n";
674 }
675
676 getaclc(@dirs,@files) if $opt_A;
677 return (@dirs,@files);
678 }
679
680
681 # reformat integer to string node
682 #
683 # INPUT: integer node
684 #
685 # OUTPUT: string node
686 sub nodes {
687 my $rdev = shift;
688 return sprintf("%03d,%03d", ($rdev >> 8) & 255, $rdev & 255);
689 }
690
691
692 sub pathsearch {
693 my $prg = shift;
694
695 foreach my $dir (split(':',$ENV{PATH})) {
696 return "$dir/$prg" if -x "$dir/$prg";
697 }
698 }
699
700
701 # reformat timetick to ISO date string
702 #
703 # INPUT: timetick
704 #
705 # OUTPUT: ISO date string
706 sub isodate {
707 my @d = localtime shift;
708 return sprintf('%d-%02d-%02d %02d:%02d:%02d',
709 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
710 }
711
712
713 # quote file name to printable name and escape shell meta chars
714 #
715 # INPUT: original file name
716 #
717 # OUTPUT: printable file name
718 sub quote {
719 local $_ = shift;
720 my $mc = '\'\[\]\\\\ `"$?&<>$*()|{};';
721
722 unless (defined $_) {
723 die "@_";
724 @x = caller;
725 die "@x";
726 }
727 if (s/[\000-\037\200-\237\241-\250]/?/g or /\'/) {
728 s/([$mc])/\\$1/g;
729 s/^~/\\~/;
730 # } elsif (/[$mc]/ or -d and /:/) {
731 } elsif (/[$mc]/) {
732 $_ = "'$_'";
733 }
734 return $_;
735 }
736
737
738 sub exist {
739 local $_ = shift;
740 return (-l or -e);
741 }
742
743
744 sub fmatch {
745 my $file = shift;
746 my $link = readlink($file)||'';
747
748 return $file if basename($file) =~ /$opt_F/i;
749 return $link if basename($link) =~ /$opt_F/i;
750 }
751
752
753 sub shellquote {
754 local $_ = shift;
755 s/([^\w\@\/!^%:_.,=+-])/\\$1/g;
756 return $_;
757 }
758
759
760 sub usage {
761 my $status = shift;
762 my $opts = '[-lastUcuidnrzFLRxNPS] [-f format] [-D X:Y]';
763 local *OUT = $status ? *STDERR : *STDOUT;
764
765 if ($prg ne 'ff') {
766 print OUT "usage: l $opts [-F 'REGEXP'] [FILE(s)]\n";
767 }
768 $opts =~ s/R//;
769 print OUT "usage: ff $opts 'REGEXP' [DIRECTORY]\n";
770 print OUT "usage: ff $opts DIRECTORY/FILE_WITH_HARDLINKS\n";
771 print OUT <<'EOD';
772 options: -l long list (implicit if called 'll')
773 -a list also .* files
774 -A flag POSIX capabilities and ACLs with a '*' in first column
775 -s sort by size
776 -t sort by time
777 -U sort by nothing (original i-node order)
778 -c list status change time instead of modification time
779 -u list last access time instead of modification time
780 -i list also inode and hard links numbers
781 -d do not list contents of diretories
782 -n numerical output
783 -r reverse list
784 -z squeeze size field (slows down output)
785 -L show absolute real path (dereference symbolic links)
786 -R recursive into subdirs
787 -x do not cross filesystem boundaries with -R
788 -m list files matching perl ^regexp$
789 -F find files matching case insensitive perl regexp (name and link)
790 -N show only normal (regular) files
791 -P list plain file names (without '\\' masking)
792 -S print statistics summary at end
793 -f user defined format output, format characters are:
794 m=mode, u=user, g=group, s=size, l=hard links count, i=inode
795 n=name only, d=date, a=access+modification+inodechange dates
796 -D list only files newer than X and older than Y
797 XY format: NUMBER[smhd] (s=seconds, m=minutes, h=hours, d=days)
798 XY format: YYYY-MM-DD (Y=year, M=month, D=day)
799 -H show help
800 -E show examples
801 EOD
802 exit $status;
803 }
804
805 sub examples {
806 print <<EOD;
807 l *.c # list files ending with .c
808 l -la # list all files in long format
809 l -Rrs # list files recursive reverse sorted by size
810 l -Pf mus # list files with native names and mode+user+size info
811 l -D 10d # list files newer than 10 days
812 ll # list files long format (equal to: l -l)
813 lll # list files extra long format (equal to: l -liS)
814 ff 'status.*mp3' # list files matching regexp (equal to: l -F 'status.*mp3')
815 ffl sda1 /dev # list devices matching sda1 (equal to: l -RF sda1 /dev)
816 EOD
817 exit;
818 }