1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85, 90, 91, 92, 93, 94, 95, 1996 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>
32 static int tee __P ((int nfiles, const char **files));
34 /* If nonzero, append to output files rather than truncating them. */
37 /* If nonzero, ignore interrupts. */
38 static int ignore_interrupts;
40 /* The name that this program was run with. */
43 /* If nonzero, display usage information and exit. */
46 /* If nonzero, print the version on standard output and exit. */
47 static int show_version;
49 static struct option const long_options[] =
51 {"append", no_argument, NULL, 'a'},
52 {"help", no_argument, &show_help, 1},
53 {"ignore-interrupts", no_argument, NULL, 'i'},
54 {"version", no_argument, &show_version, 1},
62 fprintf (stderr, _("Try `%s --help' for more information.\n"),
66 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
68 Copy standard input to each FILE, and also to standard output.\n\
70 -a, --append append to the given FILEs, do not overwrite\n\
71 -i, --ignore-interrupts ignore interrupt signals\n\
72 --help display this help and exit\n\
73 --version output version information and exit\n\
75 puts (_("\nReport bugs to sh-utils-bugs@gnu.ai.mit.edu"));
81 main (int argc, char **argv)
86 program_name = argv[0];
87 setlocale (LC_ALL, "");
88 bindtextdomain (PACKAGE, LOCALEDIR);
92 ignore_interrupts = 0;
94 while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
107 ignore_interrupts = 1;
117 printf ("tee (%s) %s\n", GNU_PACKAGE, VERSION);
124 if (ignore_interrupts)
127 struct sigaction sigact;
129 sigact.sa_handler = SIG_IGN;
130 sigemptyset (&sigact.sa_mask);
132 sigaction (SIGINT, &sigact, NULL);
133 #else /* !_POSIX_SOURCE */
134 signal (SIGINT, SIG_IGN);
135 #endif /* _POSIX_SOURCE */
138 /* Don't let us be killed if one of the output files is a pipe that
139 doesn't consume all its input. */
142 struct sigaction sigact;
144 sigact.sa_handler = SIG_IGN;
145 sigemptyset (&sigact.sa_mask);
147 sigaction (SIGPIPE, &sigact, NULL);
150 signal (SIGPIPE, SIG_IGN);
153 errs = tee (argc - optind, (const char **) &argv[optind]);
155 error (1, errno, _("standard input"));
157 error (1, errno, _("standard output"));
161 /* Copy the standard input into each of the NFILES files in FILES
162 and into the standard output.
163 Return 0 if successful, 1 if any errors occur. */
166 tee (int nfiles, const char **files)
170 register int bytes_read, i, ret = 0, mode;
172 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
174 mode = O_WRONLY | O_CREAT;
180 /* Move all the names `up' one in the argv array to make room for
181 the entry for standard output. This writes into argv[argc]. */
182 for (i = nfiles; i >= 1; i--)
183 files[i] = files[i - 1];
185 /* In the array of NFILES + 1 descriptors, make
186 the first one correspond to standard output. */
188 files[0] = _("standard output");
190 for (i = 1; i <= nfiles; i++)
192 descriptors[i] = open (files[i], mode, 0666);
193 if (descriptors[i] == -1)
195 error (0, errno, "%s", files[i]);
202 bytes_read = read (0, buffer, sizeof buffer);
204 if (bytes_read < 0 && errno == EINTR)
210 /* Write to all NFILES + 1 descriptors.
211 Standard output is the first one. */
212 for (i = 0; i <= nfiles; i++)
214 if (descriptors[i] != -1
215 && full_write (descriptors[i], buffer, bytes_read) < 0)
217 error (0, errno, "%s", files[i]);
218 /* Don't close stdout. That's done in main. */
219 if (descriptors[i] != 1)
220 close (descriptors[i]);
227 if (bytes_read == -1)
229 error (0, errno, _("read error"));
233 /* Close the files, but not standard output. */
234 for (i = 1; i <= nfiles; i++)
235 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
237 error (0, errno, "%s", files[i]);