blob: 13fb4a3c18735abb2b12675abc200e5a28e8b84e [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 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000014#include <signal.h>
John Beppua3e0d791999-12-10 07:41:03 +000015
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000016int tee_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000017int tee_main(int argc, char **argv)
John Beppu059f1521999-12-10 05:27:16 +000018{
Manuel Novoa III cad53642003-03-19 09:13:01 +000019 const char *mode = "w\0a";
Matt Kraai16384882000-08-28 03:53:27 +000020 FILE **files;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000021 FILE **fp;
22 char **names;
23 char **np;
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000024 char retval;
Denis Vlasenko4daad902007-09-27 10:20:47 +000025//TODO: make unconditional
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000026#if ENABLE_FEATURE_TEE_USE_BLOCK_IO
Manuel Novoa III d7097432004-05-26 15:21:19 +000027 ssize_t c;
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +000028# define buf bb_common_bufsiz1
Manuel Novoa III cad53642003-03-19 09:13:01 +000029#else
30 int c;
31#endif
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000032 retval = getopt32(argv, "ia"); /* 'a' must be 2nd */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000033 argc -= optind;
34 argv += optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +000035
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000036 mode += (retval & 2); /* Since 'a' is the 2nd option... */
Manuel Novoa III cad53642003-03-19 09:13:01 +000037
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000038 if (retval & 1) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000039 signal(SIGINT, SIG_IGN); /* TODO - switch to sigaction. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 }
Bernhard Reutner-Fischerb31c2522007-01-20 21:30:49 +000041 retval = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 /* gnu tee ignores SIGPIPE in case one of the output files is a pipe
43 * that doesn't consume all its input. Good idea... */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000044 signal(SIGPIPE, SIG_IGN); /* TODO - switch to sigaction. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000045
46 /* Allocate an array of FILE *'s, with one extra for a sentinal. */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000047 fp = files = xzalloc(sizeof(FILE *) * (argc + 2));
48 np = names = argv - 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000049
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000050 files[0] = stdout;
51 goto GOT_NEW_FILE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 do {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000053 *fp = fopen_or_warn(*argv, mode);
54 if (*fp == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 retval = EXIT_FAILURE;
56 continue;
57 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000058 *np = *argv++;
59 GOT_NEW_FILE:
60 setbuf(*fp++, NULL); /* tee must not buffer output. */
61 np++;
62 } while (*argv);
63 /* names[0] will be filled later */
Manuel Novoa III cad53642003-03-19 09:13:01 +000064
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000065#if ENABLE_FEATURE_TEE_USE_BLOCK_IO
Denis Vlasenko4daad902007-09-27 10:20:47 +000066 while ((c = safe_read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000067 fp = files;
68 do
69 fwrite(buf, 1, c, *fp++);
70 while (*fp);
John Beppu059f1521999-12-10 05:27:16 +000071 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000072 if (c < 0) { /* Make sure read errors are signaled. */
Manuel Novoa III d7097432004-05-26 15:21:19 +000073 retval = EXIT_FAILURE;
74 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000075#else
Eric Andersen637d2262003-10-22 10:18:24 +000076 setvbuf(stdout, NULL, _IONBF, 0);
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 while ((c = getchar()) != EOF) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000078 fp = files;
79 do
80 putc(c, *fp++);
81 while (*fp);
Eric Andersen2cb55071999-12-10 08:25:07 +000082 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000083#endif
John Beppu059f1521999-12-10 05:27:16 +000084
Eric Andersenc7bda1c2004-03-15 08:29:22 +000085 /* Now we need to check for i/o errors on stdin and the various
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 * output files. Since we know that the first entry in the output
87 * file table is stdout, we can save one "if ferror" test by
88 * setting the first entry to stdin and checking stdout error
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000089 * status with fflush_stdout_and_exit()... although fflush()ing
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 * is unnecessary here. */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000091 np = names;
92 fp = files;
93 names[0] = (char *) bb_msg_standard_input;
94 files[0] = stdin;
95 do { /* Now check for input and output errors. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 /* Checking ferror should be sufficient, but we may want to fclose.
97 * If we do, remember not to close stdin! */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000098 die_if_ferror(*fp++, *np++);
99 } while (*fp);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000101 fflush_stdout_and_exit(retval);
John Beppu059f1521999-12-10 05:27:16 +0000102}