1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-2000 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>
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "tee"
33 #define AUTHORS "Mike Parker, Richard M. Stallman, and David MacKenzie"
37 static int tee PARAMS ((int nfiles, const char **files));
39 /* If nonzero, append to output files rather than truncating them. */
42 /* If nonzero, ignore interrupts. */
43 static int ignore_interrupts;
45 /* The name that this program was run with. */
48 static struct option const long_options[] =
50 {"append", no_argument, NULL, 'a'},
51 {"ignore-interrupts", no_argument, NULL, 'i'},
52 {GETOPT_HELP_OPTION_DECL},
53 {GETOPT_VERSION_OPTION_DECL},
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);
90 atexit (close_stdout);
93 ignore_interrupts = 0;
95 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
107 ignore_interrupts = 1;
110 case_GETOPT_HELP_CHAR;
112 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
119 if (ignore_interrupts)
122 struct sigaction sigact;
124 sigact.sa_handler = SIG_IGN;
125 sigemptyset (&sigact.sa_mask);
127 sigaction (SIGINT, &sigact, NULL);
128 #else /* !_POSIX_SOURCE */
129 signal (SIGINT, SIG_IGN);
130 #endif /* _POSIX_SOURCE */
133 /* Don't let us be killed if one of the output files is a pipe that
134 doesn't consume all its input. */
137 struct sigaction sigact;
139 sigact.sa_handler = SIG_IGN;
140 sigemptyset (&sigact.sa_mask);
142 sigaction (SIGPIPE, &sigact, NULL);
145 signal (SIGPIPE, SIG_IGN);
148 /* FIXME: warn if tee is given no file arguments.
149 In that case it's just a slow imitation of `cat.' */
151 errs = tee (argc - optind, (const char **) &argv[optind]);
153 error (1, errno, _("standard input"));
158 /* Copy the standard input into each of the NFILES files in FILES
159 and into the standard output.
160 Return 0 if successful, 1 if any errors occur. */
163 tee (int nfiles, const char **files)
169 const char *mode_string = (append ? "a" : "w");
171 descriptors = (FILE **) xmalloc ((nfiles + 1) * sizeof (descriptors[0]));
173 /* Move all the names `up' one in the argv array to make room for
174 the entry for standard output. This writes into argv[argc]. */
175 for (i = nfiles; i >= 1; i--)
176 files[i] = files[i - 1];
178 /* In the array of NFILES + 1 descriptors, make
179 the first one correspond to standard output. */
180 descriptors[0] = stdout;
181 files[0] = _("standard output");
182 SETVBUF (stdout, NULL, _IONBF, 0);
184 for (i = 1; i <= nfiles; i++)
186 descriptors[i] = fopen (files[i], mode_string);
187 if (descriptors[i] == NULL)
189 error (0, errno, "%s", files[i]);
194 SETVBUF (descriptors[i], NULL, _IONBF, 0);
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] != NULL)
213 fwrite (buffer, bytes_read, 1, descriptors[i]);
217 if (bytes_read == -1)
219 error (0, errno, _("read error"));
223 /* Close the files, but not standard output. */
224 for (i = 1; i <= nfiles; i++)
225 if (descriptors[i] != NULL
226 && (ferror (descriptors[i]) || fclose (descriptors[i]) == EOF))
228 error (0, errno, "%s", files[i]);