Project

General

Profile

Bug #5273 ยป read-file.c

Youzhong Yang, 2014-10-29 04:16 AM

 
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <sys/types.h>
4
#include <sys/stat.h>
5
#include <fcntl.h>
6
#include <unistd.h>
7

    
8
int main(int argc, char *argv[])
9
{
10
    char *filename = NULL;
11
    int fd;
12
    int ret;
13
    char buf[16];
14
    int seconds = 300;
15

    
16
    if(argc >= 2) {
17
       filename = argv[1];
18
       if(argc >= 3) seconds = atoi(argv[2]);
19
    }
20
    else {
21
       printf("%s <file name>\n", argv[0]);
22
       exit(1);
23
    }
24

    
25
    fd = open(filename, O_RDONLY);
26
    if(fd == -1) {
27
       perror("open");
28
       exit(1);
29
    }
30

    
31
    while(seconds--) {
32
       ret = pread(fd, buf, 16, 0);
33
       if(ret == -1) {
34
          perror("pread");
35
          break;
36
       }
37
       sleep(1);
38
    }
39

    
40
    ret = close(fd);
41
    if(ret == -1) {
42
       perror("close");
43
       exit(1);
44
    }
45

    
46
    return(0);
47
}
    (1-1/1)