📄 mmcat.c
字号:
/* * mmcat.c - Implement the cat command using memory maps */#include <sys/types.h>#include <sys/mman.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>void err_quit(char *msg);int main(int argc, char *argv[]){ int fdin; char *src; struct stat statbuf; off_t len; /* make sure we were called properly */ if(argc != 2) { fprintf(stderr, "usage: mmcat {file}\n"); exit(EXIT_FAILURE); } /* open the input file and stdout */ if((fdin = open(argv[1], O_RDONLY)) < 0) { err_quit("open"); } /* need the size of the input file for mmap call */ if((fstat(fdin, &statbuf)) < 0) { err_quit("fstat"); } len = statbuf.st_size; /* map the input file */ if((src = mmap(0, len, PROT_READ, MAP_SHARED, fdin, 0)) == (void *)-1) { err_quit("mmap"); } /* write it out */ printf("%s", src); /* clean up */ close(fdin); munmap(src, len); exit(EXIT_SUCCESS);}void err_quit(char *msg) { perror(msg); exit(EXIT_FAILURE);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -