Unix/Linux编程实践教程–cat在OS X的实现
2017-06-08 19:09
288 查看
环境:OS X 10.12.4
cat 这玩意儿在哪个Unix平台实现都一样吧- -
cat这个程序是把参数中的文件名的文件内容输出到标准输出,如果参数中没有文件名,默认是把标准输入的内容输出到标准输出。
这里使用了utmplib类似的代码用于缓存数据,减少系统调用的使用。
代码:
cat.c
xc_file.c
cat 这玩意儿在哪个Unix平台实现都一样吧- -
cat这个程序是把参数中的文件名的文件内容输出到标准输出,如果参数中没有文件名,默认是把标准输入的内容输出到标准输出。
这里使用了utmplib类似的代码用于缓存数据,减少系统调用的使用。
代码:
cat.c
#include <stdio.h> void xc_open(char *aFName); void xc_close(void); int xc_getchar(void); void xc_readFromStdin(void); /* use xc_file.c to read from file */ /* concatenate files and print on the standard output */ int main(int ac, char *av[]) { int ch; if(ac > 1){ while(--ac){ xc_open(*++av); while((ch = xc_getchar()) != EOF) putchar(ch); xc_close(); } }else{ xc_readFromStdin(); while((ch = xc_getchar()) != EOF) putchar(ch); } return 0; }
xc_file.c
#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> /* BUFSIZ define in stdio.h, commonly is 1024 */ static unsigned char chBuf[BUFSIZ]; static int fd = -1; static char fName[BUFSIZ]; static int chCur; static int chSum; void xc_readFromStdin(void) { /* define in unistd.h */ fd = STDIN_FILENO; } void xc_open(char *aFName) { if((fd = open(aFName, O_RDONLY)) == -1){ perror(aFName); exit(1); } strcpy(fName, aFName); /* record which file is opened */ chCur = chSum = 0; } int xc_reload(void) { int bytes_read; if((bytes_read = read(fd, chBuf, BUFSIZ)) > 0){ chCur = 0; chSum = bytes_read; return chBuf[chCur++]; }else if(bytes_read == -1){ perror(fName); exit(1); }else if (bytes_read == 0) return EOF; } int xc_getchar(void) { if(fd == -1) return EOF; if(chSum == chCur) return xc_reload(); return chBuf[chCur++]; } void xc_close(void) { if(fd != -1) { if(close(fd) == -1){ perror(fName); exit(1); } fd = -1; } }
相关文章推荐
- Unix/Linux编程实践教程–cp在OS X的实现
- Unix/Linux编程实践教程--who在OS X的实现
- Unix/Linux编程实践教程–tail在OS X的实现
- Unix/Linux编程实践教程–head在OS X的实现
- Unix/Linux编程实践教程–od在OS X的实现
- Unix/Linux编程实践教程–ac在Ubuntu 14.04的实现
- Unix/Linux编程实践教程–chmod在Centos7.3的实现
- Unix/Linux编程实践教程–last在Ubuntu 14.04的实现
- cat命令与script命令结合实现一对多命令演示教程
- unix/linux编程实践教程------学习笔记(第二章)
- Unix/Linux编程实践教程参考答案-----第二章学习笔记
- unix/linux编程实践教程----fflush函数有什么作用
- unix/linux编程实践教程:ls命令
- unix/linux编程实践教程:pwd命令
- unix/linux编程实践教程-Emacs常用命令整理
- unix/linux编程实践教程------学习笔记(三)
- unix/linux编程实践教程------学习笔记(四)
- unix/linux编程实践教程:进程和程序
- unix/linux编程实践教程:学习stty
- Unix/Linux编程实践教程--函数记录:文件、目录