1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85, 90, 91, 92, 93, 94, 1995 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Mike Parker, Richard M. Stallman, and David MacKenzie */
22 #include <sys/types.h>
35 /* If nonzero, append to output files rather than truncating them. */
38 /* If nonzero, ignore interrupts. */
39 static int ignore_interrupts;
41 /* The name that this program was run with. */
44 /* If non-zero, display usage information and exit. */
47 /* If non-zero, print the version on standard output and exit. */
48 static int show_version;
50 static struct option const long_options[] =
52 {"append", no_argument, NULL, 'a'},
53 {"help", no_argument, &show_help, 1},
54 {"ignore-interrupts", no_argument, NULL, 'i'},
55 {"version", no_argument, &show_version, 1},
64 fprintf (stderr, _("Try `%s --help' for more information.\n"),
68 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
70 Copy standard input to each FILE, and also to standard output.\n\
72 -a, --append append to the given FILEs, do not overwrite\n\
73 -i, --ignore-interrupts ignore interrupt signals\n\
74 --help display this help and exit\n\
75 --version output version information and exit\n\
89 program_name = argv[0];
91 ignore_interrupts = 0;
93 while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
106 ignore_interrupts = 1;
116 printf ("tee - %s\n", version_string);
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 errs = tee (argc - optind, (const char **) &argv[optind]);
139 error (1, errno, _("standard input"));
141 error (1, errno, _("standard output"));
145 /* Copy the standard input into each of the NFILES files in FILES
146 and into the standard output.
147 Return 0 if successful, 1 if any errors occur. */
156 register int bytes_read, i, ret = 0, mode;
158 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
160 mode = O_WRONLY | O_CREAT;
166 /* Move all the names `up' one in the argv array to make room for
167 the entry for standard output. This writes into argv[argc]. */
168 for (i = nfiles; i >= 1; i--)
169 files[i] = files[i - 1];
171 /* In the array of NFILES + 1 descriptors, make
172 the first one correspond to standard output. */
174 files[0] = _("standard output");
176 for (i = 1; i <= nfiles; i++)
178 descriptors[i] = open (files[i], mode, 0666);
179 if (descriptors[i] == -1)
181 error (0, errno, "%s", files[i]);
188 bytes_read = read (0, buffer, sizeof buffer);
190 if (bytes_read < 0 && errno == EINTR)
196 /* Write to all NFILES + 1 descriptors.
197 Standard output is the first one. */
198 for (i = 0; i <= nfiles; i++)
200 if (descriptors[i] != -1
201 && full_write (descriptors[i], buffer, bytes_read) < 0)
203 error (0, errno, "%s", files[i]);
204 /* Don't close stdout. That's done in main. */
205 if (descriptors[i] != 1)
206 close (descriptors[i]);
213 if (bytes_read == -1)
215 error (0, errno, _("read error"));
219 /* Close the files, but not standard output. */
220 for (i = 1; i <= nfiles; i++)
221 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
223 error (0, errno, "%s", files[i]);