Mercurial > hg > Applications > Grep
view c/mmap/main.cc @ 28:178cf2dfc45b
add make test
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 11 May 2014 18:14:02 +0900 |
parents | 0f4ccdbaf57f |
children | b9d005c23aaa |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/mman.h> const char *usr_help_str = "Usage : ./mmap [-file filename]"; int main(int argc, char *argv[]){ struct stat sb; char *filename = 0; for (int i = 1; argv[i]; ++i) { if (strcmp(argv[i], "-file") == 0){ filename = argv[i+1]; i++; } } if (filename == 0){ puts(usr_help_str); exit(1); } int fd = -1; if ((fd=open(filename,O_RDONLY,0666))==0){ fprintf(stderr,"can't open %s\n",filename); } if (fstat(fd,&sb)){ fprintf(stderr,"can't open %s\n",filename); } int text_size = sb.st_size; char *text = (char *)mmap(0, text_size, PROT_READ, MAP_PRIVATE, fd, 0); printf("%s\n",text); munmap(text, text_size); close(fd); return 0; }