[PATCH] Correct usage information
[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 (fstat(STDOUT_FILENO, &sb) < 0)
28                 return error("stat");
29         if (!S_ISFIFO(sb.st_mode)) {
30                 fprintf(stderr, "stdout must be a pipe\n");
31                 return usage(argv[0]);
32         }
33
34         fd = open(argv[1], O_RDONLY);
35         if (fd < 0)
36                 return error("open input");
37
38         if (fstat(fd, &sb) < 0)
39                 return error("stat input");
40
41         do {
42                 int ret = splice(fd, NULL, STDOUT_FILENO, NULL, sb.st_size, 0);
43
44                 if (ret < 0)
45                         return error("splice");
46                 else if (!ret)
47                         break;
48
49                 sb.st_size -= ret;
50         } while (1);
51
52         close(fd);
53         return 0;
54 }