1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-1999 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 #include "long-options.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "tee"
34 #define AUTHORS "Mike Parker, Richard M. Stallman, and David MacKenzie"
38 static int tee PARAMS ((int nfiles, const char **files));
40 /* If nonzero, append to output files rather than truncating them. */
43 /* If nonzero, ignore interrupts. */
44 static int ignore_interrupts;
46 /* The name that this program was run with. */
49 static struct option const long_options[] =
51 {"append", no_argument, NULL, 'a'},
52 {"ignore-interrupts", no_argument, NULL, 'i'},
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\
70 --help display this help and exit\n\
71 --version output version information and exit\n\
73 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
79 main (int argc, char **argv)
84 program_name = argv[0];
85 setlocale (LC_ALL, "");
86 bindtextdomain (PACKAGE, LOCALEDIR);
89 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
93 ignore_interrupts = 0;
95 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
107 ignore_interrupts = 1;
115 if (ignore_interrupts)
118 struct sigaction sigact;
120 sigact.sa_handler = SIG_IGN;
121 sigemptyset (&sigact.sa_mask);
123 sigaction (SIGINT, &sigact, NULL);
124 #else /* !_POSIX_SOURCE */
125 signal (SIGINT, SIG_IGN);
126 #endif /* _POSIX_SOURCE */
129 /* Don't let us be killed if one of the output files is a pipe that
130 doesn't consume all its input. */
133 struct sigaction sigact;
135 sigact.sa_handler = SIG_IGN;
136 sigemptyset (&sigact.sa_mask);
138 sigaction (SIGPIPE, &sigact, NULL);
141 signal (SIGPIPE, SIG_IGN);
144 errs = tee (argc - optind, (const char **) &argv[optind]);
146 error (1, errno, _("standard input"));
153 /* Copy the standard input into each of the NFILES files in FILES
154 and into the standard output.
155 Return 0 if successful, 1 if any errors occur. */
158 tee (int nfiles, const char **files)
162 register int bytes_read, i, ret = 0, mode;
164 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
166 mode = O_WRONLY | O_CREAT;
172 /* Move all the names `up' one in the argv array to make room for
173 the entry for standard output. This writes into argv[argc]. */
174 for (i = nfiles; i >= 1; i--)
175 files[i] = files[i - 1];
177 /* In the array of NFILES + 1 descriptors, make
178 the first one correspond to standard output. */
180 files[0] = _("standard output");
182 for (i = 1; i <= nfiles; i++)
184 descriptors[i] = open (files[i], mode, 0666);
185 if (descriptors[i] == -1)
187 error (0, errno, "%s", files[i]);
194 bytes_read = read (0, buffer, sizeof buffer);
196 if (bytes_read < 0 && errno == EINTR)
202 /* Write to all NFILES + 1 descriptors.
203 Standard output is the first one. */
204 for (i = 0; i <= nfiles; i++)
206 if (descriptors[i] != -1
207 && full_write (descriptors[i], buffer, bytes_read) < 0)
209 error (0, errno, "%s", files[i]);
210 /* Don't close stdout. That's done in main. */
211 if (descriptors[i] != 1)
212 close (descriptors[i]);
219 if (bytes_read == -1)
221 error (0, errno, _("read error"));
225 /* Close the files, but not standard output. */
226 for (i = 1; i <= nfiles; i++)
227 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
229 error (0, errno, "%s", files[i]);