2 * A tee implementation using sys_tee. Stores stdin input in the given file
3 * and duplicates that to stdout.
11 #include <sys/types.h>
18 static int splice_flags;
20 static int do_splice(int infd, int outfd, unsigned int len, char *msg)
23 int written = splice(infd, NULL, outfd, NULL, len, splice_flags);
34 static int usage(char *name)
36 fprintf(stderr, "... | %s: [-m(ove)] outfile\n", name);
40 static int parse_options(int argc, char *argv[])
44 while ((c = getopt(argc, argv, "m")) != -1) {
47 splice_flags = SPLICE_F_MOVE;
58 int main(int argc, char *argv[])
63 return usage(argv[0]);
65 if (check_input_pipe())
66 return usage(argv[0]);
68 index = parse_options(argc, argv);
69 if (index == -1 || index + 1 > argc)
70 return usage(argv[0]);
72 fd = open(argv[index], O_WRONLY | O_CREAT | O_TRUNC, 0644);
74 return error("open output");
77 int tee_len = tee(STDIN_FILENO, STDOUT_FILENO, INT_MAX, SPLICE_F_NONBLOCK);
80 if (errno == EAGAIN) {
89 * Send output to file, also consumes input pipe.
91 if (do_splice(STDIN_FILENO, fd, tee_len, "splice-file"))