tee: fix infinite looping on open error (echo asd | tee "")
[oweals/busybox.git] / coreutils / tee.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * tee implementation for busybox
4  *
5  * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /* BB_AUDIT SUSv3 compliant */
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/tee.html */
12
13 #include "libbb.h"
14 #include <signal.h>
15
16 int tee_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
17 int tee_main(int argc, char **argv)
18 {
19         const char *mode = "w\0a";
20         FILE **files;
21         FILE **fp;
22         char **names;
23         char **np;
24         char retval;
25 //TODO: make unconditional
26 #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
27         ssize_t c;
28 # define buf bb_common_bufsiz1
29 #else
30         int c;
31 #endif
32         retval = getopt32(argv, "ia");  /* 'a' must be 2nd */
33         argc -= optind;
34         argv += optind;
35
36         mode += (retval & 2);   /* Since 'a' is the 2nd option... */
37
38         if (retval & 1) {
39                 signal(SIGINT, SIG_IGN); /* TODO - switch to sigaction. (why?) */
40         }
41         retval = EXIT_SUCCESS;
42         /* gnu tee ignores SIGPIPE in case one of the output files is a pipe
43          * that doesn't consume all its input.  Good idea... */
44         signal(SIGPIPE, SIG_IGN);
45
46         /* Allocate an array of FILE *'s, with one extra for a sentinal. */
47         fp = files = xzalloc(sizeof(FILE *) * (argc + 2));
48         np = names = argv - 1;
49
50         files[0] = stdout;
51         goto GOT_NEW_FILE;
52         do {
53                 *fp = stdout;
54                 if (NOT_LONE_DASH(*argv)) {
55                         *fp = fopen_or_warn(*argv, mode);
56                         if (*fp == NULL) {
57                                 retval = EXIT_FAILURE;
58                                 argv++;
59                                 continue;
60                         }
61                 }
62                 *np = *argv++;
63  GOT_NEW_FILE:
64                 setbuf(*fp, NULL);      /* tee must not buffer output. */
65                 fp++;
66                 np++;
67         } while (*argv);
68         /* names[0] will be filled later */
69
70 #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
71         while ((c = safe_read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
72                 fp = files;
73                 do
74                         fwrite(buf, 1, c, *fp++);
75                 while (*fp);
76         }
77         if (c < 0) {            /* Make sure read errors are signaled. */
78                 retval = EXIT_FAILURE;
79         }
80 #else
81         setvbuf(stdout, NULL, _IONBF, 0);
82         while ((c = getchar()) != EOF) {
83                 fp = files;
84                 do
85                         putc(c, *fp++);
86                 while (*fp);
87         }
88 #endif
89
90         /* Now we need to check for i/o errors on stdin and the various
91          * output files.  Since we know that the first entry in the output
92          * file table is stdout, we can save one "if ferror" test by
93          * setting the first entry to stdin and checking stdout error
94          * status with fflush_stdout_and_exit()... although fflush()ing
95          * is unnecessary here. */
96         np = names;
97         fp = files;
98         names[0] = (char *) bb_msg_standard_input;
99         files[0] = stdin;
100         do {    /* Now check for input and output errors. */
101                 /* Checking ferror should be sufficient, but we may want to fclose.
102                  * If we do, remember not to close stdin! */
103                 die_if_ferror(*fp++, *np++);
104         } while (*fp);
105
106         fflush_stdout_and_exit(retval);
107 }