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>
31 static int tee PARAMS ((int nfiles, const char **files));
33 /* If nonzero, append to output files rather than truncating them. */
36 /* If nonzero, ignore interrupts. */
37 static int ignore_interrupts;
39 /* The name that this program was run with. */
42 /* If nonzero, display usage information and exit. */
45 /* If nonzero, print the version on standard output and exit. */
46 static int show_version;
48 static struct option const long_options[] =
50 {"append", no_argument, NULL, 'a'},
51 {"help", no_argument, &show_help, 1},
52 {"ignore-interrupts", no_argument, NULL, 'i'},
53 {"version", no_argument, &show_version, 1},
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
65 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
67 Copy standard input to each FILE, and also to standard output.\n\
69 -a, --append append to the given FILEs, do not overwrite\n\
70 -i, --ignore-interrupts ignore interrupt signals\n\
71 --help display this help and exit\n\
72 --version output version information and exit\n\
74 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
80 main (int argc, char **argv)
85 program_name = argv[0];
86 setlocale (LC_ALL, "");
87 bindtextdomain (PACKAGE, LOCALEDIR);
91 ignore_interrupts = 0;
93 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
105 ignore_interrupts = 1;
115 printf ("tee (%s) %s\n", GNU_PACKAGE, VERSION);
122 if (ignore_interrupts)
125 struct sigaction sigact;
127 sigact.sa_handler = SIG_IGN;
128 sigemptyset (&sigact.sa_mask);
130 sigaction (SIGINT, &sigact, NULL);
131 #else /* !_POSIX_SOURCE */
132 signal (SIGINT, SIG_IGN);
133 #endif /* _POSIX_SOURCE */
136 /* Don't let us be killed if one of the output files is a pipe that
137 doesn't consume all its input. */
140 struct sigaction sigact;
142 sigact.sa_handler = SIG_IGN;
143 sigemptyset (&sigact.sa_mask);
145 sigaction (SIGPIPE, &sigact, NULL);
148 signal (SIGPIPE, SIG_IGN);
151 errs = tee (argc - optind, (const char **) &argv[optind]);
153 error (1, errno, _("standard input"));
155 error (1, errno, _("standard output"));
159 /* Copy the standard input into each of the NFILES files in FILES
160 and into the standard output.
161 Return 0 if successful, 1 if any errors occur. */
164 tee (int nfiles, const char **files)
168 register int bytes_read, i, ret = 0, mode;
170 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
172 mode = O_WRONLY | O_CREAT;
178 /* Move all the names `up' one in the argv array to make room for
179 the entry for standard output. This writes into argv[argc]. */
180 for (i = nfiles; i >= 1; i--)
181 files[i] = files[i - 1];
183 /* In the array of NFILES + 1 descriptors, make
184 the first one correspond to standard output. */
186 files[0] = _("standard output");
188 for (i = 1; i <= nfiles; i++)
190 descriptors[i] = open (files[i], mode, 0666);
191 if (descriptors[i] == -1)
193 error (0, errno, "%s", files[i]);
200 bytes_read = read (0, buffer, sizeof buffer);
202 if (bytes_read < 0 && errno == EINTR)
208 /* Write to all NFILES + 1 descriptors.
209 Standard output is the first one. */
210 for (i = 0; i <= nfiles; i++)
212 if (descriptors[i] != -1
213 && full_write (descriptors[i], buffer, bytes_read) < 0)
215 error (0, errno, "%s", files[i]);
216 /* Don't close stdout. That's done in main. */
217 if (descriptors[i] != 1)
218 close (descriptors[i]);
225 if (bytes_read == -1)
227 error (0, errno, _("read error"));
231 /* Close the files, but not standard output. */
232 for (i = 1; i <= nfiles; i++)
233 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
235 error (0, errno, "%s", files[i]);