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