blob: 602d06737a65652913f26d0fff2292b6f90410fa [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppu059f1521999-12-10 05:27:16 +00002/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * tee implementation for busybox
John Beppu059f1521999-12-10 05:27:16 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
John Beppu059f1521999-12-10 05:27:16 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
John Beppu059f1521999-12-10 05:27:16 +00008 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config TEE
10//config: bool "tee"
11//config: default y
12//config: help
13//config: tee is used to read from standard input and write
14//config: to standard output and files.
15//config:
16//config:config FEATURE_TEE_USE_BLOCK_IO
17//config: bool "Enable block I/O (larger/faster) instead of byte I/O"
18//config: default y
19//config: depends on TEE
20//config: help
21//config: Enable this option for a faster tee, at expense of size.
22
23//applet:IF_TEE(APPLET(tee, BB_DIR_USR_BIN, BB_SUID_DROP))
24
25//kbuild:lib-$(CONFIG_TEE) += tee.o
John Beppu059f1521999-12-10 05:27:16 +000026
Manuel Novoa III cad53642003-03-19 09:13:01 +000027/* BB_AUDIT SUSv3 compliant */
28/* http://www.opengroup.org/onlinepubs/007904975/utilities/tee.html */
29
Pere Orga34425382011-03-31 14:43:25 +020030//usage:#define tee_trivial_usage
31//usage: "[-ai] [FILE]..."
32//usage:#define tee_full_usage "\n\n"
33//usage: "Copy stdin to each FILE, and also to stdout\n"
Pere Orga34425382011-03-31 14:43:25 +020034//usage: "\n -a Append to the given FILEs, don't overwrite"
35//usage: "\n -i Ignore interrupt signals (SIGINT)"
36//usage:
37//usage:#define tee_example_usage
38//usage: "$ echo \"Hello\" | tee /tmp/foo\n"
39//usage: "$ cat /tmp/foo\n"
40//usage: "Hello\n"
41
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000042#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020043#include "common_bufsiz.h"
John Beppua3e0d791999-12-10 07:41:03 +000044
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000045int tee_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000046int tee_main(int argc, char **argv)
John Beppu059f1521999-12-10 05:27:16 +000047{
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 const char *mode = "w\0a";
Matt Kraai16384882000-08-28 03:53:27 +000049 FILE **files;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000050 FILE **fp;
51 char **names;
52 char **np;
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000053 char retval;
Denis Vlasenko4daad902007-09-27 10:20:47 +000054//TODO: make unconditional
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000055#if ENABLE_FEATURE_TEE_USE_BLOCK_IO
Manuel Novoa III d7097432004-05-26 15:21:19 +000056 ssize_t c;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020057# define buf bb_common_bufsiz1
58 setup_common_bufsiz();
Manuel Novoa III cad53642003-03-19 09:13:01 +000059#else
60 int c;
61#endif
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000062 retval = getopt32(argv, "ia"); /* 'a' must be 2nd */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000063 argc -= optind;
64 argv += optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +000065
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000066 mode += (retval & 2); /* Since 'a' is the 2nd option... */
Manuel Novoa III cad53642003-03-19 09:13:01 +000067
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000068 if (retval & 1) {
Denis Vlasenko25591c32008-02-16 22:58:56 +000069 signal(SIGINT, SIG_IGN); /* TODO - switch to sigaction. (why?) */
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 }
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000071 retval = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 /* gnu tee ignores SIGPIPE in case one of the output files is a pipe
73 * that doesn't consume all its input. Good idea... */
Denis Vlasenko25591c32008-02-16 22:58:56 +000074 signal(SIGPIPE, SIG_IGN);
Manuel Novoa III cad53642003-03-19 09:13:01 +000075
Denys Vlasenkoea694162010-10-17 12:45:24 +020076 /* Allocate an array of FILE *'s, with one extra for a sentinel. */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000077 fp = files = xzalloc(sizeof(FILE *) * (argc + 2));
78 np = names = argv - 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000079
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000080 files[0] = stdout;
81 goto GOT_NEW_FILE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 do {
Denis Vlasenko4b8171c2008-07-15 05:10:15 +000083 *fp = stdout;
84 if (NOT_LONE_DASH(*argv)) {
85 *fp = fopen_or_warn(*argv, mode);
86 if (*fp == NULL) {
87 retval = EXIT_FAILURE;
Denis Vlasenko8ddb6412008-07-16 07:34:00 +000088 argv++;
Denis Vlasenko4b8171c2008-07-15 05:10:15 +000089 continue;
90 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000092 *np = *argv++;
93 GOT_NEW_FILE:
Denis Vlasenko8ddb6412008-07-16 07:34:00 +000094 setbuf(*fp, NULL); /* tee must not buffer output. */
95 fp++;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000096 np++;
97 } while (*argv);
98 /* names[0] will be filled later */
Manuel Novoa III cad53642003-03-19 09:13:01 +000099
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000100#if ENABLE_FEATURE_TEE_USE_BLOCK_IO
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200101 while ((c = safe_read(STDIN_FILENO, buf, COMMON_BUFSIZE)) > 0) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000102 fp = files;
103 do
Dan Fandrich77d48722010-09-07 23:38:28 -0700104 fwrite(buf, 1, c, *fp);
105 while (*++fp);
John Beppu059f1521999-12-10 05:27:16 +0000106 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000107 if (c < 0) { /* Make sure read errors are signaled. */
Manuel Novoa III d7097432004-05-26 15:21:19 +0000108 retval = EXIT_FAILURE;
109 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110#else
Eric Andersen637d2262003-10-22 10:18:24 +0000111 setvbuf(stdout, NULL, _IONBF, 0);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 while ((c = getchar()) != EOF) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000113 fp = files;
114 do
Dan Fandrich77d48722010-09-07 23:38:28 -0700115 putc(c, *fp);
116 while (*++fp);
Eric Andersen2cb55071999-12-10 08:25:07 +0000117 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118#endif
John Beppu059f1521999-12-10 05:27:16 +0000119
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000120 /* Now we need to check for i/o errors on stdin and the various
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 * output files. Since we know that the first entry in the output
122 * file table is stdout, we can save one "if ferror" test by
123 * setting the first entry to stdin and checking stdout error
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000124 * status with fflush_stdout_and_exit()... although fflush()ing
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 * is unnecessary here. */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000126 np = names;
127 fp = files;
128 names[0] = (char *) bb_msg_standard_input;
129 files[0] = stdin;
130 do { /* Now check for input and output errors. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 /* Checking ferror should be sufficient, but we may want to fclose.
132 * If we do, remember not to close stdin! */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000133 die_if_ferror(*fp++, *np++);
134 } while (*fp);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000136 fflush_stdout_and_exit(retval);
John Beppu059f1521999-12-10 05:27:16 +0000137}