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>
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);
91 ignore_interrupts = 0;
93 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
105 ignore_interrupts = 1;
108 case_GETOPT_HELP_CHAR;
110 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
117 if (ignore_interrupts)
120 struct sigaction sigact;
122 sigact.sa_handler = SIG_IGN;
123 sigemptyset (&sigact.sa_mask);
125 sigaction (SIGINT, &sigact, NULL);
126 #else /* !_POSIX_SOURCE */
127 signal (SIGINT, SIG_IGN);
128 #endif /* _POSIX_SOURCE */
131 /* Don't let us be killed if one of the output files is a pipe that
132 doesn't consume all its input. */
135 struct sigaction sigact;
137 sigact.sa_handler = SIG_IGN;
138 sigemptyset (&sigact.sa_mask);
140 sigaction (SIGPIPE, &sigact, NULL);
143 signal (SIGPIPE, SIG_IGN);
146 errs = tee (argc - optind, (const char **) &argv[optind]);
148 error (1, errno, _("standard input"));
155 /* Copy the standard input into each of the NFILES files in FILES
156 and into the standard output.
157 Return 0 if successful, 1 if any errors occur. */
160 tee (int nfiles, const char **files)
164 register int bytes_read, i, ret = 0, mode;
166 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
168 mode = O_WRONLY | O_CREAT;
174 /* Move all the names `up' one in the argv array to make room for
175 the entry for standard output. This writes into argv[argc]. */
176 for (i = nfiles; i >= 1; i--)
177 files[i] = files[i - 1];
179 /* In the array of NFILES + 1 descriptors, make
180 the first one correspond to standard output. */
182 files[0] = _("standard output");
184 for (i = 1; i <= nfiles; i++)
186 descriptors[i] = open (files[i], mode, 0666);
187 if (descriptors[i] == -1)
189 error (0, errno, "%s", files[i]);
196 bytes_read = read (0, buffer, sizeof buffer);
198 if (bytes_read < 0 && errno == EINTR)
204 /* Write to all NFILES + 1 descriptors.
205 Standard output is the first one. */
206 for (i = 0; i <= nfiles; i++)
208 if (descriptors[i] != -1
209 && full_write (descriptors[i], buffer, bytes_read) < 0)
211 error (0, errno, "%s", files[i]);
212 /* Don't close stdout. That's done in main. */
213 if (descriptors[i] != 1)
214 close (descriptors[i]);
221 if (bytes_read == -1)
223 error (0, errno, _("read error"));
227 /* Close the files, but not standard output. */
228 for (i = 1; i <= nfiles; i++)
229 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
231 error (0, errno, "%s", files[i]);