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"
33 static int tee PARAMS ((int nfiles, const char **files));
35 /* If nonzero, append to output files rather than truncating them. */
38 /* If nonzero, ignore interrupts. */
39 static int ignore_interrupts;
41 /* The name that this program was run with. */
44 static struct option const long_options[] =
46 {"append", no_argument, NULL, 'a'},
47 {"ignore-interrupts", no_argument, NULL, 'i'},
55 fprintf (stderr, _("Try `%s --help' for more information.\n"),
59 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
61 Copy standard input to each FILE, and also to standard output.\n\
63 -a, --append append to the given FILEs, do not overwrite\n\
64 -i, --ignore-interrupts ignore interrupt signals\n\
65 --help display this help and exit\n\
66 --version output version information and exit\n\
68 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
74 main (int argc, char **argv)
79 program_name = argv[0];
80 setlocale (LC_ALL, "");
81 bindtextdomain (PACKAGE, LOCALEDIR);
84 parse_long_options (argc, argv, "tee", GNU_PACKAGE, VERSION,
85 "Mike Parker, Richard M. Stallman, and David MacKenzie",
89 ignore_interrupts = 0;
91 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
103 ignore_interrupts = 1;
111 if (ignore_interrupts)
114 struct sigaction sigact;
116 sigact.sa_handler = SIG_IGN;
117 sigemptyset (&sigact.sa_mask);
119 sigaction (SIGINT, &sigact, NULL);
120 #else /* !_POSIX_SOURCE */
121 signal (SIGINT, SIG_IGN);
122 #endif /* _POSIX_SOURCE */
125 /* Don't let us be killed if one of the output files is a pipe that
126 doesn't consume all its input. */
129 struct sigaction sigact;
131 sigact.sa_handler = SIG_IGN;
132 sigemptyset (&sigact.sa_mask);
134 sigaction (SIGPIPE, &sigact, NULL);
137 signal (SIGPIPE, SIG_IGN);
140 errs = tee (argc - optind, (const char **) &argv[optind]);
142 error (1, errno, _("standard input"));
149 /* Copy the standard input into each of the NFILES files in FILES
150 and into the standard output.
151 Return 0 if successful, 1 if any errors occur. */
154 tee (int nfiles, const char **files)
158 register int bytes_read, i, ret = 0, mode;
160 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
162 mode = O_WRONLY | O_CREAT;
168 /* Move all the names `up' one in the argv array to make room for
169 the entry for standard output. This writes into argv[argc]. */
170 for (i = nfiles; i >= 1; i--)
171 files[i] = files[i - 1];
173 /* In the array of NFILES + 1 descriptors, make
174 the first one correspond to standard output. */
176 files[0] = _("standard output");
178 for (i = 1; i <= nfiles; i++)
180 descriptors[i] = open (files[i], mode, 0666);
181 if (descriptors[i] == -1)
183 error (0, errno, "%s", files[i]);
190 bytes_read = read (0, buffer, sizeof buffer);
192 if (bytes_read < 0 && errno == EINTR)
198 /* Write to all NFILES + 1 descriptors.
199 Standard output is the first one. */
200 for (i = 0; i <= nfiles; i++)
202 if (descriptors[i] != -1
203 && full_write (descriptors[i], buffer, bytes_read) < 0)
205 error (0, errno, "%s", files[i]);
206 /* Don't close stdout. That's done in main. */
207 if (descriptors[i] != 1)
208 close (descriptors[i]);
215 if (bytes_read == -1)
217 error (0, errno, _("read error"));
221 /* Close the files, but not standard output. */
222 for (i = 1; i <= nfiles; i++)
223 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
225 error (0, errno, "%s", files[i]);