blob: 640a231ef0efb9ec3dc6690e8e16cc84287e320f [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
Manuel Novoa III cad53642003-03-19 09:13:01 +000013#include "busybox.h"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000014#include <signal.h>
John Beppua3e0d791999-12-10 07:41:03 +000015
Manuel Novoa III cad53642003-03-19 09:13:01 +000016int tee_main(int argc, char **argv)
John Beppu059f1521999-12-10 05:27:16 +000017{
Manuel Novoa III cad53642003-03-19 09:13:01 +000018 const char *mode = "w\0a";
Matt Kraai16384882000-08-28 03:53:27 +000019 FILE **files;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000020 FILE **fp;
21 char **names;
22 char **np;
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 int flags;
24 int retval = EXIT_SUCCESS;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000025#if ENABLE_FEATURE_TEE_USE_BLOCK_IO
Manuel Novoa III d7097432004-05-26 15:21:19 +000026 ssize_t c;
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +000027# define buf bb_common_bufsiz1
Manuel Novoa III cad53642003-03-19 09:13:01 +000028#else
29 int c;
30#endif
Denis Vlasenko67b23e62006-10-03 21:00:06 +000031 flags = getopt32(argc, argv, "ia"); /* 'a' must be 2nd */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000032 argc -= optind;
33 argv += optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +000034
35 mode += (flags & 2); /* Since 'a' is the 2nd option... */
36
37 if (flags & 1) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000038 signal(SIGINT, SIG_IGN); /* TODO - switch to sigaction. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 /* gnu tee ignores SIGPIPE in case one of the output files is a pipe
41 * that doesn't consume all its input. Good idea... */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000042 signal(SIGPIPE, SIG_IGN); /* TODO - switch to sigaction. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000043
44 /* Allocate an array of FILE *'s, with one extra for a sentinal. */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000045 fp = files = xzalloc(sizeof(FILE *) * (argc + 2));
46 np = names = argv - 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000047
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000048 files[0] = stdout;
49 goto GOT_NEW_FILE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 do {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000051 *fp = fopen_or_warn(*argv, mode);
52 if (*fp == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 retval = EXIT_FAILURE;
54 continue;
55 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000056 *np = *argv++;
57 GOT_NEW_FILE:
58 setbuf(*fp++, NULL); /* tee must not buffer output. */
59 np++;
60 } while (*argv);
61 /* names[0] will be filled later */
Manuel Novoa III cad53642003-03-19 09:13:01 +000062
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000063#if ENABLE_FEATURE_TEE_USE_BLOCK_IO
Manuel Novoa III d7097432004-05-26 15:21:19 +000064 while ((c = safe_read(STDIN_FILENO, buf, BUFSIZ)) > 0) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000065 fp = files;
66 do
67 fwrite(buf, 1, c, *fp++);
68 while (*fp);
John Beppu059f1521999-12-10 05:27:16 +000069 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000070 if (c < 0) { /* Make sure read errors are signaled. */
Manuel Novoa III d7097432004-05-26 15:21:19 +000071 retval = EXIT_FAILURE;
72 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000073#else
Eric Andersen637d2262003-10-22 10:18:24 +000074 setvbuf(stdout, NULL, _IONBF, 0);
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 while ((c = getchar()) != EOF) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000076 fp = files;
77 do
78 putc(c, *fp++);
79 while (*fp);
Eric Andersen2cb55071999-12-10 08:25:07 +000080 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000081#endif
John Beppu059f1521999-12-10 05:27:16 +000082
Eric Andersenc7bda1c2004-03-15 08:29:22 +000083 /* Now we need to check for i/o errors on stdin and the various
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 * output files. Since we know that the first entry in the output
85 * file table is stdout, we can save one "if ferror" test by
86 * setting the first entry to stdin and checking stdout error
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000087 * status with fflush_stdout_and_exit()... although fflush()ing
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 * is unnecessary here. */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000089 np = names;
90 fp = files;
91 names[0] = (char *) bb_msg_standard_input;
92 files[0] = stdin;
93 do { /* Now check for input and output errors. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 /* Checking ferror should be sufficient, but we may want to fclose.
95 * If we do, remember not to close stdin! */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000096 die_if_ferror(*fp++, *np++);
97 } while (*fp);
Manuel Novoa III cad53642003-03-19 09:13:01 +000098
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000099 fflush_stdout_and_exit(retval);
John Beppu059f1521999-12-10 05:27:16 +0000100}