linux c模拟ls命令详解

 更新时间:2020年4月25日 17:45  点击:1702
复制代码 代码如下:

/*
 模拟ls命令,实现参数-tariRl.有些代码重复出现,可改进
 可以在该程序的基础上增加参数,该程序思路清晰,容易扩展
 */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
#include<dirent.h>
#include<sys/ioctl.h>
#include<pwd.h>
#include<grp.h>
#define LL (1<<0)  /*带l参数标志*/
#define IL (1<<1)  /*带i参数标志*/
#define RU (1<<2)  /*带R参数标志*/
#define TL (1<<16) /*带t参数标志*/
#define AL (1<<17) /*带a参数标志*/
#define RL (1<<18) /*带r参数标志*/
#define LENGTH 500 /*字符串最大长度*/
#define MAX_FILE_COUNT 500/*文件列表最大长度*/
/*函数声明*/
void error(char* error_message,int line);
void display(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode);
int file_list(char* path,struct stat* stalist,char (*namelist)[LENGTH],int mode);
void display_simply(char (*namelist)[LENGTH],int n);
void display_LL(struct stat * stalist,char (*namelist)[LENGTH],int n);
void display_IL(struct stat * stalist,char (*namelist)[LENGTH],int n);
void display_LL_IL(struct stat * stalist,char (*namelist)[LENGTH],int n);
void display_RU(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode);
void display_RU_LL(struct stat * stalist,char (*namelist)[LENGTH],int n,int mode);
void display_RU_IL(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode);
void display_RU_IL_LL(struct stat * stalist,char (*namelist)[LENGTH],int n,int mode);
/*主函数*/
int main(int argc,char **argv)
{
    int i,j;
    /*mode的前16个位用来标志那些能影响显示的参数,16位之后的位用来标志不影响输出格式的参数。这个程序只用到了0-3位,16-18位,如果要在该程序的基础上增加参数,可以用其他没有使用的位。
    mode的第0位为1表示带l参数,mode的第1位为1表示带i参数,mode的第2位为1表示带R参数,mode的第16位为1表示带t参数,mode的第17位为1表示带a参数,mode的第18位为1表示带r参数*/
    int mode=0;
    char error_message[LENGTH]={0};/*错误信息*/
    char path[LENGTH]={0};/*路径*/
    struct stat stalist[MAX_FILE_COUNT];/*文件详细信息数组*/
    char  namelist[MAX_FILE_COUNT][LENGTH];/*文件名数组*/
    int flag=0;/*判断是否指定了目录*/
    int count;/*文件个数*/
    /*解析参数*/
    for(i=1;i<argc;i++){
        if(argv[i][0]=='-'){/*如果是选项参数*/
     for(j=1;j<strlen(argv[i]);j++){
  if(argv[i][j]=='l')
      mode=mode|LL;
  else if(argv[i][j]=='i')
      mode=mode|IL;
  else if(argv[i][j]=='R')
      mode=mode|RU;
  else if(argv[i][j]=='r')
      mode=mode|RL;
  else if(argv[i][j]=='a')
      mode=mode|AL;
  else if(argv[i][j]=='t')
      mode=mode|TL;
  else{
      snprintf(error_message,LENGTH,"no option of %c",argv[i][j]);
      error(error_message,__LINE__);
  }     
     }
 }else{/*参数为目录或文件*/
     if(flag==1)
  error("can not specify more then two dir or file",__LINE__);
     else
  flag=1;
            if(argv[i][0]!='/'){/*相对路径*/
  strcat(path,get_current_dir_name());
  strcat(path,"/");
  strncat(path,argv[i],strlen(argv[i]));
     }else/*绝对路径*/
  strcpy(path,argv[i]);
 }
    }
    if(flag==0)/*未指定任何目录或文件,则使用默认当前目录*/
 strcat(path,get_current_dir_name());
    /*根据mode获取指定目录下的文件*/
    count=file_list(path,stalist,namelist,mode);

    /*根据mode显示文件*/
    display(stalist,namelist,count,mode);
    return 0;
}
/*获取文件列表*/
int file_list(char* path,struct stat* stalist,char (*namelist)[LENGTH],int mode)
{
    int i=0,index,j,k;
    DIR *dir;
    struct dirent *ptr;
    char str[LENGTH];
    char nametemp[LENGTH];
    struct stat statemp;
    if(stat(path,&stalist[0])<0){
// puts(path);
 error("the specify file is not exist",__LINE__);
    }
    if(S_ISDIR(stalist[0].st_mode)){/*如果该路径指向的是一个目录*/
        if((dir=opendir(path))==NULL){/*打开目录*/
     sprintf(str,"can not open %s",path);
     error(str,__LINE__);
 }
        chdir(path);/*改变当前工作目录到path*/
        i=0;
 while((ptr=readdir(dir))!=NULL){/*依次读取目录下的所有文件*/
     if(ptr->d_name[0]=='.' && !(mode & AL))/*忽略隐藏文件*/
  continue;
     /*按字母顺序插入到stalist数组及namelist数组*/
     for(j=i;j>0;j--){
  if(strcmp(ptr->d_name,namelist[j-1])>0)
      break;
     }
     for(k=i;k>j;k--){
  strcpy(namelist[k],namelist[k-1]);
  stalist[k]=stalist[k-1];
     }
            strcpy(namelist[j],ptr->d_name);
            stat(ptr->d_name,&stalist[j]);
     i++;
     if(i==MAX_FILE_COUNT)
  error("file count beyond MAX_FILE_COUNT",__LINE__);
 }
 closedir(dir);
    }else{
 j=strlen(path)-1;
 while(j>=0 && path[j]!='/'){
     j--;
 }
 if(j<0)
     error("path error",__LINE__);
 j++;
 k=0;
        while(path[j]){
     namelist[0][k]=path[j];
            j++;
      k++;
 }
        namelist[0][k]=0;
        return 1;
    }
    if(mode & TL){/*按文件修改时间排序*/
 for(j=0;j<i;j++){
     index=j;
     for(k=j+1;k<i;k++)
  if(stalist[index].st_mtime<stalist[k].st_mtime){
      index=k;
  }
     statemp=stalist[j];
     stalist[j]=stalist[index];
     stalist[index]=statemp;
     strcpy(nametemp,namelist[j]);
     strcpy(namelist[j],namelist[index]);
     strcpy(namelist[index],nametemp);
 }
    }
    if(mode & RL){/*反序排列*/
 for(j=0;j<i/2;j++){
     strcpy(nametemp,namelist[j]);
     strcpy(namelist[j],namelist[i-j-1]);
     strcpy(namelist[i-j-1],nametemp);
     statemp=stalist[j];
     stalist[j]=stalist[i-j-1];
     stalist[i-j-1]=statemp;
 }
    }
    return i;
}
/*根据mode选择不同的函数进行显示*/
void display(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode)
{
    /*使mode的高16位为0*/
    int m = mode & ((1<<16)-1);
    switch(m){
 case 0:
     display_simply(namelist,n);
     break;
 case 1:
     display_LL(stalist,namelist,n);
     break;
 case 2:
     display_IL(stalist,namelist,n);
     break;
 case 3:
     display_LL_IL(stalist,namelist,n);
     break;
 case 4:
     display_RU(stalist,namelist,n,mode);
     break;
 case 5:
     display_RU_LL(stalist,namelist,n,mode);
     break;
        case 6:
     display_RU_IL(stalist,namelist,n,mode);
     break;
 case 7:
     display_RU_IL_LL(stalist,namelist,n,mode);
     break;
    }
}
/*不带显示选项,简单显示文件名*/
void display_simply(char (*namelist)[LENGTH],int n)
{
    int i,maxlength=0,cols;
    struct winsize ts;
    for(i=0;i<n;i++)
 if(strlen(namelist[i])>maxlength)
     maxlength=strlen(namelist[i]);
    /*获取终端的大小*/
    ioctl(STDIN_FILENO,TIOCGWINSZ,&ts);
    cols=ts.ws_col;
    cols/=(maxlength+1);
    //lines=ts.ws_row;
    for(i=0;i<n;i++){
       if(i!=0 && i%cols==0)
    puts("");
       printf("%*s",-(maxlength+1),namelist[i]);
    }
    putchar('\n');
}
/*-l 参数,显示详细文件信息*/
void display_LL(struct stat * stalist,char (*namelist)[LENGTH],int n)
{
    int i,mode;
    char* str;
    for(i=0;i<n;i++){
        mode=stalist[i].st_mode;
        if(S_ISDIR(mode))
     printf("d");
 else
     printf("-");
 if(mode & (1<<8))
     printf("r");
 else
     printf("-");
        if(mode & (1<<7))
     printf("w");
 else
     printf("-");
        if(mode & (1<<6))
     printf("x");
 else
     printf("-");
        if(mode & (1<<5))
     printf("r");
 else
     printf("-");
        if(mode & (1<<4))
     printf("w");
 else
     printf("-");
        if(mode & (1<<3))
     printf("x");
 else
     printf("-");
        if(mode & (1<<2))
     printf("r");
 else
     printf("-");
        if(mode & (1<<1))
     printf("w");
 else
     printf("-");
        if(mode & (1<<0))
     printf("x");
 else
     printf("-");
 printf(" %-3d",stalist[i].st_nlink);
 printf(" %-6s",getpwuid(stalist[i].st_uid)->pw_name);
 printf(" %-6s",getgrgid(stalist[i].st_gid)->gr_name);
 printf(" %-10d",stalist[i].st_size);
 str=ctime(&stalist[i].st_mtime);
 str[strlen(str)-2]=0;
 printf(" %s",str);
 printf(" %s\n",namelist[i]);
    }
}
/*-i 参数,显示文件名及节点号*/
void display_IL(struct stat* stalist,char (*namelist)[LENGTH],int n)
{
    int i,maxlength=0,cols;
    struct winsize ts;
    for(i=0;i<n;i++)
 if(strlen(namelist[i])>maxlength)
     maxlength=strlen(namelist[i]);
    /*获取终端的大小*/
    ioctl(STDIN_FILENO,TIOCGWINSZ,&ts);
    cols=ts.ws_col;
    cols/=(maxlength+9);
    //lines=ts.ws_row;

    for(i=0;i<n;i++){
       if(i!=0 && i%cols==0)
    puts("");
       printf("%-8d ",stalist[i].st_ino);
       printf("%*s",-(maxlength+1),namelist[i]);
    }
    putchar('\n');  
}
/*-li 参数,显示详细文件信息和节点号*/
void display_LL_IL(struct stat * stalist,char (*namelist)[LENGTH],int n)
{
    int i,mode;
    char* str;
    for(i=0;i<n;i++){
        printf("%-8d ",stalist[i].st_ino);
        mode=stalist[i].st_mode;
        if(S_ISDIR(mode))
     printf("d");
 else
     printf("-");
 if(mode & (1<<8))
     printf("r");
 else
     printf("-");
        if(mode & (1<<7))
     printf("w");
 else
     printf("-");
        if(mode & (1<<6))
     printf("x");
 else
     printf("-");
        if(mode & (1<<5))
     printf("r");
 else
     printf("-");
        if(mode & (1<<4))
     printf("w");
 else
     printf("-");
        if(mode & (1<<3))
     printf("x");
 else
     printf("-");
        if(mode & (1<<2))
     printf("r");
 else
     printf("-");
        if(mode & (1<<1))
     printf("w");
 else
     printf("-");
        if(mode & (1<<0))
     printf("x");
 else
     printf("-");
 printf(" %-3d",stalist[i].st_nlink);
 printf(" %-6s",getpwuid(stalist[i].st_uid)->pw_name);
 printf(" %-6s",getgrgid(stalist[i].st_gid)->gr_name);
 printf(" %-10d",stalist[i].st_size);
 str=ctime(&stalist[i].st_mtime);
 str[strlen(str)-2]=0;
 printf(" %s",str);
 printf(" %s\n",namelist[i]);

    }
}
/*-R 参数,简单显示所有文件,包括目录下面的子目录*/
void display_RU(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode)
{
    int i,count;
    char path[LENGTH]={0},temp[LENGTH]={0};
    struct stat sta[MAX_FILE_COUNT];
    char name[MAX_FILE_COUNT][LENGTH];
    puts(get_current_dir_name());
    display_simply(namelist,n);
    putchar('\n');
    strcpy(path,get_current_dir_name());

    for(i=0;i<n;i++){
        if(strcmp(namelist[i],".")==0 || strcmp(namelist[i],"..")==0)
          continue;
 if(S_ISDIR(stalist[i].st_mode)){
     strcpy(temp,path);
     strcat(path,"/");
     strcat(path,namelist[i]);
     count=file_list(path,sta,name,mode);
     display_RU(sta,name,count,mode);
            strcpy(path,temp);
 }
    }
}
/*-Rl 参数,显示所有文件,包括目录下面的子目录的详细信息*/
void display_RU_LL(struct stat * stalist,char (*namelist)[LENGTH],int n,int mode)
{
    int i,count;
    char path[LENGTH]={0},temp[LENGTH]={0};
    struct stat sta[MAX_FILE_COUNT];
    char name[MAX_FILE_COUNT][LENGTH];
    puts(get_current_dir_name());
    display_LL(stalist,namelist,n);
    putchar('\n');
    strcpy(path,get_current_dir_name());

    for(i=0;i<n;i++){
 if(strcmp(namelist[i],".")==0 || strcmp(namelist[i],"..")==0)
          continue;
 if(S_ISDIR(stalist[i].st_mode)){
     strcpy(temp,path);
     strcat(path,"/");
     strcat(path,namelist[i]);
     count=file_list(path,sta,name,mode);
     display_RU_LL(sta,name,count,mode);
     strcpy(path,temp);
 }
    }
}
/*-Ri 参数,简单显示所有文件,包括目录下的子目录,及节点号*/
void display_RU_IL(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode)
{
    int i,count;
    char path[LENGTH]={0},temp[LENGTH]={0};
    struct stat sta[MAX_FILE_COUNT];
    char name[MAX_FILE_COUNT][LENGTH];
    puts(get_current_dir_name());
    display_IL(stalist,namelist,n);
    putchar('\n');
    strcpy(path,get_current_dir_name());

    for(i=0;i<n;i++){
 if(strcmp(namelist[i],".")==0 || strcmp(namelist[i],"..")==0)
          continue;
 if(S_ISDIR(stalist[i].st_mode)){
     strcpy(temp,path);
     strcat(path,"/");
     strcat(path,namelist[i]);
     count=file_list(path,sta,name,mode);
     display_RU_IL(sta,name,count,mode);
     strcpy(path,temp);
 }
    }
}
/*-Ril 参数,显示所有文件,包括目录下面的子目录的详细信息,及节点号*/
void display_RU_IL_LL(struct stat * stalist,char (*namelist)[LENGTH],int n,int mode)
{
    int i,count;
    char path[LENGTH]={0},temp[LENGTH]={0};
    struct stat sta[MAX_FILE_COUNT];
    char name[MAX_FILE_COUNT][LENGTH];
    puts(get_current_dir_name());
    display_LL_IL(stalist,namelist,n);
    putchar('\n');
    strcpy(path,get_current_dir_name());

    for(i=0;i<n;i++){
 if(strcmp(namelist[i],".")==0 || strcmp(namelist[i],"..")==0)
          continue;
 if(S_ISDIR(stalist[i].st_mode)){
     strcpy(temp,path);
     strcat(path,"/");
     strcat(path,namelist[i]);
     count=file_list(path,sta,name,mode);
     display_RU_IL_LL(sta,name,count,mode);
     strcpy(path,temp);
 }
    }
}
//错误处理函数
void error(char* error_message,int line)
{
    char str[5];
    sprintf(str,"%d:",line);
    strcat(str,error_message);
    perror(str);
    exit(EXIT_FAILURE);
}
[!--infotagslink--]

