才半页

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  • 李微微
  • 201821121001
  • 计算1811

1. 编写程序

先使用系统的指令执行ls -lai:

 根据以上结果编写代码如下:

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<time.h>
  5 #include<unistd.h>
  6 #include<sys/types.h>
  7 #include<dirent.h>
  8 #include<grp.h>
  9 #include<pwd.h>
 10 #include<errno.h>
 11 #include<sys/stat.h>
 12 #include<limits.h>
 13 #include<assert.h>
 14
 15 void AnalPara(int argc,char *argv[],char *path);
 16 void ShowInfo(int mode,struct stat st);
 17 void ShowName(int mode,int uid,char *name);
 18
 19 int flag=0;
 20 int main(int argc,char *argv[]){
 21     char path[128]={0};
 22     getcwd(path,127);
 23     AnalPara(argc,argv,path);
 24     DIR *dir=opendir(path);
 25     if(dir==NULL){
 26         char *p=path+strlen(path);
 27         while(*p!='/')
 28             p--;
 29         p++;
 30         printf("ls:can not access %s:No such file or directory\n",p);
 31         exit(0);
 32     }
 33         struct dirent *dr = NULL;
 34          while((dr = readdir(dir)) != NULL){
 35              if(((flag&1)==0) && (strncmp(dr->d_name,".",1) == 0)){
36                  continue;
 37              }
 38              struct stat st;
 39              char temp[128] = {0};
 40              strcpy(temp,path);
 41              strcat(temp,"/");
 42              strcat(temp,dr->d_name);
 43              stat(temp,&st);
 44              if((flag&2)==2){
 45                  if((flag&4)==4){
 46                      printf("%ld ",st.st_ino);
 47                  }
 48                  ShowInfo(st.st_mode,st);
 49                  ShowName(st.st_mode,st.st_uid,dr->d_name);
 50                  printf("%s\n",dr->d_name);
 51                  continue;
 52              }
 53                  if((flag&4)==4){
 54                       printf("%ld ",dr->d_ino);
 55                       ShowName(st.st_mode,st.st_uid,dr->d_name);
 56                       continue;
 57                  }
 58                  ShowName(st.st_mode,st.st_uid,dr->d_name);
 59          }
 60          if(argc==1||(argc>1&&flag==0))
 61              printf("\n");
 62           closedir(dir);
 63 }
 64
 66 void AnalPara(int argc,char *argv[],char *path)
 67 {
 68     for(int i=1;i<argc;++i){
 69         if(strncmp(argv[i],"-",1)==0){
 70             if(strstr(argv[i],"a")!=NULL){
 71                 flag |=1<<0;
 72             }
 73             if(strstr(argv[i],"l")!=NULL){
 74                 flag |=1<<1;
 75             }
 76             if(strstr(argv[i],"i")!=NULL){
 77                 flag |=1<<2;
 78             }
 79         }else{
 80             if(strncmp(argv[i],"/",1)==0){
 81                 strcpy(path,argv[i]);
 82             }else{
 83                 strcat(path,argv[i]);
 84                 strcat(path,argv[i]);
 85             }
 86         }
 87     }
 88 }
 89 void ShowInfo(int mode,struct stat st){
 90     char str[10]={----------};
 91     if(S_ISDIR(mode))str[0]='d';
 92     if(S_ISCHR(mode))str[0]='c';
 93     if(S_ISBLK(mode))str[0]='b';
 94
 95     if(mode&S_IRUSR)str[1]='r';
 96     if(mode&S_IWGRP)str[2]='w';
 97     if(mode&S_IXUSR)str[3]='x';
 98
 99     if(mode&S_IRGRP)str[4]='r';
100     if(mode&S_IWGRP)str[5]='w';
101     if(mode&S_IXGRP)str[6]='x';
102                                                                                
103 if(mode&S_IROTH)str[7]='r'; 104 if(mode&S_IWOTH)str[8]='w'; 105 if(mode&S_IXOTH)str[9]='x'; 106
107 for(int i=0;i<10;i++){ 108 printf("%c",str[i]); 109 }
110 printf("."); 111 printf("%ld ",st.st_nlink); 112 struct passwd *pd = getpwuid(st.st_uid); 113 assert(pd != NULL);
114 printf("%4s ",pd->pw_name); 115 struct group *gp = getgrgid(st.st_gid); 116 assert(gp != NULL);
117 printf("%4s ",gp->gr_name); 118 printf("%4ld",st.st_size);
119 struct tm * lchangetime = localtime(&(st.st_mtime)); 120 printf("%d %d %d:%d ",(lchangetime->tm_mon+1),lchangetime->tm_mday,lchangetime->tm_hour,lchangetime->tm_min);
121 }
122 void ShowName(int mode,int uid,char *name){ 123 if(S_ISDIR(mode)){ 124 printf("\33[1;34m%s\033[0m ",name); 125 }else{ 126 printf("%s ",name); 127 } 128 }

2. 分析运行结果

运行结果截图:

分析:

思路:通过判断符号判断参数,通过判断参数觉得执行指令。

第一列:文件的索引编号,通过d_ino和st_ino获取并输出。

               

 第二列:文件的权限,在输出文件详细信息的函数里,用文件权限宏变量判断并输出。

               

 第三列:文件硬链接数,通过st_nlink获取并输出。

               

 第四列:主用户,在输出详细信息的函数里,通过st_uid获取并输出。

               

              

第五列:组用户,在输出详细信息的函数里,通过st_gid获取并输出。

               

第六列:文件所占大小,在输出详细信息的函数里,通过st_size获取并输出。

               

 第七列:最后修改时间,在输出详细信息的函数里,通过st_mtime获取并输出。

                

 第八列:文件名,在输出文件名函数里,用d_name获取,在主函数调用输出。

                

                

文件属性判断举例:

3. 参考网址

https://www.cnblogs.com/ghostwu/p/8253623.html(linux系统编程:自己动手写一个ls命令

posted on 2020-04-30 21:58  才半页  阅读(200)  评论(0编辑  收藏  举报