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