Commit | Line | Data |
---|---|---|
d8525fbd | 1 | /* |
134883d3 | 2 | * Splice stdin to file |
d8525fbd JA |
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 | { | |
f91353a3 | 15 | fprintf(stderr, "... | %s: [-m] out_file\n", name); |
5d418df6 JA |
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 | ||
76797253 | 41 | if (check_input_pipe()) |
f91353a3 | 42 | return usage(argv[0]); |
f91353a3 | 43 | |
5d418df6 JA |
44 | index = parse_options(argc, argv); |
45 | if (index == -1 || index + 1 > argc) | |
46 | return usage(argv[0]); | |
47 | ||
d8525fbd | 48 | fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644); |
49b68ef7 JA |
49 | if (fd < 0) |
50 | return error("open"); | |
d8525fbd JA |
51 | |
52 | do { | |
5d418df6 | 53 | int ret = splice(STDIN_FILENO, NULL, fd, NULL, SPLICE_SIZE, splice_flags); |
d8525fbd | 54 | |
49b68ef7 JA |
55 | if (ret < 0) |
56 | return error("splice"); | |
57 | else if (!ret) | |
d8525fbd JA |
58 | break; |
59 | } while (1); | |
60 | ||
61 | close(fd); | |
62 | return 0; | |
63 | } |