Framework for exporting system header to user space (#59)
[lunaix-os.git] / lunaix-os / usr / file_test.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #define BUFSIZE 4096
7
8 static char buffer[BUFSIZE];
9
10 #define _open(f, o)                                                 \
11     ({                                                              \
12         int __fd = open(f, o);                                      \
13         if (__fd < 0) {                                             \
14             printf("open failed: %s (error: %d)", f, errno);   \
15             _exit(__fd);                                            \
16         }                                                           \
17         __fd;                                                       \
18     })
19
20 int
21 main(int argc, const char* argv[])
22 {
23     int fd = 0, fdrand = 0;
24     int size = 0, sz2 = 0;
25
26     fd = _open(argv[1], O_RDWR | O_CREAT);
27     fdrand = _open("/dev/rand", O_RDONLY);
28     
29     for (int i = 0; i < 100; i++)
30     {
31         printf("write to file: (round) {%d}/100\n", i + 1);
32         
33         size = read(fdrand, buffer, BUFSIZE);
34         printf(">>> read random chars: %d\n", size);
35         for (int i = 0; i < size; i++)
36         {
37             buffer[i] = (char)((unsigned char)(buffer[i] % 94U) + 33U);
38         }
39         
40         sz2 += write(fd, buffer, size);
41         sz2 += write(fd, "\n\n", 2);
42     }
43
44     close(fd);
45     close(fdrand);
46
47     return 0;
48 }