]>
Commit | Line | Data |
---|---|---|
97f1f27b YY |
1 | #define _GNU_SOURCE |
2 | #include <stdio.h> | |
3 | #include <unistd.h> | |
4 | #include <sys/types.h> | |
5 | #include <sys/stat.h> | |
6 | #include <fcntl.h> | |
7 | #include <errno.h> | |
8 | #include <string.h> | |
9 | #include <stdlib.h> | |
10 | ||
11 | #define BUFSIZE 1025 | |
12 | char buf[BUFSIZE]; | |
13 | ||
14 | int read_count = 2; | |
15 | ||
16 | int main(int argc, char *argv[]){ | |
17 | if(argc < 3){ | |
18 | fprintf(stderr, "usage: %s <file> <count> [buffer|direct]\n", argv[0]); | |
19 | exit(1); | |
20 | } | |
21 | char *file = argv[1]; | |
22 | read_count = atoi(argv[2]); | |
23 | int ret = 0,sum = 0, i = 0, fd = -1; | |
24 | if(argc == 4 && strncmp(argv[3], "direct",6) == 0) | |
25 | fd = open(file, O_RDONLY|O_DIRECT); | |
26 | else | |
27 | fd = open(file, O_RDONLY); | |
28 | ||
29 | while(i++ < read_count){ | |
30 | memset(buf, 0, BUFSIZE); | |
31 | ret = read(fd, buf, BUFSIZE-1); | |
32 | if(ret > 0){ | |
33 | write(STDOUT_FILENO, buf, ret); | |
34 | sum += ret; | |
35 | }else if(ret == 0){ | |
36 | printf("======read end======\n"); | |
37 | break; | |
38 | }else{ | |
39 | printf("error:%d\n", errno); | |
40 | break; | |
41 | } | |
42 | sleep(1); | |
43 | } | |
44 | printf("======read sum: %d======\n", sum); | |
45 | close(fd); | |
46 | return 0; | |
47 | } |