Ticket #2031: ls_hack

File ls_hack, 4.5 KB (added by mcaccount, 14 years ago)

The 'not very nice solution' for both perl 5.0 and perl 5.8

Line 
1#LIST /${FISH_DIR }
2LC_TIME=C
3export LC_TIME
4
5fish_list_lsq ()
6{
7FISH_DIR="$1"
8ls -Qlan "${FISH_DIR}" 2>/dev/null | grep '^[^cbt]' | (
9while read p l u g s m d y n; do
10    echo "P$p $u.$g"
11    echo "S$s"
12    echo "d$m $d $y"
13    echo ":$n"
14    echo
15done
16)
17
18ls -Qlan "${FISH_DIR}" 2>/dev/null | grep '^[cb]' | (
19while read p l u g a i m d y n; do
20    echo "P$p $u.$g"
21    echo "E$a$i"
22    echo "d$m $d $y"
23    echo ":$n"
24    echo
25done
26)
27echo '### 200'
28}
29
30fish_list_sed ()
31{
32FISH_DIR="$1"
33ls -lan "${FISH_DIR}" 2>/dev/null | grep '^[^cbt]' | (
34while read p l u g s rec; do
35    if [ -n "$g" ]; then
36        if [ -n "$FISH_HAVE_DATE_MDYT" ]; then
37            filename=`echo "$rec"| sed 's/[^[:space:]]\+ \+[^[:space:]]\+ \+[^[:space:]]\+ //'`
38            filedate=`echo "$rec"| sed 's/\([^[:space:]]\+ \+[^[:space:]]\+ \+[^[:space:]]\+\) .*/\1/'`
39        else
40            filename=`echo "$rec"| sed 's/[^[:space:]]\+ \+[^[:space:]]\+ //'`
41            filedate=`echo "$rec"| sed 's/\([^[:space:]]\+ \+[^[:space:]]\+\) .*/\1/'`
42        fi
43        pfile=\"`echo "$filename" | sed -e 's#^\(.*\) -> \(.*\)#\1" -> "\2#'`\"
44        echo "P$p $u.$g"
45        echo "S$s"
46        if [ -n "$FISH_HAVE_DATE_MDYT" ]; then
47            echo "d$filedate"
48        else
49            echo "D$filedate"
50        fi
51        echo ":$pfile"
52        echo
53    fi
54done
55)
56ls -lan "${FISH_DIR}" 2>/dev/null | grep '^[cb]' | (
57while read p l u g a i rec; do
58    if [ -n "$g" ]; then
59        if [ -n "$FISH_HAVE_DATE_MDYT" ]; then
60            filename=`echo "$rec"| sed 's/[^[:space:]]\+ \+[^[:space:]]\+ \+[^[:space:]]\+ //'`
61            filedate=`echo "$rec"| sed 's/\([^[:space:]]\+ \+[^[:space:]]\+ \+[^[:space:]]\+\) .*/\1/'`
62        else
63            filename=`echo "$rec"| sed 's/[^[:space:]]\+ \+[^[:space:]]\+ //'`
64            filedate=`echo "$rec"| sed 's/\([^[:space:]]\+ \+[^[:space:]]\+\) .*/\1/'`
65        fi
66        pfile=\"`echo "$filename" | sed -e 's#^\(.*\) -> \(.*\)#\1" -> "\2#'`\"
67        echo "P$p $u.$g"
68        echo "E$a$i"
69        if [ -n "$FISH_HAVE_DATE_MDYT" ]; then
70            echo "d$filedate"
71        else
72            echo "D$filedate"
73        fi
74        echo ":$pfile"
75        echo
76    fi
77done
78)
79echo '### 200'
80}
81
82fish_list_poor_ls ()
83{
84FISH_DIR="$1"
85ls -lan "${FISH_DIR}" 2>/dev/null | grep '^[^cbt]' | (
86while read p l u g s m d y n n2 n3; do
87    if [ -n "$g" ]; then
88        if [ "$m" = "0" ]; then
89            s=$d; m=$y; d=$n; y=$n2; n=$n3
90        else
91            n=$n" "$n2" "$n3
92        fi
93        echo "P$p $u $g"
94        echo "S$s"
95        echo "d$m $d $y"
96        echo ":"$n
97        echo
98    fi
99done
100)
101ls -lan "${FISH_DIR}" 2>/dev/null | grep '^[cb]' | (
102while read p l u g a i m d y n n2 n3; do
103    if [ -n "$g" ]; then
104        if [ "$a" = "0" ]; then
105            a=$m; i=$d; m=$y; d=$n; y=$n2; n=$n3
106        else
107            n=$n" "$n2" "$n3
108        fi
109        echo "P$p $u $g"
110        echo "S$s"
111        echo "d$m $d $y"
112        echo ":"$n
113        echo
114    fi
115done
116)
117echo '### 200'
118}
119
120fish_list_perl ()
121{
122FISH_DIR=$1
123perl -e '
124use strict;
125use POSIX;
126use Fcntl;
127use POSIX ":fcntl_h"; #S_ISLNK was here until 5.6
128my $dirname = $ARGV[0];
129if (opendir (DIR, $dirname)) {
130while((my $filename = readdir (DIR))){
131    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat("$dirname/$filename");
132    my $mloctime= strftime("%%m-%%d-%%Y %%H:%%M", localtime $mtime);
133    my $strutils_shell_escape_regex = s/([;<>\*\|`&\$!#\(\)\[\]\{\}:'\''"\ \\])/\\$1/g;
134    my $e_filename = $filename;
135    $e_filename =~ $strutils_shell_escape_regex;
136
137        # here we count the required values manually since
138        # I was not able to 'link' it anyhow using standard perl way
139        my $s_ifmt =   $mode & 00170000;
140        my $s_imode =  $mode & 07777;
141        my $s_islnk = ($mode & 00170000) == 0120000;
142
143    if ($s_islnk) {
144        my $linkname = readlink ("$dirname/$filename");
145        $linkname =~ $strutils_shell_escape_regex;
146        printf("R%%o %%o $uid.$gid\nS$size\nd$mloctime\n:\"$e_filename\" -> \"$linkname\"\n\n", $s_imode, $s_ifmt);
147    } else {
148        printf("R%%o %%o $uid.$gid\nS$size\nd$mloctime\n:\"$e_filename\"\n\n", $s_imode,, $s_ifmt);
149    }
150}
151    printf("### 200\n");
152    closedir(DIR);
153} else {
154    printf("### 500\n");
155}
156exit 0
157' "/${FISH_DIR}"
158}
159
160if [ -n "${FISH_HAVE_PERL}" ]; then
161    fish_list_perl "/${FISH_FILENAME}"
162elif [ -n "${FISH_HAVE_LSQ}" ]; then
163    fish_list_lsq "/${FISH_FILENAME}"
164elif [ -n "${FISH_HAVE_SED}" ]; then
165    fish_list_sed "/${FISH_FILENAME}"
166else
167    fish_list_poor_ls "/${FISH_FILENAME}"
168fi