Mercurial > hg > Members > masakoha > testcode
annotate 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 |
rev | line source |
---|---|
3 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
28
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
3 #include <string.h> |
3 | 4 #include <unistd.h> |
5 #include <fcntl.h> | |
6 #include <sys/stat.h> | |
7 #include <sys/types.h> | |
28
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
8 #include <sys/mman.h> |
3 | 9 |
10 | |
28
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
11 const char *usr_help_str = "Usage : ./mmap [-file filename]"; |
3 | 12 |
13 int main(int argc, char *argv[]){ | |
14 | |
15 struct stat sb; | |
16 | |
17 char *filename = 0; | |
18 for (int i = 1; argv[i]; ++i) { | |
19 if (strcmp(argv[i], "-file") == 0){ | |
28
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
20 filename = argv[i+1]; i++; |
3 | 21 } |
22 } | |
23 | |
24 if (filename == 0){ | |
25 puts(usr_help_str); | |
26 exit(1); | |
27 } | |
28 | |
29 int fd = -1; | |
30 if ((fd=open(filename,O_RDONLY,0666))==0){ | |
31 fprintf(stderr,"can't open %s\n",filename); | |
32 } | |
33 | |
34 if (fstat(fd,&sb)){ | |
35 fprintf(stderr,"can't open %s\n",filename); | |
36 } | |
37 | |
28
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
38 int text_size = sb.st_size; |
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
39 char *text = (char *)mmap(0, text_size, PROT_READ, MAP_PRIVATE, fd, 0); |
3 | 40 |
28
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
41 printf("%s\n",text); |
3 | 42 |
28
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
43 munmap(text, text_size); |
3 | 44 close(fd); |
45 | |
46 return 0; | |
47 } |