相关文章

  • c++中的system("pause")的作用和含义解析

    这篇文章主要介绍了c++中system("pause")的作用和含义,非常不错,具有参考借鉴价值,需要的朋友参考下吧...2020-04-25
  • 基于BootStrap Metronic开发框架经验小结【八】框架功能总体界面介绍

    这篇文章主要介绍了基于BootStrap Metronic开发框架经验小结【八】框架功能总体界面介绍 的相关资料,需要的朋友可以参考下...2016-05-14
  • 在linux中使用包管理器安装node.js

    网上文章中,在linux下安装node.js都是使用源码编译,其实node的github上已经提供了各个系统下使用各自的包管理器(package manager)安装node.js的方法。...2015-03-15
  • Linux中grep命令详解

    grep命令是Linux系统中最重要的命令之一,功能是从文本文件或管道数据流中筛选匹配的行和数据,如果再配合正则表达式,功能十分强大,是Linux运维人员必备的命令,这篇文章主要介绍了Linux中grep详解,需要的朋友可以参考下...2023-02-15
  • C# 16 进制字符串转 int的方法

    这篇文章主要介绍了C# 16 进制字符串转 int的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • Linux安装Pytorch1.8GPU(CUDA11.1)的实现

    这篇文章主要介绍了Linux安装Pytorch1.8GPU(CUDA11.1)的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-25
  • InterlliJ IDEA2020新建java web项目找不到Static Web的解决

    这篇文章主要介绍了InterlliJ IDEA2020新建java web项目找不到Static Web的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-02
  • C#类中static变量用法分析

    这篇文章主要介绍了C#类中static变量用法,实例分析了static变量使用技巧与相关注意事项,需要的朋友可以参考下...2020-06-25
  • 基于Ionic3实现选项卡切换并重新加载echarts

    这篇文章主要介绍了基于Ionic3实现选项卡切换并重新加载echarts,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-24
  • C# WinForm快捷键设置技巧

    这篇文章主要给大家介绍C# winform快捷键设置技巧,涉及到C winform快捷键相关知识,对C winform知识感兴趣的朋友可以参考下本篇文章...2020-06-25
  • C#判断一个字符串是否是数字或者含有某个数字的方法

    这篇文章主要介绍了C#判断一个字符串是否是数字或者含有某个数字的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • Linux 下使用shell脚本定时维护数据库的案例

    这篇文章主要介绍了Linux 下使用shell脚本定时维护数据库,本文通过案例分析给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-07-11
  • linux服务器快速卸载安装node环境(简单上手)

    这篇文章主要介绍了linux服务器快速卸载安装node环境(简单上手),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-22
  • 详解Linux环境变量配置全攻略

    这篇文章主要介绍了Linux环境变量配置全攻略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-03-31
  • 详解C#切换窗口

    最近项目不多忙,于是抽点时间巩固下切换窗口问题,感兴趣的朋友跟着小编一起学习吧...2020-06-25
  • 基于BootStrap Metronic开发框架经验小结【六】对话框及提示框的处理和优化

    这篇文章主要介绍了基于BootStrap Metronic开发框架经验小结【六】对话框及提示框的处理和优化的相关知识,主要对比说明在Bootstrap开发中用到的这些技术要点,对此文感兴趣的朋友一起学习吧...2016-05-14
  • pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率

    今天小编就为大家分享一篇pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-05-02
  • PC蓝牙通信C#代码实现

    这篇文章主要为大家详细介绍了PC蓝牙通信C#代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • C#实现带进度条的ListView

    这篇文章主要介绍了C#实现带进度条的ListView 的相关资料,需要的朋友可以参考下...2020-06-25
  • SpringMVC文件上传原理及实现过程解析

    这篇文章主要介绍了SpringMVC文件上传原理及实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-15