1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,90,91,92,93,94,95,96,1997 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, NULL)) != -1)
106 ignore_interrupts = 1;
116 printf ("tee (%s) %s\n", GNU_PACKAGE, VERSION);
123 if (ignore_interrupts)
126 struct sigaction sigact;
128 sigact.sa_handler = SIG_IGN;
129 sigemptyset (&sigact.sa_mask);
131 sigaction (SIGINT, &sigact, NULL);
132 #else /* !_POSIX_SOURCE */
133 signal (SIGINT, SIG_IGN);
134 #endif /* _POSIX_SOURCE */
137 /* Don't let us be killed if one of the output files is a pipe that
138 doesn't consume all its input. */
141 struct sigaction sigact;
143 sigact.sa_handler = SIG_IGN;
144 sigemptyset (&sigact.sa_mask);
146 sigaction (SIGPIPE, &sigact, NULL);
149 signal (SIGPIPE, SIG_IGN);
152 errs = tee (argc - optind, (const char **) &argv[optind]);
154 error (1, errno, _("standard input"));
156 error (1, errno, _("standard output"));
160 /* Copy the standard input into each of the NFILES files in FILES
161 and into the standard output.
162 Return 0 if successful, 1 if any errors occur. */
165 tee (int nfiles, const char **files)
169 register int bytes_read, i, ret = 0, mode;
171 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
173 mode = O_WRONLY | O_CREAT;
179 /* Move all the names `up' one in the argv array to make room for
180 the entry for standard output. This writes into argv[argc]. */
181 for (i = nfiles; i >= 1; i--)
182 files[i] = files[i - 1];
184 /* In the array of NFILES + 1 descriptors, make
185 the first one correspond to standard output. */
187 files[0] = _("standard output");
189 for (i = 1; i <= nfiles; i++)
191 descriptors[i] = open (files[i], mode, 0666);
192 if (descriptors[i] == -1)
194 error (0, errno, "%s", files[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 if (descriptors[i] != -1
214 && full_write (descriptors[i], buffer, bytes_read) < 0)
216 error (0, errno, "%s", files[i]);
217 /* Don't close stdout. That's done in main. */
218 if (descriptors[i] != 1)
219 close (descriptors[i]);
226 if (bytes_read == -1)
228 error (0, errno, _("read error"));
232 /* Close the files, but not standard output. */
233 for (i = 1; i <= nfiles; i++)
234 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
236 error (0, errno, "%s", files[i]);