busybox源码剖析(3)---cat.c
来源:互联网 发布:百度指数数据来源 编辑:程序博客网 时间:2024/06/05 11:46
cat显示一个文件的内容。需要注意的是,cat并不能处理目录。
1 extern int cat_main(int argc, char **argv) 2 { 3 int status = EXIT_SUCCESS; 4 5 if (argc == 1) { 6 print_file(stdin); 7 return status; 8 } 9 10 while (--argc > 0) {11 if(!(strcmp(*++argv, "-"))) {12 print_file(stdin);13 } else if (print_file_by_name(*argv) == FALSE) {14 status = EXIT_FAILURE;15 }16 }17 return status;18 }
当输入cat或者是cat -时,都会调用print_file函数。
1 extern void print_file(FILE *file)2 {3 fflush(stdout);4 copyfd(fileno(file), fileno(stdout));//将file文件的内容拷贝到stdout5 fclose(file);6 }
显然,print_file(stdin)就是将stdin的内容拷贝到stdout。即输入什么,就输出什么。
V658tekiMacBook-Pro:busybox-0.60.3 neilhappy$ cat -input--stdininput--stdin
当cat filename时,进入print_file_by_name函数。
1 extern int print_file_by_name(char *filename) 2 { 3 struct stat statBuf; 4 int status = TRUE; 5 6 if(is_directory(filename, TRUE, &statBuf)==TRUE) { 7 error_msg("%s: Is directory", filename); 8 status = FALSE; 9 } else {10 FILE *f = wfopen(filename, "r");11 if(f!=NULL)12 print_file(f);13 else14 status = FALSE;15 }16 17 return status;18 }
is_directory判断是否是目录。如果不是目录,就用pring_file函数打印。要注意,这里的TRUE参数决定了只使用stat(不考虑符号链接)。
1 int is_directory(const char *fileName, const int followLinks, struct stat *statBuf) 2 { 3 int status; 4 int didMalloc = 0; 5 6 if (statBuf == NULL) { 7 statBuf = (struct stat *)xmalloc(sizeof(struct stat)); 8 ++didMalloc; 9 }10 11 if (followLinks == TRUE)12 status = stat(fileName, statBuf);13 else14 status = lstat(fileName, statBuf);15 16 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {17 status = FALSE;18 }19 else status = TRUE;20 21 if (didMalloc) {22 free(statBuf);23 statBuf = NULL;24 }25 return status;26 }
整个流程的结构非常清晰。
- busybox源码剖析(3)---cat.c
- busybox源码剖析(3)
- busybox源码剖析(1)---whoami.c
- busybox源码剖析(2)---pwd.c
- busybox源码剖析(2)
- busybox源码剖析(1)
- Linux cat 命令源码剖析
- linux c源码之cat源码
- Busybox源码
- c/c++:strlen源码剖析
- Lua源码剖析(lmathlib.c)
- Lua源码剖析(lstrlib.c)
- C++STL源码剖析代码
- STL源码剖析(3)
- linux常用命令剖析之cat
- busybox源码解析
- BusyBox源码结构简介
- 红黑树c源码实现与剖析
- JAVA操作properties文件
- hdu 2206 IP的计算
- 常见面试题之C++
- hdu 1272 小希的迷宫
- 对比Windows 8模拟器(Simulator)和Windows Phone仿真器(Emulator)
- busybox源码剖析(3)---cat.c
- Secret of the JavaScript Ninja 学习笔记 - 5
- get table info by column name
- Java GUI API
- Java子类的构造方法与多态
- Java异常处理
- Java抽象类与接口
- Atomic Commit In SQLite
- DLL文件脱壳(重定位表修复部分)