blob: a0e177cbcff9f413016e2e1a4806b3cf4ea86d90 [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 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
11/* http://www.opengroup.org/onlinepubs/007904975/utilities/tee.html */
12
Pere Orga34425382011-03-31 14:43:25 +020013//usage:#define tee_trivial_usage
14//usage: "[-ai] [FILE]..."
15//usage:#define tee_full_usage "\n\n"
16//usage: "Copy stdin to each FILE, and also to stdout\n"
Pere Orga34425382011-03-31 14:43:25 +020017//usage: "\n -a Append to the given FILEs, don't overwrite"
18//usage: "\n -i Ignore interrupt signals (SIGINT)"
19//usage:
20//usage:#define tee_example_usage
21//usage: "$ echo \"Hello\" | tee /tmp/foo\n"
22//usage: "$ cat /tmp/foo\n"
23//usage: "Hello\n"
24
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000025#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020026#include "common_bufsiz.h"
John Beppua3e0d791999-12-10 07:41:03 +000027
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000028int tee_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000029int tee_main(int argc, char **argv)
John Beppu059f1521999-12-10 05:27:16 +000030{
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 const char *mode = "w\0a";
Matt Kraai16384882000-08-28 03:53:27 +000032 FILE **files;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000033 FILE **fp;
34 char **names;
35 char **np;
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000036 char retval;
Denis Vlasenko4daad902007-09-27 10:20:47 +000037//TODO: make unconditional
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000038#if ENABLE_FEATURE_TEE_USE_BLOCK_IO
Manuel Novoa III d7097432004-05-26 15:21:19 +000039 ssize_t c;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020040# define buf bb_common_bufsiz1
41# define sizeof_buf COMMON_BUFSIZE
Manuel Novoa III cad53642003-03-19 09:13:01 +000042#else
43 int c;
44#endif
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000045 retval = getopt32(argv, "ia"); /* 'a' must be 2nd */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000046 argc -= optind;
47 argv += optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +000048
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000049 mode += (retval & 2); /* Since 'a' is the 2nd option... */
Manuel Novoa III cad53642003-03-19 09:13:01 +000050
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000051 if (retval & 1) {
Denis Vlasenko25591c32008-02-16 22:58:56 +000052 signal(SIGINT, SIG_IGN); /* TODO - switch to sigaction. (why?) */
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 }
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000054 retval = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 /* gnu tee ignores SIGPIPE in case one of the output files is a pipe
56 * that doesn't consume all its input. Good idea... */
Denis Vlasenko25591c32008-02-16 22:58:56 +000057 signal(SIGPIPE, SIG_IGN);
Manuel Novoa III cad53642003-03-19 09:13:01 +000058
Denys Vlasenkoea694162010-10-17 12:45:24 +020059 /* Allocate an array of FILE *'s, with one extra for a sentinel. */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000060 fp = files = xzalloc(sizeof(FILE *) * (argc + 2));
61 np = names = argv - 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000062
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000063 files[0] = stdout;
64 goto GOT_NEW_FILE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 do {
Denis Vlasenko4b8171c2008-07-15 05:10:15 +000066 *fp = stdout;
67 if (NOT_LONE_DASH(*argv)) {
68 *fp = fopen_or_warn(*argv, mode);
69 if (*fp == NULL) {
70 retval = EXIT_FAILURE;
Denis Vlasenko8ddb6412008-07-16 07:34:00 +000071 argv++;
Denis Vlasenko4b8171c2008-07-15 05:10:15 +000072 continue;
73 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000075 *np = *argv++;
76 GOT_NEW_FILE:
Denis Vlasenko8ddb6412008-07-16 07:34:00 +000077 setbuf(*fp, NULL); /* tee must not buffer output. */
78 fp++;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000079 np++;
80 } while (*argv);
81 /* names[0] will be filled later */
Manuel Novoa III cad53642003-03-19 09:13:01 +000082
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000083#if ENABLE_FEATURE_TEE_USE_BLOCK_IO
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020084 while ((c = safe_read(STDIN_FILENO, buf, sizeof_buf)) > 0) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000085 fp = files;
86 do
Dan Fandrich77d48722010-09-07 23:38:28 -070087 fwrite(buf, 1, c, *fp);
88 while (*++fp);
John Beppu059f1521999-12-10 05:27:16 +000089 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000090 if (c < 0) { /* Make sure read errors are signaled. */
Manuel Novoa III d7097432004-05-26 15:21:19 +000091 retval = EXIT_FAILURE;
92 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000093#else
Eric Andersen637d2262003-10-22 10:18:24 +000094 setvbuf(stdout, NULL, _IONBF, 0);
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 while ((c = getchar()) != EOF) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000096 fp = files;
97 do
Dan Fandrich77d48722010-09-07 23:38:28 -070098 putc(c, *fp);
99 while (*++fp);
Eric Andersen2cb55071999-12-10 08:25:07 +0000100 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101#endif
John Beppu059f1521999-12-10 05:27:16 +0000102
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000103 /* Now we need to check for i/o errors on stdin and the various
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 * output files. Since we know that the first entry in the output
105 * file table is stdout, we can save one "if ferror" test by
106 * setting the first entry to stdin and checking stdout error
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000107 * status with fflush_stdout_and_exit()... although fflush()ing
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 * is unnecessary here. */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000109 np = names;
110 fp = files;
111 names[0] = (char *) bb_msg_standard_input;
112 files[0] = stdin;
113 do { /* Now check for input and output errors. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 /* Checking ferror should be sufficient, but we may want to fclose.
115 * If we do, remember not to close stdin! */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000116 die_if_ferror(*fp++, *np++);
117 } while (*fp);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000119 fflush_stdout_and_exit(retval);
John Beppu059f1521999-12-10 05:27:16 +0000120}