By xxx0624
Done:
    ls
    ls -a
    ls -l
    ls /tmp
    ls -R
    ls -t
    FileName color
    FileName output

 /*
By xxx0624
Done:
ls
ls -a
ls -l
ls /tmp
ls -R
ls -t
FileName color
FileName output
*/ #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/ioctl.h>
#include<dirent.h>
#include<pwd.h>
#include<grp.h>
#include<unistd.h>
/*head file*/ #define LenOfName 256
#define maxN 1005
#define maxM 505
#define maxL 105
#define LenOfPath 256<<4
#define LS 0 //ls
#define LS_A 1 //ls -a
#define LS_L 2 //ls -l
#define LS_TMP 3 //ls /tmp
#define LS_R 4 //ls -R
#define LS_T 5 //ls -t
/*define file*/ void do_ls( int,char [] );
void dostat( char * );
void show_file_info( char *,struct stat * );
void mode_to_letters( int,char [] );
char *uid_to_name( uid_t );
char *gid_to_name( gid_t );
void getcolor( char * );
int get_file_type( char * );
int get_modify_time( char * );
void getWidth();
void display_R( char * );
int cmp1( const void * ,const void * );
int cmp2( const void * ,const void * );
int cmp3( const void * ,const void * ); struct outputFile{
char FileName[ LenOfName ];
int modify_time ;
int file_type ;
}Output[ maxN ],OutputPoint[ maxM ],Temp[ maxN+maxM ];
int colormode,foreground,background;
int terminalWidth ; void dostat( char *filename ){
struct stat info;
if( stat( filename,&info )==- ){
perror( filename );
}
else{
getcolor( filename );
show_file_info( filename,&info );
}
return ;
}
/*get file info*/ void mode_to_letters( int mode,char str[] ){
strcpy( str,"----------" ); if( S_ISDIR( mode ) ) str[] = 'd';
if( S_ISCHR( mode ) ) str[] = 'c';
if( S_ISBLK( mode ) ) str[] = 'b'; if( mode&S_IRUSR ) str[] = 'r';
if( mode&S_IWUSR ) str[] = 'w';
if( mode&S_IXUSR ) str[] = 'x'; if( mode&S_IRGRP ) str[] = 'r';
if( mode&S_IWGRP ) str[] = 'w';
if( mode&S_IXGRP ) str[] = 'x'; if( mode&S_IROTH ) str[] = 'r';
if( mode&S_IWOTH ) str[] = 'w';
if( mode&S_IXOTH ) str[] = 'x'; return ;
} char *uid_to_name( uid_t uid ){
struct passwd *pw_ptr;
static char numstr[ ];
if( (pw_ptr = getpwuid( uid ) )==NULL ){
sprintf(numstr,"%d",uid);
return numstr;
}
else{
return pw_ptr->pw_name;
}
}
char *gid_to_name( gid_t gid ){
struct group *grp_ptr;
static char numstr[ ];
if( (grp_ptr = getgrgid( gid ) )==NULL ){
sprintf(numstr,"%d",gid);
return numstr;
}
else{
return grp_ptr->gr_name;
}
}
/*get userid & groupid*/ int get_file_type( char *filename ){
struct stat info;
stat( filename,&info );
int file_type = ;
file_type = info.st_mode & S_IFMT;
return file_type;
}
/*get file type*/ int get_modify_time( char *filename ){
struct stat info;
stat( filename,&info );
int modify_time = ;
modify_time = info.st_mtime;
return modify_time;
}
/*get file last modify time*/ int isadir(char *str)
{
struct stat info; return ( lstat(str,&info) != - && S_ISDIR(info.st_mode) );
}
/*judge the file is or not a dir*/ void display_R( char *filename ){
char fullpath[ LenOfPath ];
struct outputFile Output2[ maxN ];
getcolor( filename );
printf("\033[%d;%d;%dm%s\033[0m :\n",colormode,foreground,background,filename);
DIR * dir_ptr ;
struct dirent *direntp;
int cntTemp = ;
int i ;
if( (dir_ptr = opendir( filename ) )==NULL ){
fprintf( stderr,"myls:cannot open %s\n",filename );
closedir( dir_ptr );
return ;
}
else {
while( (direntp = readdir( dir_ptr ))!=NULL ){
if( strcmp( direntp->d_name,"." )==||strcmp( direntp->d_name,".." )== )
continue;
strcpy( Output2[ cntTemp ].FileName,direntp->d_name );
strcpy( Output[ cntTemp ].FileName,direntp->d_name );
cntTemp ++ ;
}
closedir( dir_ptr );
if( cntTemp> ){
qsort( Output2,cntTemp,sizeof( Output2[] ),cmp1 );
qsort( Output,cntTemp,sizeof( Output[] ),cmp1 );
}
display_Ls( cntTemp );
for( i=;i<cntTemp;i++ ){
memset( fullpath,'\0',sizeof( fullpath ) );
strcpy( fullpath,filename );
strcat( fullpath,"/" );
strcat( fullpath,Output2[ i ].FileName );
if( isadir( fullpath ) )
display_R( fullpath );
}
}
return ;
}
/*active ls -R && Output */ void getWidth(){
char *tp;
struct winsize wbuf;
terminalWidth = ;
if( isatty(STDOUT_FILENO) ){
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &wbuf) == - || wbuf.ws_col == ){
if( tp = getenv("COLUMNS") )
terminalWidth = atoi( tp );
}
else
terminalWidth = wbuf.ws_col;
}
return ;
}
/******************************************************************
得出终端的宽度,默认宽度为80,如果获取失败,将激活-1选项
*******************************************************************/ int cmp1( const void *p ,const void *q ){
char T1[ LenOfName ],T2[ LenOfName ];
strcpy( T1,(*(struct outputFile *)p).FileName );
strcpy( T2,(*(struct outputFile *)q).FileName );
int len1 = strlen( T1 );
int i ;
for( i=;i<len1;i++ ){
if( T1[ i ]>='A' && T1[ i ]<='Z' ){
T1[ i ] = T1[ i ] - 'A' + 'a';
}
}
int len2 = strlen( T2 );
for( i=;i<len2;i++ ){
if( T2[ i ]>='A' && T2[ i ]<='Z' ){
T2[ i ] = T2[ i ] - 'A' + 'a';
}
}
return strcmp( T1,T2 );
}
/********************************************************
文件名排序 cmp1
*********************************************************/ int cmp2( const void *p,const void *q ){
return (*(struct outputFile *)p).file_type < (*(struct outputFile *)q).file_type ;
}
/********************************************************
文件类型排序 cmp2
*********************************************************/ int cmp3( const void *p,const void *q ){
return (*(struct outputFile *)p).modify_time < (*(struct outputFile *)q).modify_time ;
}
/********************************************************
文件修改时间排序 cmp3
*********************************************************/ void show_file_info( char *filename,struct stat * info_p ){
char modestr[ ]; mode_to_letters( info_p->st_mode,modestr ); printf("%s",modestr);
printf("%4d ",(int)info_p->st_nlink);
printf("%-8s ",uid_to_name(info_p->st_uid));
printf("%-8s ",gid_to_name(info_p->st_gid));
printf("%8ld ",(long)info_p->st_size);
printf("%.12s ",+ctime(&info_p->st_mtime));
printf("\033[%d;%d;%dm%s\033[0m\n",colormode,foreground,background,filename);
return ;
}
/*******************************************************
ls -l 的输出
********************************************************/ void getcolor( char *filename ){
struct stat info;
stat( filename,&info );
foreground = ;
background = ;
colormode = ;
switch ( (info.st_mode & S_IFMT) ){
case S_IFREG: /*regular 普通文件 , 白色*/
foreground = ;
break;
case S_IFLNK: /*symbolic link 链接文件 , 青蓝色*/
foreground = ;
colormode = ;
break;
case S_IFSOCK: /*紫红色*/
foreground = ;
colormode = ;
break;
case S_IFDIR: /*directory 目录文件 , 蓝色*/
foreground = ;
break;
case S_IFBLK: /*block special 块设备文件 , 黄色*/
foreground = ;
colormode = ;
break;
case S_IFCHR: /*character special 字符设备文件 , 黄色*/
foreground = ;
colormode = ;
break;
case S_IFIFO: /*fifo 绿色*/
foreground = ;
colormode = ;
break;
}
}
/*******************************************************
给文件添加颜色
********************************************************/ void display_Ls( int cnt ){
int wordLenMax = ;//the LenMax word
int wordRowNum = ;//the amount of one row
int wordColNum = ;//the amount of one col
int i , j;
for( i=;i<cnt;i++ ){
if( i== ) wordLenMax = strlen( Output[ i ].FileName );
else wordLenMax = wordLenMax>strlen( Output[ i ].FileName )?wordLenMax:strlen( Output[ i ].FileName );
}
wordLenMax += ;
wordRowNum = terminalWidth / wordLenMax;
if( cnt%wordRowNum== ) wordColNum = cnt / wordRowNum;
else wordColNum = cnt / wordRowNum + ;
for( i=;i<wordColNum;i++ ){
j = i;
while( j<cnt ){
getcolor( Output[ j ].FileName );
printf("\033[%d;%d;%dm%-15s\033[0m ",colormode,foreground,background,Output[ j ].FileName);
j += wordColNum;
}
printf("\n");
}
return ;
}
/**********************************************************
ls的分栏输出
***********************************************************/ void display_Ls_a( int cntPoint,int cnt ){
int CNT = ;
int wordLenMax = ;//the LenMax word
int wordRowNum = ;//the amount of one row
int wordColNum = ;//the amount of one col
int i , j;
for( i=;i<cntPoint;i++ ){
strcpy( Temp[ CNT ].FileName,OutputPoint[ i ].FileName );
Temp[ CNT ].file_type = OutputPoint[ i ].file_type;
Temp[ CNT ].modify_time = OutputPoint[ i ].modify_time;
CNT ++;
wordLenMax = wordLenMax>strlen( OutputPoint[ i ].FileName )?wordLenMax:strlen( OutputPoint[ i ].FileName );
}
for( i=;i<cnt;i++ ){
strcpy( Temp[ CNT ].FileName,Output[ i ].FileName );
Temp[ CNT ].file_type = Output[ i ].file_type;
Temp[ CNT ].modify_time = Output[ i ].modify_time;
CNT ++;
wordLenMax = wordLenMax>strlen( Output[ i ].FileName )?wordLenMax:strlen( Output[ i ].FileName );
}
wordLenMax += ;
wordRowNum = terminalWidth / wordLenMax;
if( CNT%wordRowNum== ) wordColNum = CNT / wordRowNum;
else wordColNum = CNT / wordRowNum + ;
for( i=;i<wordColNum;i++ ){
j = i;
while( j<CNT ){
getcolor( Temp[ j ].FileName );
printf("\033[%d;%d;%dm%-15s\033[0m ",colormode,foreground,background,Temp[ j ].FileName);
j += wordColNum;
}
printf("\n");
}
return ;
}
/**********************************************************
ls -a 的分栏输出
***********************************************************/ void display_Ls_tmp( int cnt ){
display_Ls( cnt );
return ;
}
/**********************************************************
ls /tmp 的分栏输出
***********************************************************/ void display_Ls_R( char *filename ){
char fullpath[ LenOfPath ];
strcpy( fullpath,get_current_dir_name() );
strcat( fullpath,"/" );
strcat( fullpath,filename );
display_R( fullpath );
return ;
}
/**********************************************************
ls -R 的分栏输出
***********************************************************/ void do_ls( int myJudge,char myOrder[] ){
if( myJudge==LS_R ){
display_Ls_R( "." );
return ;
}
/**********************************************************
ls -R
***********************************************************/
char dirname[ maxL ];
if( myJudge!=LS_TMP ){
strcpy( dirname,"." );
}
else {
strcpy( dirname,myOrder );
}
DIR * dir_ptr;
struct dirent *direntp;
int cntOutput = ;
int cntOutputPoint = ;
if( ( dir_ptr = opendir( dirname ) )==NULL ){
fprintf( stderr,"myls:cannot open %s\n",dirname );
}
else{
while( (direntp = readdir( dir_ptr ) )!=NULL ){
if( direntp->d_name[ ]=='.' ) {
strcpy( OutputPoint[ cntOutputPoint ].FileName,direntp->d_name );
OutputPoint[ cntOutputPoint ].file_type = get_file_type( OutputPoint[ cntOutputPoint ].FileName );
OutputPoint[ cntOutputPoint ].modify_time = get_modify_time( OutputPoint[ cntOutputPoint ].FileName );
cntOutputPoint ++;
}
else {
strcpy( Output[ cntOutput ].FileName,direntp->d_name );
Output[ cntOutput ].file_type = get_file_type( Output[ cntOutput ].FileName );
Output[ cntOutput ].modify_time = get_modify_time( Output[ cntOutput ].FileName );
cntOutput ++;
}
} if( myJudge==LS_T ){
qsort( OutputPoint,cntOutputPoint,sizeof( OutputPoint[] ),cmp3 );
qsort( Output,cntOutput,sizeof( Output[] ),cmp3 );
}
else {
qsort( OutputPoint,cntOutputPoint,sizeof( OutputPoint[] ),cmp1 );
qsort( Output,cntOutput,sizeof( Output[] ),cmp1 );
}
/**********************************************************
预处理输出
***********************************************************/
if( myJudge==LS||myJudge==LS_T ){
display_Ls( cntOutput );
closedir( dir_ptr );
}
/**********************************************************
ls && ls -t
***********************************************************/
else if( myJudge==LS_A ){
display_Ls_a( cntOutputPoint,cntOutput );
closedir( dir_ptr );
}
/**********************************************************
ls -a
***********************************************************/
else if( myJudge==LS_L ){
int i;
for( i=;i<cntOutput;i++ )
dostat( Output[ i ].FileName );
closedir( dir_ptr );
}
/**********************************************************
ls -l
***********************************************************/
else {
display_Ls_tmp( cntOutput );
closedir( dir_ptr );
}
/**********************************************************
ls /tmp
***********************************************************/
}
return ;
}/*sovle*/ int main( int argc, char *argv[] ){
getWidth( );
int i ;
if( argc== ){
do_ls( LS,"ls" );
}
else{
int ord;
while( (ord = getopt(argc,argv,":laRt"))!=- ){
switch( ord ){
case 'a':
do_ls( LS_A,"ls-a" );
break;
case 'l':
do_ls( LS_L,"ls-l" );
break;
case 'R':
do_ls( LS_R,"ls-R" );
break;
case 't':
do_ls( LS_T,"ls-t" );
break;
default :
break;
}
}
for( i=;i<argc;i++ ){
if( argv[ i ][ ]=='-' ) continue;
printf("%s:\n",argv[ i ]);
do_ls( LS_TMP,argv[ i ] );
}
}
return ;
}
/*main*/

