《unix/linux编程实践教程》学习笔记:第三章 编写ls
2017-03-03 22:24
239 查看
概念准备
目录是一种特殊的文件,它的内容是文件和目录的名字,每一个目录都包括两个特殊的项——“.”(当前目录文件列表)和“..”(上一级目录文件列表)。可以通过命令显示隐藏的文件
显示出来,也可以通过
编写ls的思路就是读取
如何读取目录的内容?
学习三个系统调用:opendir,readdir,closedir
编写ls
编写ls -l
(1)ls - l 可以查看文件的属性和信息,要实现它的功能,我们需要学习系统调用stat
stat使用例子
(2)模式字段转换为字符
使用掩码解码,例如:
if(mode & S_IRUSR) str[1] = ‘r’;.
设置和修改文件属性
chmod:修改文件的许可权限和特殊属性
chown:修改文件所有者和组
utime:修改文件的最后访问时间和最后修改时间
rename:修改文件名或移动文件的位置
目录是一种特殊的文件,它的内容是文件和目录的名字,每一个目录都包括两个特殊的项——“.”(当前目录文件列表)和“..”(上一级目录文件列表)。可以通过命令显示隐藏的文件
ls -a
显示出来,也可以通过
vim .打开目录文件。
编写ls的思路就是读取
.文件,通过printf打印。
如何读取目录的内容?
学习三个系统调用:opendir,readdir,closedir
编写ls
#include <stdio.h> #include <sys/types.h> #include <dirent.h> void do_ls(char []); int main(int ac,char *av[]) { if(ac == 1) do_ls("."); else while(-- ac){ printf("%s:\n",*++av); do_ls(*av); } return 0; } void do_ls(char dirname[]){ DIR * dir_ptr; struct dirent * direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"lsl:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp -> d_name); closedir(dir_ptr); } }
编写ls -l
(1)ls - l 可以查看文件的属性和信息,要实现它的功能,我们需要学习系统调用stat
stat使用例子
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> int main(int ac, char *av[]) { struct stat info; if(ac > 1) if( stat(av[1],&info) != -1){ show_stat_info(av[1],&info); return 0; }else{ perror(av[1]); } return 1; } show_stat_info(char *fname, struct stat *buf) { printf(" mode: %o\n",buf->st_mode); printf(" links: %d\n",buf->st_nlink); printf(" user: %d\n",buf->st_uid); printf(" group: %d\n",buf->st_gid); printf(" size: %d\n",buf->st_size); printf(" modtime: %d\n",buf->st_mtime); printf(" name: %s\n",fname); }
(2)模式字段转换为字符
使用掩码解码,例如:
if(mode & S_IRUSR) str[1] = ‘r’;.
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <string.h> void do_ls(char []); void dostat(char *); void show_file_info(char *,struct stat *); void mode_to_letters(int,char []); char *uid_to_name(uid_t); char *gid_to_name(gid_t); int main(int ac,char *av[]) { if(ac == 1) do_ls("."); else while(-- ac){ printf("%s:\n",*++av); do_ls(*av); } return 0; } void do_ls(char dirname[]){ DIR * dir_ptr; struct dirent * direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"lsl:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) dostat(direntp->d_name); closedir(dir_ptr); } } void dostat(char *filename) { struct stat info; if(stat(filename,&info) == -1) perror(filename); else show_file_info(filename,&info); } void show_file_info(char * filename,struct stat * info_p) { char *uid_to_name(),*ctime(),*gid_to_name(),*filemode(); void mode_to_letters(); char modestr[11]; mode_to_letters(info_p->st_mode,modestr); printf("%s",modestr); printf("%4d",(int)info_p->st_nlink); printf("%-5s",uid_to_name(info_p->st_uid)); printf("%-5s",gid_to_name(info_p->st_gid)); printf("%5ld ",(long)info_p->st_size); printf("%.10s ",4+ctime(&info_p->st_mtime)); printf("%s\n",filename); } void mode_to_letters(int mode,char str[]) { strcpy(str,"------------"); if(S_ISDIR(mode)) str[0] = 'd'; if(S_ISCHR(mode)) str[0] = 'c'; if(S_ISBLK(mode)) str[0] = 'b'; if(mode & S_IRUSR) str[1] = 'r'; if(mode & S_IWUSR) str[2] = 'w'; if(mode & S_IXUSR) str[3] = 'x'; if(mode & S_IRGRP) str[4] = 'r'; if(mode & S_IWGRP) str[5] = 'w'; if(mode & S_IXGRP) str[6] = 'x'; if(mode & S_IROTH) str[7] = 'r'; if(mode & S_IWOTH) str[8] = 'w'; if(mode & S_IXOTH) str[9] = 'x'; } #include <pwd.h> char *uid_to_name( uid_t uid) { struct passwd * getpwuid(), *pw_ptr; static char numstr[10]; if((pw_ptr = getpwuid(uid)) == NULL){ sprintf(numstr,"%d",uid); return numstr; } else return pw_ptr->pw_name; } #include <grp.h> char *gid_to_name(gid_t gid) { struct group *getgrpid(),*grp_ptr; static char numstr[10]; if((grp_ptr = getgrgid(gid)) == NULL){ sprintf(numstr,"%d",gid); return numstr; } else return grp_ptr->gr_name; }
设置和修改文件属性
chmod:修改文件的许可权限和特殊属性
chown:修改文件所有者和组
utime:修改文件的最后访问时间和最后修改时间
rename:修改文件名或移动文件的位置
相关文章推荐
- linux下ls命令的编写
- 《Python学习手册》学习笔记(22)之第22章模块代码编写基础(关键词:编程语言/Python/模块/import/from)
- 《Windows核心编程》学习笔记– 如何编写Unicode代码
- 《Android进阶之光》学习笔记——第三章 View体系与自定义View
- UNIX-LINUX编程实践教程->第三章->实例代码注解->ls2
- S5PV210(TQ210)学习笔记——LCD驱动编写
- TensorFlow:实战Goole深度学习框架第三章学习笔记
- 第三章 制作Nine-Patch图片与编写精美聊天界面
- 《Android面试宝典》学习笔记(第三章:组件)
- S5PV210(TQ210)学习笔记——LCD驱动编写
- 《VC++深入详解》学习笔记 第三章 MFC框架程序剖析(未完成)
- 《JavaScript高级程序设计》学习笔记(第三章)- 上
- 《Windows核心编程》学习笔记 – 如何编写unicode代码续
- Linux C程序设计系列之 练习系统调用文件IO、内存映射程序 编写ls程序
- 《从零开始学Swift》学习笔记(Day4)——用Playground工具编写Swift
- 《unix/linux编程实践教程》学习笔记:第四章 文件系统:编写pwd
- Python编程入门-第三章 编写程序 -学习笔记
- 《Mongodb权威指南》学习笔记 第三章 创建、更新及删除文档(一)
- 编写一个SHELL脚本,可以获得指定目录下的所有文件及文件夹的大小。输出的样式与 ls -lh 命令相同。
- 第3章 目录与文件属性:编写 ls