当前位置:首页 > 科技 > 正文内容

linux模拟ls命令代码(linux甯哥敤鍛戒护ls-l)

2019-05-28 20:18:08科技584

大家好,今天就和大牛一起来看看这个问题吧 。linux甯哥敤鍛戒护ls-l,linux模拟ls命令代码很多人还不知道,现在让我们一起来看看吧!

linux模拟ls命令代码代码如下:

linux模拟ls命令代码(linux甯哥敤鍛戒护ls-l)

/*

模拟限位开关(限位开关)命令,实现参数-塔里尔。有些代码重复出现,可改进

可以在该程序的基础上增加参数,该程序思路清晰,容易扩展

*/

#包括

#包括

#包括

#包括

#包括

#包括

#包括

#包括

#包括

#包括

#定义LL (10) /*带l参数标志*/

#定义白细胞介素11) /*带我参数标志*/

#定义RU (12) /*带稀有参数标志*/

#定义TL (116) /*带t参数标志*/

#定义铝(117) /*带a参数标志*/

#定义RL (118) /*带r参数标志*/

#定义长度500 /*字符串最大长度*/

#定义最大文件计数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(* name list)[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;

/*模式的前16个位用来标志那些能影响显示的参数,16位之后的位用来标志不影响输出格式的参数。这个程序只用到了0-3位,16-18位,如果要在该程序的基础上增加参数,可以用其他没有使用的位。

方式的第0位为一表示带l参数,模式的第一位为一表示带我参数,模式的第2位为一表示带稀有参数,模式的第16位为一表示带t参数,模式的第17位为一表示带a参数,模式的第18位为一表示带r参数*/

int mode=0;

char error _ message[LENGTH]={ 0 };/*错误信息*/

char path[LENGTH]={ 0 };/*路径*/

struct stat stalist[MAX _ FILE _ COUNT];/*文件详细信息数组*/

字符名称列表[MAX _ FILE _ COUNT][LENGTH];/*文件名数组*/

int flag=0;/*判断是否指定了目录*/

(同Internationalorganizations)国际组织计数;/*文件个数*/

/*解析参数*/

for(I=1;我

if(argv[i][0]=='

  for(j=1;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

  index=j;

  for(k=j+1;k

  if(stalist[index].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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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);

  }

  看了“linux模拟ls命令代码”还想看:

1.ls命令怎么使用

2.Linux常用指令大全

3.解决ftp的dir或ls命令无法查看文件列表的办法

4.怎么实现ls命令在win7下正常运行

这篇文章到此就结束,希望能帮助到大家。

扫描二维码推送至手机访问。

版权声明:文章内容摘自网络,如果无意之中侵犯了您的版权,请联系本站,本站将在3个工作日内删除。谢谢!

本文链接:https://www.xixia168.com/n/kj/4017.html

标签: 命令代码
分享给朋友:

“linux模拟ls命令代码(linux甯哥敤鍛戒护ls-l)” 的相关文章

10月有哪些节日全部(十月份节日有哪些)

10月有哪些节日全部(十月份节日有哪些)

大家好,今天就和小白一起来看看这个问题吧 。十月份节日有哪些,10月有哪些节日全部很多人还不知道,现在让我们一起来看看吧!  一、十月份节日有哪些  10月1日:“十.一”国庆节、国际音乐节、国际老年人日  10月2日:国际和平与民主自由斗争日、国际非暴力日  10月4日:世界动物日  10月4-1...

不惑之年是指哪个年龄(人不惑之年是指多少岁)

不惑之年是指哪个年龄(人不惑之年是指多少岁)

大家好,今天就和小鱼一起来看看这个问题吧 。人不惑之年是指多少岁,不惑之年是指哪个年龄很多人还不知道,现在让我们一起来看看吧!  一、  人不惑之年是指多少岁  不惑之年指的是四十岁,意思是遇事能明辨不疑。因为其源于孔子所说的“吾十有五而志于学,三十而立,四十而不惑,五十而知天命,六十而耳顺,七十而...

重阳节登高的原因有哪些(重阳节为什么要登高)

重阳节登高的原因有哪些(重阳节为什么要登高)

大家好,今天就和小白一起来看看这个问题吧 。重阳节为什么要登高,重阳节登高的原因有哪些很多人还不知道,现在让我们一起来看看吧!  一、  重阳节为什么要登高  重阳节登高是传统习俗,有登高辞青的说法。  重阳节登高辞青和清明节上山踏青具有呼应作用,重阳节时正值深秋,万木萧瑟、气候凉爽,这个季节登高远...

端午节挂艾草有什么寓意(端午节为什么要挂艾草)

端午节挂艾草有什么寓意(端午节为什么要挂艾草)

大家好,今天就和蜜蜜一起来看看这个问题吧 。端午节为什么要挂艾草,端午节挂艾草有什么寓意很多人还不知道,现在让我们一起来看看吧!    端午节为什么要挂艾草  古人认为,端午节一到,夏季炎热天气就到了,天气炎热,自然界的各种飞虫走兽开始兴盛,包括一些有毒的小动物,比如毒蛇、蟾蜍、蝎子、蜈蚣等也会活跃...

植树节的由来简说(植树节的来历简写50字)

植树节的由来简说(植树节的来历简写50字)

大家好,今天就和兔兔一起来看看这个问题吧 。植树节的来历简写50字,植树节的由来简说很多人还不知道,现在让我们一起来看看吧!    植树节的来历简写50字:植树节是为了纪念中国民主革命先行者孙中山先生。他是中国近代史上 第一个重视并大力提倡植树造林的人。习俗就是在这一天植树造林,美化环境的活动。一、...

八月十五由来的意义是什么(八月十五的来历)

八月十五由来的意义是什么(八月十五的来历)

大家好,今天就和小鱼一起来看看这个问题吧 。八月十五的来历,八月十五由来的意义是什么很多人还不知道,现在让我们一起来看看吧!  一、  八月十五的来历  来历一:由古代祭月慢慢演变而来,我国古代历法,把每个季节分为三个月,分别是孟月、仲月、季月,而农历八月份正好是秋季的第二个月,所以被称为 “仲秋”...