2 * A tee implementation using sys_tee. If only one argument is given,
3 * stdin output is stored in that file and sent to stdout as well. If a
4 * second argument is given, that must be in the form if host:port - in
5 * that case, output is stored in file and sent over the network to the
6 * given host at given port.
15 #include <sys/types.h>
22 static int do_splice(int infd, int outfd, unsigned int len, char *msg)
25 int written = splice(infd, NULL, outfd, NULL, len, 0);
36 int main(int argc, char *argv[])
42 fprintf(stderr, "%s: outfile\n", argv[0]);
46 if (fstat(STDIN_FILENO, &sb) < 0)
48 if (!S_ISFIFO(sb.st_mode)) {
49 fprintf(stderr, "stdout must be a pipe\n");
53 fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
55 return error("open output");
58 int tee_len = tee(STDIN_FILENO, STDOUT_FILENO, INT_MAX, SPLICE_F_NONBLOCK);
61 if (errno == EAGAIN) {
70 * Send output to file, also consumes input pipe.
72 if (do_splice(STDIN_FILENO, fd, tee_len, "splice-file"))