-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfd.c
49 lines (40 loc) · 795 Bytes
/
fd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUF_SIZE (1024 * 1024)
int main(int argc, char *argv[])
{
int fd;
int ret;
int fd1;
char buf[BUF_SIZE];
fd = open (argv[1], O_RDWR | O_APPEND);
if (fd == -1) {
perror ("open failed\n");
exit (1);
}
fd1 = open ("/dev/urandom", O_RDONLY);
if (fd == -1) {
perror ("open failed for urandom\n");
exit (1);
}
while (1) {
/* copy till quota exceeds */
if ((ret = read (fd1, buf, BUF_SIZE)) != -1) {
if (write (fd, buf, ret) != ret) {
perror ("write failed\n");
exit (1);
}
if (fsync (fd) == -1) {
perror ("fsync failed\n");
exit (1);
}
}
if (errno)
perror ("read failed\n");
}
}