1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-2005 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 */
22 #include <sys/types.h>
28 #include "stdio-safer.h"
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "tee"
33 #define AUTHORS "Mike Parker", "Richard M. Stallman", "David MacKenzie"
35 static bool tee (int nfiles, const char **files);
37 /* If true, append to output files rather than truncating them. */
40 /* If true, ignore interrupts. */
41 static bool ignore_interrupts;
43 /* The name that this program was run with. */
46 static struct option const long_options[] =
48 {"append", no_argument, NULL, 'a'},
49 {"ignore-interrupts", no_argument, NULL, 'i'},
50 {GETOPT_HELP_OPTION_DECL},
51 {GETOPT_VERSION_OPTION_DECL},
58 if (status != EXIT_SUCCESS)
59 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
65 Copy standard input to each FILE, and also to standard output.\n\
67 -a, --append append to the given FILEs, do not overwrite\n\
68 -i, --ignore-interrupts ignore interrupt signals\n\
70 fputs (HELP_OPTION_DESCRIPTION, stdout);
71 fputs (VERSION_OPTION_DESCRIPTION, stdout);
74 If a FILE is -, copy again to standard output.\n\
76 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
82 main (int argc, char **argv)
87 initialize_main (&argc, &argv);
88 program_name = argv[0];
89 setlocale (LC_ALL, "");
90 bindtextdomain (PACKAGE, LOCALEDIR);
93 atexit (close_stdout);
96 ignore_interrupts = false;
98 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
107 ignore_interrupts = true;
110 case_GETOPT_HELP_CHAR;
112 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
115 usage (EXIT_FAILURE);
119 if (ignore_interrupts)
120 signal (SIGINT, SIG_IGN);
122 /* Do *not* warn if tee is given no file arguments.
123 POSIX requires that it work when given no arguments. */
125 ok = tee (argc - optind, (const char **) &argv[optind]);
126 if (close (STDIN_FILENO) != 0)
127 error (EXIT_FAILURE, errno, _("standard input"));
129 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
132 /* Copy the standard input into each of the NFILES files in FILES
133 and into the standard output.
134 Return true if successful. */
137 tee (int nfiles, const char **files)
144 const char *mode_string = (append ? "a" : "w");
146 descriptors = xnmalloc (nfiles + 1, sizeof *descriptors);
148 /* Move all the names `up' one in the argv array to make room for
149 the entry for standard output. This writes into argv[argc]. */
150 for (i = nfiles; i >= 1; i--)
151 files[i] = files[i - 1];
155 /* In the array of NFILES + 1 descriptors, make
156 the first one correspond to standard output. */
157 descriptors[0] = stdout;
158 files[0] = _("standard output");
159 SETVBUF (stdout, NULL, _IONBF, 0);
161 for (i = 1; i <= nfiles; i++)
163 descriptors[i] = (STREQ (files[i], "-")
165 : fopen_safer (files[i], mode_string));
166 if (descriptors[i] == NULL)
168 error (0, errno, "%s", files[i]);
173 SETVBUF (descriptors[i], NULL, _IONBF, 0);
174 SET_BINARY (fileno (descriptors[i]));
180 bytes_read = read (0, buffer, sizeof buffer);
182 if (bytes_read < 0 && errno == EINTR)
188 /* Write to all NFILES + 1 descriptors.
189 Standard output is the first one. */
190 for (i = 0; i <= nfiles; i++)
192 && fwrite (buffer, 1, bytes_read, descriptors[i]) != bytes_read)
194 error (0, errno, "%s", files[i]);
195 descriptors[i] = NULL;
200 if (bytes_read == -1)
202 error (0, errno, _("read error"));
206 /* Close the files, but not standard output. */
207 for (i = 1; i <= nfiles; i++)
208 if (!STREQ (files[i], "-")
209 && descriptors[i] && fclose (descriptors[i]) != 0)
211 error (0, errno, "%s", files[i]);