Fix various compile warnings
[splice.git] / splice-out.c
1 /*
2  * Splice stdin to file
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8
9 #include "splice.h"
10
11 static int splice_flags;
12 static unsigned int splice_size = SPLICE_SIZE;
13
14 static int usage(char *name)
15 {
16         fprintf(stderr, "... | %s: [-m] [-s splice size] out_file\n", name);
17         return 1;
18 }
19
20 static int parse_options(int argc, char *argv[])
21 {
22         int c, index = 1;
23
24         while ((c = getopt(argc, argv, "ms:")) != -1) {
25                 switch (c) {
26                 case 'm':
27                         splice_flags = SPLICE_F_MOVE;
28                         index++;
29                         break;
30                 case 's':
31                         splice_size = atoi(optarg);
32                         index++;
33                         break;
34                 default:
35                         return -1;
36                 }
37         }
38
39         return index;
40 }
41
42 int main(int argc, char *argv[])
43 {
44         int fd, index;
45
46         if (check_input_pipe())
47                 return usage(argv[0]);
48
49         index = parse_options(argc, argv);
50         if (index == -1 || index + 1 > argc)
51                 return usage(argv[0]);
52
53         fd = open(argv[index], O_WRONLY | O_CREAT | O_TRUNC, 0644);
54         if (fd < 0)
55                 return error("open");
56
57         do {
58                 int ret = ssplice(STDIN_FILENO, NULL, fd, NULL, splice_size, splice_flags);
59
60                 if (ret < 0)
61                         return error("splice");
62                 else if (!ret)
63                         break;
64         } while (1);
65
66         close(fd);
67         return 0;
68 }