操作系统第4次实验报告:文件系统
- 姓名:蒋浩天
- 学号:201821121024
- 班级:计算1811
1. 编写程序
在服务器上用Vim编写一个程序:实现Linux系统命令ls -lai
的功能,给出源代码。
#include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<sys/types.h> 5 #include<sys/stat.h> 6 #include<time.h> 7 #include<unistd.h> 8 #include<pwd.h> 9 #include<grp.h> 10 #include<dirent.h> 11 #include<fcntl.h> 12 #define Max_size 1024 13 void file_type(const struct stat file_stat){ 14 if(S_ISREG(file_stat.st_mode)) 15 printf("-"); 16 else if(S_ISDIR(file_stat.st_mode)){ 17 printf("d"); 18 } 19 else if(S_ISCHR(file_stat.st_mode)){ 20 printf("c"); 21 } 22 else if(S_ISBLK(file_stat.st_mode)){ 23 printf("b"); } 25 else if(S_ISFIFO(file_stat.st_mode)){ 26 printf("p"); 27 } 28 else if(S_ISLNK(file_stat.st_mode)){ 29 printf("l"); 30 } 31 else if(S_ISSOCK(file_stat.st_mode)){ 32 printf("s"); 33 } 34 } 35 36 void file_right(struct stat file_stat){ 37 printf("%c",file_stat.st_mode&S_IRUSR?'r':'-'); 38 printf("%c",file_stat.st_mode&S_IWUSR?'w':'-'); 39 printf("%c",file_stat.st_mode&S_IXUSR?'x':'-'); 40 printf("%c",file_stat.st_mode&S_IRGRP?'r':'-'); 41 printf("%c",file_stat.st_mode&S_IWGRP?'w':'-'); 42 printf("%c",file_stat.st_mode&S_IXGRP?'x':'-'); 43 printf("%c",file_stat.st_mode&S_IROTH?'r':'-'); 44 printf("%c",file_stat.st_mode&S_IWOTH?'w':'-'); 45 printf("%c",file_stat.st_mode&S_IXOTH?'x':'-'); 46 } int main(){ 50 char dir[Max_size]=""; 51 getcwd(dir,sizeof(dir)); 52 DIR * file=NULL; 53 struct dirent *dnt=NULL; 54 struct stat file_stat={0}; 55 struct passwd *pwd=NULL; 56 struct group *grp=NULL; 57 struct tm time={0}; 58 59 chdir(dir); 60 file=opendir(dir); 61 while((dnt=readdir(file))!=NULL){ 62 if(strncmp(dnt->d_name,".",1)){ 63 stat(dnt->d_name,&file_stat); 64 printf("%ld ",file_stat.st_ino);//索引号 65 file_type(file_stat);//文件类型 66 file_right(file_stat);//文件权限 67 printf(" %ld ",file_stat.st_nlink);//文件硬链接数 68 69 pwd=getpwuid(file_stat.st_uid);//文件拥有者 70 printf("%s ",pwd->pw_name); 71 72 grp=getgrgid(file_stat.st_gid);//文件拥有者所在组 73 printf("%s ",grp->gr_name); 74 75 printf("%5ld ",file_stat.st_size);//文件大小 76 77 localtime_r(&(file_stat.st_mtime),&time);//文件最近修改时间 78 printf("%d ",time.tm_mon+1); 79 printf("%4d ",time.tm_mday); 80 printf("%02d:",time.tm_hour); 81 printf("%02d ",time.tm_min); 82 83 printf("%s\n",dnt->d_name);//文件名 84 } 85 } 86 closedir(file); 87 return 0; 88 }
2. 分析运行结果
给出运行结果截图,对于每一列是如何获取的,结合源代码做解释
运行结果:
第一列:索引号
64 printf("%ld ",file_stat.st_ino);//索引号
第二列:文件的类型和权限
类型:
14 if(S_ISREG(file_stat.st_mode)) 15 printf("-"); 16 else if(S_ISDIR(file_stat.st_mode)){ 17 printf("d"); 18 } 19 else if(S_ISCHR(file_stat.st_mode)){ 20 printf("c"); 21 } 22 else if(S_ISBLK(file_stat.st_mode)){ 23 printf("b"); } 25 else if(S_ISFIFO(file_stat.st_mode)){ 26 printf("p"); 27 } 28 else if(S_ISLNK(file_stat.st_mode)){ 29 printf("l"); 30 } 31 else if(S_ISSOCK(file_stat.st_mode)){ 32 printf("s"); 33 }
文件权限:
36 void file_right(struct stat file_stat){ 37 printf("%c",file_stat.st_mode&S_IRUSR?'r':'-'); 38 printf("%c",file_stat.st_mode&S_IWUSR?'w':'-'); 39 printf("%c",file_stat.st_mode&S_IXUSR?'x':'-'); 40 printf("%c",file_stat.st_mode&S_IRGRP?'r':'-'); 41 printf("%c",file_stat.st_mode&S_IWGRP?'w':'-'); 42 printf("%c",file_stat.st_mode&S_IXGRP?'x':'-'); 43 printf("%c",file_stat.st_mode&S_IROTH?'r':'-'); 44 printf("%c",file_stat.st_mode&S_IWOTH?'w':'-'); 45 printf("%c",file_stat.st_mode&S_IXOTH?'x':'-'); 46 }
第三列:文件的硬链接数
67 printf(" %ld ",file_stat.st_nlink);//文件硬链接数
第四列:文件拥有者
69 pwd=getpwuid(file_stat.st_uid);//文件拥有者 70 printf("%s ",pwd->pw_name);
第五列:文件拥有者所在组
72 grp=getgrgid(file_stat.st_gid);//文件拥有者所在组 73 printf("%s ",grp->gr_name);
第六列:文件大小
75 printf("%5ld ",file_stat.st_size);//文件大小
第七列:文件最近修改时间
77 localtime_r(&(file_stat.st_mtime),&time);//文件最近修改时间 78 printf("%d ",time.tm_mon+1); 79 printf("%4d ",time.tm_mday); 80 printf("%02d:",time.tm_hour); 81 printf("%02d ",time.tm_min);
第八列:文件名
83 printf("%s\n",dnt->d_name);//文件名
3. 通过该实验产生新的疑问及解答
在刚做实验时,感觉题目有难度,无从下手。但后来发现只要按照老师给的ppt中的思路来,是可以进行的。解决问题的重点在:查询ls -lai指令显示出的各列的含义,查找相应的实现函数。结合实验实际合理使用,就可以做出来实验。
在做完实验后,我发现ls -lai产生的内容中,文件的最近修改日期中的月份是英文简写,而我的是数字。解决方法:可以加入判断语句,根据月份的数字,输出相应的月份英文简写。