d0a31f2fb0b99c028d6e76f57dd4292ef0ae5642
[gapil.git] / listati / tee.c
1 #define _GNU_SOURCE
2 #include <fcntl.h>       /* file control functions */
3 ...
4 int main(int argc, char *argv[])
5 {
6     size_t size = 4096;
7     int fd, len, nwrite;
8     struct stat fdata;
9     ...
10     /* tee loop */
11     while (1) {
12         /* copy stdin to stdout */
13         len = tee(STDIN_FILENO, STDOUT_FILENO, size, 0);
14         if (len == 0) break;
15         if (len < 0) {
16             if (errno == EAGAIN) {
17                 continue;
18             } else {
19                 perror("error on tee stdin to stdout");
20                 exit(EXIT_FAILURE);
21             }
22         }
23         /* write data to the file using splice */
24         while (len > 0) {
25             nwrite = splice(STDIN_FILENO, NULL, fd, NULL, len, SPLICE_F_MOVE);
26             if (nwrite < 0) {
27                 perror("error on splice stdin to file");
28                 break;
29             }
30             len -= nwrite;
31         }
32     }
33     exit(EXIT_SUCCESS);
34 }