blob: a194153e0271bbd8f9b5cf5beca69b4dae8c4fb5 [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;
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 FILE **p;
21 char **filenames;
22 int flags;
23 int retval = EXIT_SUCCESS;
24#ifdef CONFIG_FEATURE_TEE_USE_BLOCK_IO
Manuel Novoa III d7097432004-05-26 15:21:19 +000025 ssize_t c;
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +000026# define buf bb_common_bufsiz1
Manuel Novoa III cad53642003-03-19 09:13:01 +000027#else
28 int c;
29#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000030
Denis Vlasenko67b23e62006-10-03 21:00:06 +000031 flags = getopt32(argc, argv, "ia"); /* 'a' must be 2nd */
Manuel Novoa III cad53642003-03-19 09:13:01 +000032
33 mode += (flags & 2); /* Since 'a' is the 2nd option... */
34
35 if (flags & 1) {
36 signal(SIGINT, SIG_IGN); /* TODO - switch to sigaction.*/
37 }
38
39 /* gnu tee ignores SIGPIPE in case one of the output files is a pipe
40 * that doesn't consume all its input. Good idea... */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000041 signal(SIGPIPE, SIG_IGN); /* TODO - switch to sigaction.*/
Manuel Novoa III cad53642003-03-19 09:13:01 +000042
43 /* Allocate an array of FILE *'s, with one extra for a sentinal. */
44 p = files = (FILE **)xmalloc(sizeof(FILE *) * (argc - optind + 2));
45 *p = stdout;
46 argv += optind - 1;
47 filenames = argv - 1;
48 *filenames = (char *) bb_msg_standard_input; /* for later */
49 goto GOT_NEW_FILE;
50
51 do {
52 if ((*p = bb_wfopen(*argv, mode)) == NULL) {
53 retval = EXIT_FAILURE;
54 continue;
55 }
56 filenames[(int)(p - files)] = *argv;
57 GOT_NEW_FILE:
58 setbuf(*p, NULL); /* tee must not buffer output. */
59 ++p;
60 } while (*++argv);
61
62 *p = NULL; /* Store the sentinal value. */
63
64#ifdef CONFIG_FEATURE_TEE_USE_BLOCK_IO
Manuel Novoa III d7097432004-05-26 15:21:19 +000065 while ((c = safe_read(STDIN_FILENO, buf, BUFSIZ)) > 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 for (p=files ; *p ; p++) {
67 fwrite(buf, 1, c, *p);
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 }
John Beppu059f1521999-12-10 05:27:16 +000069 }
John Beppu059f1521999-12-10 05:27:16 +000070
Manuel Novoa III d7097432004-05-26 15:21:19 +000071 if (c < 0) { /* Make sure read errors are signaled. */
72 retval = EXIT_FAILURE;
73 }
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) {
78 for (p=files ; *p ; p++) {
79 putc(c, *p);
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 }
Eric Andersen2cb55071999-12-10 08:25:07 +000081 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000082#endif
John Beppu059f1521999-12-10 05:27:16 +000083
Eric Andersenc7bda1c2004-03-15 08:29:22 +000084 /* Now we need to check for i/o errors on stdin and the various
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 * output files. Since we know that the first entry in the output
86 * file table is stdout, we can save one "if ferror" test by
87 * setting the first entry to stdin and checking stdout error
88 * status with bb_fflush_stdout_and_exit()... although fflush()ing
89 * is unnecessary here. */
John Beppu059f1521999-12-10 05:27:16 +000090
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 p = files;
92 *p = stdin;
93 do { /* Now check for (input and) output errors. */
94 /* Checking ferror should be sufficient, but we may want to fclose.
95 * If we do, remember not to close stdin! */
Rob Landleyd921b2e2006-08-03 15:41:12 +000096 xferror(*p, filenames[(int)(p - files)]);
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 } while (*++p);
98
99 bb_fflush_stdout_and_exit(retval);
John Beppu059f1521999-12-10 05:27:16 +0000100}