Linux---Ls命令 初级实现的更多相关文章

  1. linux ls命令教程,ls命令怎么用,全部招数都教你

    linux ls命令的用法大全 学习linux这么久了,最常用的命令莫属 ls命令了,今天就总结下ls命令的用法与经验技巧.   ls命令按文件大小查看文件   a.降序:ls -lsh moudae ...

  2. Linux ls命令详解

    ls 命令可以说是Linux下最常用的命令之一. -a 列出目录下的所有文件,包括以 . 开头的隐含文件.(后有详解)-b 把文件名中不可输出的字符用反斜杠加字符编号(就象在c语言里一样)的形式列出. ...

  3. linux ls 命令

    ls 命令是 Linux 下最常用的命令之一,用来查询目录下的内容(list directory contents).本文将介绍其基本的用法和一些典型的用例.笔者使用的测试环境为 ubuntu 16. ...

  4. Linux ls命令

    ls:即列表List的意思,用来列出目录下的文件用来列出给定目录下的文件,参数为空默认列出当前目录下的文件. 用法是:ls [选项] [目录] 常用的选项有 -a, –all 列出目录下的所有文件,包 ...

  5. linux ls命令按时间显示文件

      本文介绍下,使用ls命令显示文件,并按时间排序的方法,供大家学习参考. 在linux系统中,使用ls命令按时间排序文件,其实很简单,如下: #ls -tr 即可按时间排序当前目录下的文件. 附,l ...

  6. Linux ls命令参数详解

    -a -- 全部(all).列举目录中的全部文件,包括隐藏文件(.filename).位于这个列表的起首处的 .. 和 . 依次是指父目录和你的当前目录.      -l -- 长(long).列举目 ...

  7. Linux ls命令参数详解 <转>

    下面是一个与 ls 一起使用的一些常用选项的简短列表.请记住,你可以通过阅读 ls 的说明书页(man ls)来获得选项的完整列表. -a – 全部(all).列举目录中的全部文件,包括隐藏文件(.f ...

  8. Linux ls 命令实现(简化版)

    在学习linux系统编程的时候,实现了ls命令的简化版本号. 实现的功能例如以下: 1. 每种文件类型有自己的颜色 (- 普通文件, d 文件夹文件, l 链接文件. c 字符设备文件. b 快设备文 ...

  9. 你应该了解的 7个Linux ls 命令技巧

    在前面我们系列报道的两篇文章中,我们已经涵盖了关于‘ls’命令的绝大多数内容.本文时‘ls命令’系列的最后一部分.如果你还没有读过该系列的其它两篇文章,你可以访问下面的链接. 15 个‘ls’命令的面 ...

  10. Linux ls命令详解-乾颐堂CCIE

      ls命令用法举例: 例一:列出/home文件夹下的所有文件和目录的详细资料: 1 ls -l -R /home 命令参数之前要有一短横线“-”, 上面的命令也可以这样写: 1 ls -lR /ho ...

随机推荐

  1. self指向函数地址 动态调用函数的简单例子

    #import <Foundation/Foundation.h> @interface Person : NSObject - (void)test1; - (void)test2:(N ...

  2. php和apache的关系和作用()

    出处:http://blog.csdn.net/hongweideng/article/details/41723465 php和apache的关系和作用是很多学习php的朋友非常关注的问题 首先在一 ...

  3. FileZilla ftp服务器安装

    在官网下载server版本的FileZilla,我下载的是(The latest stable version of FileZilla Server is 0.9.41) 然后直接安装就可了,我由于 ...

  4. .Net Core 中的包、元包与框架(Packages, Metapackages and Frameworks)

    包,元包与框架 本文翻译自 Packages, Metapackages and Frameworks. .Net Core 是一种由 NuGet 包组成的平台.一些产品体验受益于代码包的细粒度定义, ...

  5. lex&yacc6 ---error

    类的检测 http://blog.csdn.net/pandaxcl/article/details/1536784

  6. 一个CFile::Remove引起的奇怪问题

    今天收到测试的一个反馈,我们的一个程序,在WIN7.WIN8下安装后,运行不起来,在进程列表中可以看到,但就是不出来窗口,同样的程序在XP下正常,在UAC关闭的情况下也正常,在以管理员权限运行时也正常 ...

  7. ssh 登陆指定 验证文件

    当前用户jim ssh-keygen -t rsa 生成密钥 把pub结尾的公用密钥数据追加到192.168.1.3上的 /home/tom/.ssh/authKeys(文件名可能不一样) ssh - ...

  8. jquery 获取 CheckBox 的状态

    <td style="width:220px;vertical-align:central;"><input type="checkbox" ...

  9. Spark小课堂Week3 FirstSparkApp(RDD开发)

    Spark小课堂Week3 FirstSparkApp 问题:Java有哪些数据结构 大致有如下几种,其中List与Map是最重要的: List Map Set Array Heap Stack Qu ...

  10. python装饰器总结

    一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象.简单的说装饰器就是一个用来返回函数的函数 ...