-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathls.pl
More file actions
executable file
·252 lines (217 loc) · 5.89 KB
/
ls.pl
File metadata and controls
executable file
·252 lines (217 loc) · 5.89 KB
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
#!/usr/bin/perl
# ls.pl - Prints colorized directory items like GNU ls does
#
# Copyright (C) 2010 Joachim "Joe" Stiegler <blablabla@trullowitsch.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 3 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, see <http://www.gnu.org/licenses/>.
#
# --
#
# Version: 1.0 - 2010-10-13
#
# TODO:
# Option t (sort by timestamp)
# Option d (stat directory)
# Standard output like ls -C ($cols exists...)
# If file is a link, show target (fileA -> fileB)
use warnings;
use strict;
use Cwd;
use Term::ANSIColor;
use Fcntl ':mode';
use POSIX qw(strftime);
use Getopt::Std;
if (!getopts ("ahilrsA")) {
die "Usage: $0 [-ahilrsA]\n";
}
our ($opt_a, $opt_h, $opt_i, $opt_l, $opt_r, $opt_s, $opt_A);
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks);
my @dirent;
my $columns = `tput cols`;
if (!defined($columns)) {
my $columns = 80;
}
if (!@ARGV) {
push @ARGV, getcwd;
}
my $argc = scalar @ARGV;
sub getsize {
my $size = shift(@_);
if ( ($size >= 1024) && ($size <= 1048576) ) {
$size = sprintf("%.1f%s", $size / 1024, "K");
}
elsif ( ($size >= 1048576) && ($size <= 1073741824) ) {
$size = sprintf("%.1f%s", $size / (1024 ** 2), "M");
}
elsif ( ($size >= 1073741824) && ($size <= 1099511627776) ) {
$size = sprintf("%.1f%s", $size / (1024 ** 3), "G");
}
elsif ( ($size >= 1099511627776) && ($size <= 1125899906842624) ) {
$size = sprintf("%.1f%s", $size / (1024 ** 4), "T");
}
return ($size);
}
sub readdirectory {
my $dir = shift(@_);
opendir(my $pwd, $dir) || die "$dir: $!\n";
if (defined($opt_r)) {
@dirent = sort {$b cmp $a} readdir($pwd);
}
else {
@dirent = sort readdir($pwd);
}
closedir $pwd;
return (@dirent);
}
sub readfile {
@dirent = shift(@_);
return (@dirent);
}
for (my $d=0; $d<=$argc-1;$d++) {
if (-e $ARGV[$d]) {
if (-d $ARGV[$d]) {
print "\n$ARGV[$d]:\n" if ($argc > 1);
@dirent = readdirectory($ARGV[$d]);
}
else {
@dirent = readfile($ARGV[$d]);
}
}
else {
print "$ARGV[$d]: No such file or directory\n";
next;
}
# Find the length of the longest item.
my $current=0;
my $longest=0;
foreach my $j (@dirent) {
foreach my $k (@dirent) {
if (length($j) > length($k)) {
$current = length($j);
}
}
if ($longest < $current) {
$longest = $current;
}
}
# Number of columns for output like ls -C
my $cols = int($columns / ($longest + 1));
my $items = scalar @dirent;
#FIXME:
# If "ls.pl -l foodir/ foofile", then foofile can't be found, because our cwd is now foodir
chdir $ARGV[$d];
for (my $i=0; $i<=$items-1; $i++) {
if ( ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat($dirent[$i]) ) {
my $time = strftime "%b %e %H:%M:%S %Y", localtime($mtime);
my $user = getpwuid($uid);
my $group = getgrgid($gid);
if (defined($opt_h)) {
$size = getsize($size);
}
if (defined($opt_A)) {
if ($dirent[$i] =~ /^\.$/) {
next;
}
elsif ($dirent[$i] =~ /^\.\.$/) {
next;
}
}
if ( (!defined($opt_a)) && (!defined($opt_A)) ) {
if ($dirent[$i] =~ /^\./) {
next;
}
}
my $moderwx;
if (-d $dirent[$i]) {
$moderwx .= "d";
}
elsif (-l $dirent[$i]) {
$moderwx .= "l";
}
elsif (-f $dirent[$i]) {
$moderwx .= "-";
}
elsif (-b $dirent[$i]) {
$moderwx .= "b";
}
elsif (-c $dirent[$i]) {
$moderwx .= "c";
}
elsif (-p $dirent[$i]) {
$moderwx .= "p";
}
elsif (-S $dirent[$i]) {
$moderwx .= "S";
}
use Switch;
my $nmode = sprintf ("%4o", S_IMODE($mode));
my @modes = split (/ */, $nmode);
foreach my $number (@modes) {
switch ($number) {
case 0 { $moderwx .= "---"; }
case 7 { $moderwx .= "rwx"; }
case 6 { $moderwx .= "rw-"; }
case 5 { $moderwx .= "r-x"; }
case 4 { $moderwx .= "r--"; }
}
}
if ( (defined($opt_l)) && (defined($opt_i)) && (defined($opt_s)) ) {
printf ("%10s %5s %10s %4s %10s %10s %10s %20s ", $ino,$blocks,$moderwx,$nlink,$user,$group,$size,$time);
}
elsif ( (defined($opt_l)) && (defined($opt_i)) && (!defined($opt_s)) ) {
printf ("%10s %10s %4s %10s %10s %10s %20s ", $ino,$moderwx,$nlink,$user,$group,$size,$time);
}
elsif ( (defined($opt_l)) && (!defined($opt_i)) && (defined($opt_s)) ) {
printf ("%5s %10s %4s %10s %10s %10s %20s ", $blocks,$moderwx,$nlink,$user,$group,$size,$time);
}
elsif ( (defined($opt_l)) && (!defined($opt_i)) && (!defined($opt_s)) ) {
printf ("%10s %4s %10s %10s %10s %20s ", $moderwx,$nlink,$user,$group,$size,$time);
}
elsif ( (!defined($opt_l)) && (defined($opt_i)) && (defined($opt_s)) ) {
printf ("%10s %5s ", $ino,$blocks);
}
elsif ( (!defined($opt_l)) && (defined($opt_i)) && (!defined($opt_s)) ) {
printf ("%10s ", $ino);
}
elsif ( (!defined($opt_l)) && (!defined($opt_i)) && (defined($opt_s)) ) {
printf ("%5s ", $blocks);
}
if (-d $dirent[$i]) {
print color 'bold blue';
}
elsif (-l $dirent[$i]) {
if (!stat($dirent[$i])) {
print color 'bold red';
}
else {
print color 'bold cyan';
}
}
elsif (-x $dirent[$i]) {
print color 'bold green';
}
# if (!defined($opt_l)) {
# for (my $col=0; $col<=$cols; $col++) {
# printf "%s ", $dirent[$i++];
# }
# }
# else {
print $dirent[$i];
# }
print color 'reset';
print "\n";
}
else {
print "can't stat file $dirent[$i]. this should never happens...";
}
}
}