[PATCH] Formalize input/output pipe checking
[splice.git] / splice-in.c
1 /*
2  * Splice argument file to stdout
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10
11 #include "splice.h"
12
13 static int usage(char *name)
14 {
15         fprintf(stderr, "%s: infile | ...\n", name);
16         return 1;
17 }
18
19 int main(int argc, char *argv[])
20 {
21         struct stat sb;
22         int fd;
23
24         if (argc < 2)
25                 return usage(argv[0]);
26
27         if (check_output_pipe())
28                 return usage(argv[0]);
29
30         fd = open(argv[1], O_RDONLY);
31         if (fd < 0)
32                 return error("open input");
33
34         if (fstat(fd, &sb) < 0)
35                 return error("stat input");
36
37         do {
38                 int ret = splice(fd, NULL, STDOUT_FILENO, NULL, sb.st_size, 0);
39
40                 if (ret < 0)
41                         return error("splice");
42                 else if (!ret)
43                         break;
44
45                 sb.st_size -= ret;
46         } while (1);
47
48         close(fd);
49         return 0;
50 }