| 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 | |
| 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[]) |
| 20 | { |
| 21 | int c, index = 1; |
| 22 | |
| 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 | } |
| 32 | } |
| 33 | |
| 34 | return index; |
| 35 | } |
| 36 | |
| 37 | int main(int argc, char *argv[]) |
| 38 | { |
| 39 | int fd, index; |
| 40 | |
| 41 | if (check_input_pipe()) |
| 42 | return usage(argv[0]); |
| 43 | |
| 44 | index = parse_options(argc, argv); |
| 45 | if (index == -1 || index + 1 > argc) |
| 46 | return usage(argv[0]); |
| 47 | |
| 48 | fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 49 | if (fd < 0) |
| 50 | return error("open"); |
| 51 | |
| 52 | do { |
| 53 | int ret = splice(STDIN_FILENO, NULL, fd, NULL, SPLICE_SIZE, splice_flags); |
| 54 | |
| 55 | if (ret < 0) |
| 56 | return error("splice"); |
| 57 | else if (!ret) |
| 58 | break; |
| 59 | } while (1); |
| 60 | |
| 61 | close(fd); |
| 62 | return 0; |
| 63 | } |