《UNIX环境高级编程》读书笔记之文件与目录(2)
来源:互联网 发布:阿里云计算认证考试 编辑:程序博客网 时间:2024/06/04 18:14
动手练习:
(1)自己实现ls命令
#include <dirent.h>#include <string.h>int ls(int argc,char * argv[]){ int i; for(i = 2;i < argc;i++) { DIR * dp; struct dirent *dirp; if((dp = opendir(argv[i])) == NULL) err_sys("can't open %s\n",argv[i]); else printf("%s:\n",argv[i]); while((dirp = readdir(dp)) != NULL) { if(dirp->d_name[0] != '.') printf("%s\t",dirp->d_name); } printf("\n"); closedir(dp); } return 0;}</span>
(2)实现简单的stat语句
#include "apue.h"#include <unistd.h>#include <time.h>int get_stat(int argc,char * argv[]){ struct stat buf; char * type; off_t size; blkcnt_t blkcount; blksize_t blksize; dev_t dev; dev_t rdev; ino_t ino; nlink_t nlink; uid_t uid; gid_t gid; time_t a_time; time_t m_time; time_t c_time; char user[4]; char group[4]; char other[4]; int i; for(i = 2;i < argc;i++) { if(lstat(argv[i],&buf) < 0){ printf("lstat error\n"); return -1; } if(S_ISREG(buf.st_mode)) type = "regular"; else if(S_ISDIR(buf.st_mode)) type = "directory"; else if(S_ISCHR(buf.st_mode)) type = "character special"; else if(S_ISBLK(buf.st_mode)) type = "block special"; else if(S_ISFIFO(buf.st_mode)) type = "fifo"; else if(S_ISLNK(buf.st_mode)) type = "symbolic link"; else if(S_ISSOCK(buf.st_mode)) type = "socket"; size = buf.st_size; blkcount = buf.st_blocks; blksize = buf.st_blksize; dev = buf.st_dev; rdev = buf.st_rdev; ino = buf.st_ino; nlink = buf.st_nlink; uid = buf.st_uid; gid = buf.st_gid; a_time = buf.st_atime; m_time = buf.st_mtime; c_time = buf.st_ctime; if(buf.st_mode & S_IRUSR) user[0] = 'r'; else user[0] = '-'; if(buf.st_mode & S_IWUSR) user[1] = 'w'; else user[1] = '-'; if(buf.st_mode & S_IXUSR) user[2] = 'x'; else user[2] = '-'; if(buf.st_mode & S_IRGRP) group[0] = 'r'; else group[0] = '-'; if(buf.st_mode & S_IWGRP) group[1] = 'w'; else group[1] = '-'; if(buf.st_mode & S_IXGRP) group[2] = 'x'; else group[2] = '-'; if(buf.st_mode & S_IROTH) other[0] = 'r'; else other[0] = '-'; if(buf.st_mode & S_IWOTH) other[1] = 'w'; else other[1] = '-'; if(buf.st_mode & S_IXOTH) other[2] = 'x'; else other[2] = '-'; user[3] = '\0'; group[3] = '\0'; other[3] = '\0'; printf("File:%s\n",argv[i]); printf("Size:%d\n",size); printf("Blocks:%d\n",blkcount); printf("blksize:%d\n",blksize); printf("%s\n",type); printf("Device:%d\\%d\n",major(dev),minor(dev)); printf("Inode:%d\n",ino); printf("Links:%d\n",nlink); printf("Access:%s%s%s\n",user,group,other); printf("Userid:%d\n",uid); printf("Groupid:%d\n",gid); printf("Access Time:%s",ctime(&a_time)); printf("Modify Time:%s",ctime(&m_time)); printf("Change Time:%s",ctime(&c_time)); }}
程序截图:
0 0
- 《UNIX环境高级编程》读书笔记之文件与目录(2)
- 《UNIX高级环境编程》读书笔记之文件与目录(3)
- 《UNIX环境高级编程》读书笔记之文件IO(2)
- 文件和目录(二)--unix环境高级编程读书笔记
- 《UNIX环境高级编程》读书笔记之文件和目录(1)
- 《UNIX环境高级编程》读书笔记之文件IO(1)
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《Unix环境高级编程》读书笔记:文件和目录
- 《UNIX环境高级编程》读书笔记之信号(2)
- 《UNIX环境高级编程》读书笔记之线程(2)
- 《unix环境高级编程》 读书笔记 目录
- UNIX环境高级编程读书笔记 第一部分 文件 (第四章 文件和目录)
- Unix环境高级编程-文件和目录(2)
- 《unix环境高级编程》 读书笔记 (2)
- UNIX环境高级编程读书笔记(2)
- UNIX环境高级编程之学习笔记(四)-文件和目录(2)
- unix环境高级编程 chap4 文件与目录
- 《Unix环境高级编程》之 文件和目录
- App模块化及工程扩展
- cocos2d-x v3.2 FlappyBird 各个类对象具体代码分析(2)
- try_module_get和module_put
- Flex更改Alert提示框的字体大小和样式
- UITextField控件之ValueChanged事件
- 《UNIX环境高级编程》读书笔记之文件与目录(2)
- HDU-1548 A strange lift
- [解决]Xcode6模拟器虚拟键盘不显示
- Web 应用层分类和识别
- typedef和#define
- HTTP使用BASIC认证的原理及实现方法
- 今年暑假不AC(贪心--区间调度)
- GARCH(二)
- Looper、MessageQueue、Handler各自的作用总结