Commit | Line | Data |
---|---|---|
d8525fbd JA |
1 | /* |
2 | * Splice stdout 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 | ||
5d418df6 JA |
11 | static int splice_flags; |
12 | ||
13 | static int usage(char *name) | |
14 | { | |
15 | fprintf(stderr, "%s: [-m] out_file\n", name); | |
16 | return 1; | |
17 | } | |
18 | ||
19 | static int parse_options(int argc, char *argv[]) | |
d8525fbd | 20 | { |
5d418df6 | 21 | int c, index = 1; |
d8525fbd | 22 | |
5d418df6 JA |
23 | while ((c = getopt(argc, argv, "m")) != -1) { |
24 | switch (c) { | |
25 | case 'm': | |
26 | splice_flags = SPLICE_F_MOVE; | |
27 | index++; | |
28 | break; | |
29 | default: | |
30 | return -1; | |
31 | } | |
d8525fbd JA |
32 | } |
33 | ||
5d418df6 JA |
34 | return index; |
35 | } | |
36 | ||
37 | int main(int argc, char *argv[]) | |
38 | { | |
39 | int fd, index; | |
40 | ||
41 | index = parse_options(argc, argv); | |
42 | if (index == -1 || index + 1 > argc) | |
43 | return usage(argv[0]); | |
44 | ||
d8525fbd | 45 | fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644); |
49b68ef7 JA |
46 | if (fd < 0) |
47 | return error("open"); | |
d8525fbd JA |
48 | |
49 | do { | |
5d418df6 | 50 | int ret = splice(STDIN_FILENO, NULL, fd, NULL, SPLICE_SIZE, splice_flags); |
d8525fbd | 51 | |
49b68ef7 JA |
52 | if (ret < 0) |
53 | return error("splice"); | |
54 | else if (!ret) | |
d8525fbd JA |
55 | break; |
56 | } while (1); | |
57 | ||
58 | close(fd); | |
59 | return 0; | |
60 | } |