1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-2006, 2008 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Mike Parker, Richard M. Stallman, and David MacKenzie */
20 #include <sys/types.h>
28 /* The official name of this program (e.g., no `g' prefix). */
29 #define PROGRAM_NAME "tee"
32 proper_name ("Mike Parker"), \
33 proper_name ("Richard M. Stallman"), \
34 proper_name ("David MacKenzie")
36 static bool tee_files (int nfiles, const char **files);
38 /* If true, append to output files rather than truncating them. */
41 /* If true, ignore interrupts. */
42 static bool ignore_interrupts;
44 /* The name that this program was run with. */
47 static struct option const long_options[] =
49 {"append", no_argument, NULL, 'a'},
50 {"ignore-interrupts", no_argument, NULL, 'i'},
51 {GETOPT_HELP_OPTION_DECL},
52 {GETOPT_VERSION_OPTION_DECL},
59 if (status != EXIT_SUCCESS)
60 fprintf (stderr, _("Try `%s --help' for more information.\n"),
64 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
66 Copy standard input to each FILE, and also to standard output.\n\
68 -a, --append append to the given FILEs, do not overwrite\n\
69 -i, --ignore-interrupts ignore interrupt signals\n\
71 fputs (HELP_OPTION_DESCRIPTION, stdout);
72 fputs (VERSION_OPTION_DESCRIPTION, stdout);
75 If a FILE is -, copy again to standard output.\n\
77 emit_bug_reporting_address ();
83 main (int argc, char **argv)
88 initialize_main (&argc, &argv);
89 program_name = argv[0];
90 setlocale (LC_ALL, "");
91 bindtextdomain (PACKAGE, LOCALEDIR);
94 atexit (close_stdout);
97 ignore_interrupts = false;
99 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
108 ignore_interrupts = true;
111 case_GETOPT_HELP_CHAR;
113 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
116 usage (EXIT_FAILURE);
120 if (ignore_interrupts)
121 signal (SIGINT, SIG_IGN);
123 /* Do *not* warn if tee is given no file arguments.
124 POSIX requires that it work when given no arguments. */
126 ok = tee_files (argc - optind, (const char **) &argv[optind]);
127 if (close (STDIN_FILENO) != 0)
128 error (EXIT_FAILURE, errno, _("standard input"));
130 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
133 /* Copy the standard input into each of the NFILES files in FILES
134 and into the standard output.
135 Return true if successful. */
138 tee_files (int nfiles, const char **files)
145 char const *mode_string =
147 ? (append ? "ab" : "wb")
148 : (append ? "a" : "w"));
150 descriptors = xnmalloc (nfiles + 1, sizeof *descriptors);
152 /* Move all the names `up' one in the argv array to make room for
153 the entry for standard output. This writes into argv[argc]. */
154 for (i = nfiles; i >= 1; i--)
155 files[i] = files[i - 1];
157 if (O_BINARY && ! isatty (STDIN_FILENO))
158 freopen (NULL, "rb", stdin);
159 if (O_BINARY && ! isatty (STDOUT_FILENO))
160 freopen (NULL, "wb", stdout);
162 /* In the array of NFILES + 1 descriptors, make
163 the first one correspond to standard output. */
164 descriptors[0] = stdout;
165 files[0] = _("standard output");
166 setvbuf (stdout, NULL, _IONBF, 0);
168 for (i = 1; i <= nfiles; i++)
170 descriptors[i] = (STREQ (files[i], "-")
172 : fopen (files[i], mode_string));
173 if (descriptors[i] == NULL)
175 error (0, errno, "%s", files[i]);
179 setvbuf (descriptors[i], NULL, _IONBF, 0);
184 bytes_read = read (0, buffer, sizeof buffer);
186 if (bytes_read < 0 && errno == EINTR)
192 /* Write to all NFILES + 1 descriptors.
193 Standard output is the first one. */
194 for (i = 0; i <= nfiles; i++)
196 && fwrite (buffer, 1, bytes_read, descriptors[i]) != bytes_read)
198 error (0, errno, "%s", files[i]);
199 descriptors[i] = NULL;
204 if (bytes_read == -1)
206 error (0, errno, _("read error"));
210 /* Close the files, but not standard output. */
211 for (i = 1; i <= nfiles; i++)
212 if (!STREQ (files[i], "-")
213 && descriptors[i] && fclose (descriptors[i]) != 0)
215 error (0, errno, "%s", files[i]);