1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Mike Parker, Richard M. Stallman, and David MacKenzie */
22 #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 int tee (int nfiles, const char **files);
36 /* If nonzero, append to output files rather than truncating them. */
39 /* If nonzero, ignore interrupts. */
40 static int 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},
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);
71 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
77 main (int argc, char **argv)
82 initialize_main (&argc, &argv);
83 program_name = argv[0];
84 setlocale (LC_ALL, "");
85 bindtextdomain (PACKAGE, LOCALEDIR);
88 atexit (close_stdout);
91 ignore_interrupts = 0;
93 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
105 ignore_interrupts = 1;
108 case_GETOPT_HELP_CHAR;
110 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
113 usage (EXIT_FAILURE);
117 if (ignore_interrupts)
120 struct sigaction sigact;
122 sigact.sa_handler = SIG_IGN;
123 sigemptyset (&sigact.sa_mask);
125 sigaction (SIGINT, &sigact, NULL);
126 #else /* !_POSIX_SOURCE */
127 signal (SIGINT, SIG_IGN);
128 #endif /* _POSIX_SOURCE */
131 /* Don't let us be killed if one of the output files is a pipe that
132 doesn't consume all its input. */
135 struct sigaction sigact;
137 sigact.sa_handler = SIG_IGN;
138 sigemptyset (&sigact.sa_mask);
140 sigaction (SIGPIPE, &sigact, NULL);
143 signal (SIGPIPE, SIG_IGN);
146 /* Do *not* warn if tee is given no file arguments.
147 POSIX requires that it work when given no arguments. */
149 errs = tee (argc - optind, (const char **) &argv[optind]);
150 if (close (STDIN_FILENO) != 0)
151 error (EXIT_FAILURE, errno, _("standard input"));
156 /* Copy the standard input into each of the NFILES files in FILES
157 and into the standard output.
158 Return 0 if successful, 1 if any errors occur. */
161 tee (int nfiles, const char **files)
167 const char *mode_string = (append ? "a" : "w");
169 descriptors = xnmalloc (nfiles + 1, sizeof *descriptors);
171 /* Move all the names `up' one in the argv array to make room for
172 the entry for standard output. This writes into argv[argc]. */
173 for (i = nfiles; i >= 1; i--)
174 files[i] = files[i - 1];
178 /* In the array of NFILES + 1 descriptors, make
179 the first one correspond to standard output. */
180 descriptors[0] = stdout;
181 files[0] = _("standard output");
182 SETVBUF (stdout, NULL, _IONBF, 0);
184 for (i = 1; i <= nfiles; i++)
186 descriptors[i] = fopen (files[i], mode_string);
187 if (descriptors[i] == NULL)
189 error (0, errno, "%s", files[i]);
194 SETVBUF (descriptors[i], NULL, _IONBF, 0);
195 SET_BINARY (fileno (descriptors[i]));
201 bytes_read = read (0, buffer, sizeof buffer);
203 if (bytes_read < 0 && errno == EINTR)
209 /* Write to all NFILES + 1 descriptors.
210 Standard output is the first one. */
211 for (i = 0; i <= nfiles; i++)
213 && fwrite (buffer, 1, bytes_read, descriptors[i]) != bytes_read)
215 error (0, errno, "%s", files[i]);
216 descriptors[i] = NULL;
221 if (bytes_read == -1)
223 error (0, errno, _("read error"));
227 /* Close the files, but not standard output. */
228 for (i = 1; i <= nfiles; i++)
229 if (descriptors[i] && fclose (descriptors[i]) != 0)
231 error (0, errno, "%s", files[i]);