#!/usr/bin/perl
use strict;
#use Win32::File qw(GetAttributes);
my $dir=$ARGV[1];
my $count=0;
my @stack=();
my @ref=();
my @size=();
my @time=();
my $size;
my $datestr;
my $year;
my $mon;
my $day;
my $hh;
my $mm;
my $ss;
if(defined(my $opt=$ARGV[0])){
if ($opt !~ /^\-/){
print "you should add '-' before your argment.\n"
} else {
$opt =~ s/^\-//o;
last if $opt eq '-';
if($opt eq '-help'){
&usage;
exit;
}
if($opt eq '-version'){
&ver;
exit;
}
if ( $opt=~/[a-z]/ ){
&a if $opt eq 'a';
&d if $opt eq 'd';
&l if $opt eq 'l';
&r if $opt eq 'r';
&s if $opt eq 's';
&re if $opt eq 'e';
&sz if $opt eq 'c';
&st if $opt eq 't';
&lsf if $opt eq 'f';
&s if $opt =~/\w{2,}/;
#&t if $opt eq 't';
} elsif ( $opt=~/[0-9]{0,}/ ){
#deal with like : ls -1000
&dir;
while(<*>){
$count++;
my $stp=&size($_);
my $size=&tpe($stp);
my $date=&gmdate($_);
my $file=&jdf($_);
&fmt($file,$size,$date);
if ( $count>$opt-1 ){
last;
}
}
}
}
} else {
&l;
}
sub usage {
print <
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Mandatory arguments to long options are mandatory for short options too.
-a, list all .
-d, list directory .
-n, like -l, but list numer of input
-r, reverse order while sorting
-l, use a long listing format
-r, reverse order while sorting
-e, list subdirectories recursively
-s, sort list.
-c, list by size.
-t, list by time.
--help display this help and exit
--version output version information and exit
SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, GB 1000*1000*1000, M 1024*1024
*1024.
Report bugs to
EOF
}
sub ver{
print <
Written by irror
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
}
sub dir{
if (defined($dir)){
chdir ("$dir");
} else {
chdir (".");
}
}
sub a{
&dir;
while(<*>){
if ( -d $_ ){
print "directory :|===> $_"."\n";
}else{
print "file:|---< $_"."\n";
}
}
}
sub d{
&dir;
while(<*>){
if ( -d $_ ){
print "directory :|===> $_"."\n";
}
}
}
########## -l 选项#################
sub l{
&dir;
while(<*>){
my $stp=&size($_);
my $size=&tpe($stp);
my $date=&gmdate($_);
my $file=&jdf($_);
&fmt($file,$size,$date);
}
}
###########输出打印格式#############
sub fmt{
my($file,$size,$date)=@_;
write();
format STDOUT_TOP=
filename filesize(B/K/M/G) filedate
---------------------------------- ------------------ -----------------
.
format STDOUT=
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<
$file, $size, $date
.
}
############反向排序文件#############
sub r{
my @tmp1=();
my @tmp2=();
my $cc;
&dir;
while(<*>){
if ( -d $_ ){
$_=~s;\s;?;g;
push(@tmp1,$_);
}else{
$_=~s;\s;?;g;
push(@tmp2,$_);
}
}
my @tp= sort("@tmp1 @tmp2");
foreach(reverse <@tp>){
$_=~s;\?; ;g;
my $stp=&size($_);
my $size=&tpe($stp);
my $date=&gmdate($_);
my $file=&jdf($_);
&fmt($file,$size,$date);
}
}
#############排序文件################
sub s{
my @tmp1=();
my @tmp2=();
my $cc;
&dir;
while(<*>){
if ( -d $_ ){
$_=~s;\s;?;g; #将带有空格的特殊文件名中的空格或tab替换成'?' 后面再把'?'替换回空格。
push(@tmp1,$_);
}else{
$_=~s;\s;?;g;
push(@tmp2,$_);
}
}
my @tp= sort("@tmp1 @tmp2"); #@tmp1与@tmp2中间空格是必须的,不然@tmp1最后一个元素与@tmp2第一个元素会合成一个元素,造成文件不存在。
foreach(<@tp>){
$_=~s;\?; ;g;
my $stp=&size($_);
my $size=&tpe($stp);
my $date=&gmdate($_);
my $file=&jdf($_);
&fmt($file,$size,$date);
}
}
#############文件修改时间##############
sub gmdate {
my $rev=shift;
$datestr=(lstat $rev)[9];
&ttime($datestr);
}
#############判断目录和文件#############
sub jdf{
my $rr=shift;
my $fe;
if (-d $rr){
$fe='D#=>>'.$rr;
}else{
$fe='f#=<<'.$rr;
}
return $fe;
}
############给定合适后缀#################
sub tpe{
my $bb=shift;
my $type;
if($bb<1048576 && $bb>=1024){
$type=(eval "$bb/1024")."K";
}elsif($bb>=1048576 && $bb<1073741824){
$type=(eval "$bb/(1024*1024)")."M";
}elsif($bb>=1073741824){
$type=(eval "$bb/(1024*1024*1024)")."G";
}else {
$type=$bb."B";
}
$type =~ s;^(\d+\.\d{2})(\d+)([B|K|M|G]);$1$3;g; #$type now is a array.
scalar($type);
}
##########给出文件大小#################
sub size{
my $civ=shift;
my $sze=(stat $civ)[7];
}
##########递归打印文件名#########
sub re{
&dir;
while(<*>){
&pr_f($_);
}
}
sub pr_f{
if(-d){
my $tmp='|-'."$_";
push(@stack,$tmp);
print join('',@stack)."\n";
chdir $_;
foreach(<*>){
&pr_f($_);
}
pop @stack;
chdir "..";
} else {
print join('',@stack)."|-$_"."\n";
}
}
####按文件大小排序##############
sub sz{
&dir;
open(TMP,'>c:\ptmp') || die "can't create file : $!\n";
while(<*>){
my $size=&size($_);
my $date=&gmdate($_);
my $file=&jdf($_);
$file=~s;\s;?;g;
print TMP $file.' '.$size.' '.$date."\n";
}
close(TMP);
open (FH,'c:\ptmp') || die "can't open file : $!\n";;
while(
my @row=split(/\s+/,$_);
my $ref=[[$row[0]],[$row[1]],[[$row[2]],[$row[3]],[$row[4]]]];
push(@ref,$ref);
}
close(FH);
foreach( @ref ){
push (@size,$_->[1][0].'|'.$_->[0][0].'|'.$_->[2][0][0].' '.$_->[2][1][0].' '. $_->[2][2][0]);
}
for(my $i=0;$i<@size;$i++){
for(my $j=$i+1;$j<@size;$j++){
my $tmp1=$size[$i];
my $tmp2=$size[$j];
$tmp1=~s/(\d+)[\|].*/$1/;
$tmp2=~s/(\d+)[\|].*/$1/;
if ($tmp1>$tmp2){
($size[$i],$size[$j])=($size[$j],$size[$i]);
}
}
}
foreach (@size){
$_=~/^(\d+)\|(.*)\|(.*)$/;
my $file=$2;
my $size=&tpe($1);
my $date=$3;
$file=~s;\?; ;g; #处理文件名含有空格的文件。
&fmt($file,$size,$date);
}
unlink 'c:\ptmp';
}
########按文件修改时间排序####################
sub st{
&dir;
open(TMP,'>c:\ptmp') || die "can't create file : $!\n";
while(<*>){
my $size=&size($_);
my $date=(stat $_)[9];
my $file=&jdf($_);
$file=~s;\s;?;g;
print TMP $file.' '.$size.' '.$date."\n";
}
close(TMP);
open (FH,'c:\ptmp') || die "can't open file : $!\n";;
while(
my @row=split(/\s+/,$_);
my $ref=[[$row[0]],[$row[1]],[$row[2]]];
push(@ref,$ref);
}
close(FH);
foreach( @ref ){
push (@time,$_->[1][0].'|'.$_->[0][0].'|'.$_->[2][0]);
}
for(my $i=0;$i<@time;$i++){
for(my $j=$i+1;$j<@time;$j++){
my ($tmp1,$tmp2)=($time[$i],$time[$j]);
$tmp1=~s/(\d+)[\|].*[\|](.*)/$2/;
$tmp2=~s/(\d+)[\|].*[\|](.*)/$2/;
if ($tmp1>$tmp2){
($time[$i],$time[$j])=($time[$j],$time[$i]);
}
}
}
foreach (@time){
$_=~/^(\d+)\|(.*)\|(.*)$/;
my $file=$2;
my $size=&tpe($1);
my $date=&ttime($3);
$file=~s;\?; ;g;
&fmt($file,$size,$date);
}
unlink 'c:\ptmp' || die "delete file failed:$!\n";
}
###########for need#############
sub ttime{
my $ttime=shift;
$year=(gmtime $ttime)[5]+1900;
$mon=(gmtime $ttime)[4]+1;
$day=(gmtime $ttime)[3];
$hh=(gmtime $ttime)[2];
$mm=sprintf("%02d",(gmtime $ttime)[1]);
$ss=sprintf("%02d",(gmtime $ttime)[0]);
return $year.' '."$mon\/$day".' '."$hh:$mm:$ss";
}
##########列出文件属性####################
sub lsf{
my $stp=&size(<$dir>);
my $size=&tpe($stp);
my $date=&gmdate($_);
my $file=&jdf($_);
&fmt($file,$size,$date);
}