操作系统第4次实验报告:文件系统
- 姓名:杨富宏
- 学号:201821121017
- 班级:计算1811
1. 编写程序
1#include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<sys/types.h> 5 #include<dirent.h> 6 #include<sys/stat.h> 7 #include<time.h> 8 #include<unistd.h> 9 #include<errno.h> 10 #include<limits.h> 11 #include<grp.h> 12 #include<pwd.h> 13 #include<assert.h> 14 15 16 void do_ls(char []); //目录下文件 17 void dostat(char *); 18 void show_file_info(char *, struct stat *); 19 void mode_to_letters(int mode, char str[]); 20 char *st_uid(uid_t uid); 21 char *st_gid(gid_t gid); 22 23 void do_ls(char dirname[]) //目录下的文件 24 { 25 DIR *dir_ptr; 26 struct dirent *direntp; 27 28 if ((dir_ptr = opendir(dirname)) == NULL) 29 { 30 fprintf(stderr, "ls2: cannot open %s\n", dirname); 31 } 32 else 33 { 34 while((direntp = readdir(dir_ptr)) != NULL) 35 { 36 dostat(direntp->d_name); 37 } 38 closedir(dir_ptr); 39 } 40 } 41 42 43 void dostat(char *filename) // 44 { 45 struct stat info; 46 if (stat(filename, &info) == -1) 47 { 48 perror(filename); 49 } 50 else 51 { 52 show_file_info(filename, &info); 53 } 54 } 55 56 57 void mode_to_letters(int mode, char str[]) //输出文件的详细信息 58 { 59 strcpy(str, "-----------"); 60 if (S_ISDIR(mode)) 61 str[0] = 'd'; 62 if (S_ISCHR(mode)) 63 str[0] = 'c'; 64 if (S_ISBLK(mode)) 65 str[0] = 'b'; 66 67 if (mode & S_IRUSR) 68 str[1] = 'r'; 69 if (mode & S_IWUSR) 70 str[2] = 'w'; 71 if (mode & S_IXUSR) 72 str[3] = 'x'; 73 if (mode & S_IRGRP) 74 str[4] = 'r'; 75 if (mode & S_IWGRP) 76 str[5] = 'w'; 77 if (mode & S_IXGRP) 78 str[6] = 'x'; 79 if (mode & S_IROTH) 80 str[7] = 'r'; 81 if (mode & S_IWOTH) 82 str[8] = 'w'; 83 if (mode & S_IXOTH) 84 str[9] = 'x'; 85 } 86 87 char *st_uid(uid_t uid) //文件属主的UID号 88 { 89 struct passwd * getpwuid(), *pw_ptr; 90 static char numstr[10]; 91 92 if ((pw_ptr = getpwuid(uid)) == NULL) 93 { 94 sprintf(numstr, "%d", uid); 95 return numstr; 96 } 97 else 98 { 99 return pw_ptr->pw_name; 100 } 101 } 102 103 char *st_gid(gid_t gid) //文件属主的GID号 104 { 105 struct group *getgrgid(), *grp_ptr; 106 static char numstr[10]; 107 108 if ((grp_ptr = getgrgid(gid)) == NULL) 109 { 110 sprintf(numstr, "%d", gid); 111 return numstr; 112 } 113 else 114 { 115 return grp_ptr->gr_name; 116 } 117 } 118 119 void show_file_info(char *filename, struct stat *info_p) //输出顺序 120 { 121 char *st_uid(), *ctime(), *st_gid(); 122 void mode_to_letters(); 123 char modestr[11]; 124 mode_to_letters(info_p->st_mode, modestr); 125 printf("%d ",(int)info_p->st_ino); //索引号 126 printf("%s",modestr); //文件类型 127 printf("%4d ",(int)info_p->st_nlink); //该文件上硬连接的> 个数 128 printf("%-8s ", st_uid(info_p->st_uid)); //文件属主的UID 129 printf("%-8s ", st_gid(info_p->st_gid)); //文件属主的GID 130 printf("%8ld ", (long)info_p->st_size); //文件的大小 131 printf("%.12s ", 4 + ctime(&info_p->st_mtime)); //上一次被修改的时> 间 132 printf("%s\n", filename); //文件名 133 } 134 135 int main(int make, char **str) //主函数 136 { 137 if (make == 1) 138 do_ls("."); 139 else 140 { 141 while (--make) 142 { 143 printf("%s:\n", *++str); 144 do_ls(*str); 145 } 146 } 147 return 0; 148 }
2. 分析运行结果
本题的思路在实验报告中有提到:
获取当前工作目录路径 打开目录 读取目录文件 ;
对每一个文件,读取文件属性,解释文件属性,格式化输出。
运行结果截图:
执行 ls -lai:
执行结果分析:
这次实验显示的内容有八个属性。
1、第一列是文件的索引号,这边我们利用stat中的st_ino来得到。
2、第二列是文件的权限,第一位表示文件的类型,后面九位代表权限。
3、第三列是指该文件上硬件连接的个数。
4、第四列文件属主的UID号。
5、第五列是文件属主的GID号。
6、第六列是文件的大小。
7、第七列是该文件内容最近一次修改的日期。
8、第八列是文件名。
3. 通过该实验产生新的疑问及解答
问题:自己的代码编译出来后的显示是没有像ls -lai一样有颜色的。
查阅资料后发现可以利用mode显示模式来设置颜色或者下划线。
参考文献: