1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-2006 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Mike Parker, Richard M. Stallman, and David MacKenzie */
21 #include <sys/types.h>
29 /* The official name of this program (e.g., no `g' prefix). */
30 #define PROGRAM_NAME "tee"
32 #define AUTHORS "Mike Parker", "Richard M. Stallman", "David MacKenzie"
34 static bool tee_files (int nfiles, const char **files);
36 /* If true, append to output files rather than truncating them. */
39 /* If true, ignore interrupts. */
40 static bool ignore_interrupts;
42 /* The name that this program was run with. */
45 static struct option const long_options[] =
47 {"append", no_argument, NULL, 'a'},
48 {"ignore-interrupts", no_argument, NULL, 'i'},
49 {GETOPT_HELP_OPTION_DECL},
50 {GETOPT_VERSION_OPTION_DECL},
57 if (status != EXIT_SUCCESS)
58 fprintf (stderr, _("Try `%s --help' for more information.\n"),
62 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
64 Copy standard input to each FILE, and also to standard output.\n\
66 -a, --append append to the given FILEs, do not overwrite\n\
67 -i, --ignore-interrupts ignore interrupt signals\n\
69 fputs (HELP_OPTION_DESCRIPTION, stdout);
70 fputs (VERSION_OPTION_DESCRIPTION, stdout);
73 If a FILE is -, copy again to standard output.\n\
75 emit_bug_reporting_address ();
81 main (int argc, char **argv)
86 initialize_main (&argc, &argv);
87 program_name = argv[0];
88 setlocale (LC_ALL, "");
89 bindtextdomain (PACKAGE, LOCALEDIR);
92 atexit (close_stdout);
95 ignore_interrupts = false;
97 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
106 ignore_interrupts = true;
109 case_GETOPT_HELP_CHAR;
111 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
114 usage (EXIT_FAILURE);
118 if (ignore_interrupts)
119 signal (SIGINT, SIG_IGN);
121 /* Do *not* warn if tee is given no file arguments.
122 POSIX requires that it work when given no arguments. */
124 ok = tee_files (argc - optind, (const char **) &argv[optind]);
125 if (close (STDIN_FILENO) != 0)
126 error (EXIT_FAILURE, errno, _("standard input"));
128 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
131 /* Copy the standard input into each of the NFILES files in FILES
132 and into the standard output.
133 Return true if successful. */
136 tee_files (int nfiles, const char **files)
143 char const *mode_string =
145 ? (append ? "ab" : "wb")
146 : (append ? "a" : "w"));
148 descriptors = xnmalloc (nfiles + 1, sizeof *descriptors);
150 /* Move all the names `up' one in the argv array to make room for
151 the entry for standard output. This writes into argv[argc]. */
152 for (i = nfiles; i >= 1; i--)
153 files[i] = files[i - 1];
155 if (O_BINARY && ! isatty (STDIN_FILENO))
156 freopen (NULL, "rb", stdin);
157 if (O_BINARY && ! isatty (STDOUT_FILENO))
158 freopen (NULL, "wb", stdout);
160 /* In the array of NFILES + 1 descriptors, make
161 the first one correspond to standard output. */
162 descriptors[0] = stdout;
163 files[0] = _("standard output");
164 setvbuf (stdout, NULL, _IONBF, 0);
166 for (i = 1; i <= nfiles; i++)
168 descriptors[i] = (STREQ (files[i], "-")
170 : fopen (files[i], mode_string));
171 if (descriptors[i] == NULL)
173 error (0, errno, "%s", files[i]);
177 setvbuf (descriptors[i], NULL, _IONBF, 0);
182 bytes_read = read (0, buffer, sizeof buffer);
184 if (bytes_read < 0 && errno == EINTR)
190 /* Write to all NFILES + 1 descriptors.
191 Standard output is the first one. */
192 for (i = 0; i <= nfiles; i++)
194 && fwrite (buffer, 1, bytes_read, descriptors[i]) != bytes_read)
196 error (0, errno, "%s", files[i]);
197 descriptors[i] = NULL;
202 if (bytes_read == -1)
204 error (0, errno, _("read error"));
208 /* Close the files, but not standard output. */
209 for (i = 1; i <= nfiles; i++)
210 if (!STREQ (files[i], "-")
211 && descriptors[i] && fclose (descriptors[i]) != 0)
213 error (0, errno, "%s", files[i]);