码迷,mamicode.com
首页 > 其他好文 > 详细

操作系统第4次实验报告:文件系统

时间:2020-04-30 22:52:40      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:ring   文件   文件系统   符号   ESS   技术   lib   pen   mon   

  • 李微微
  • 201821121001
  • 计算1811

1. 编写程序

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

技术图片

 

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

#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
 65 //用于分析参数
 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]={0};
 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. 分析运行结果

运行结果截图:

技术图片

分析:

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

文件属性判断举例:

技术图片

 

操作系统第4次实验报告:文件系统

标签:ring   文件   文件系统   符号   ESS   技术   lib   pen   mon   

原文地址:https://www.cnblogs.com/caibanye/p/12811380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!