1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85, 90, 91, 92, 93, 1994 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);
71 -a, --append append to the given FILEs, do not overwrite\n\
72 -i, --ignore-interrupts ignore interrupt signals\n\
73 --help display this help and exit\n\
74 --version output version information and exit\n\
88 program_name = argv[0];
90 ignore_interrupts = 0;
92 while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
105 ignore_interrupts = 1;
115 printf ("tee - %s\n", version_string);
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 errs = tee (argc - optind, (const char **) &argv[optind]);
138 error (1, errno, "standard input");
140 error (1, errno, "standard output");
144 /* Copy the standard input into each of the NFILES files in FILES
145 and into the standard output.
146 Return 0 if successful, 1 if any errors occur. */
155 register int bytes_read, i, ret = 0, mode;
157 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
159 mode = O_WRONLY | O_CREAT;
165 /* Move all the names `up' one in the argv array to make room for
166 the entry for standard output. This writes into argv[argc]. */
167 for (i = nfiles; i >= 1; i--)
168 files[i] = files[i - 1];
170 /* In the array of NFILES + 1 descriptors, make
171 the first one correspond to standard output. */
173 files[0] = "standard output";
175 for (i = 1; i <= nfiles; i++)
177 descriptors[i] = open (files[i], mode, 0666);
178 if (descriptors[i] == -1)
180 error (0, errno, "%s", files[i]);
187 bytes_read = read (0, buffer, sizeof buffer);
189 if (bytes_read < 0 && errno == EINTR)
195 /* Write to all NFILES + 1 descriptors.
196 Standard output is the first one. */
197 for (i = 0; i <= nfiles; i++)
199 if (descriptors[i] != -1
200 && full_write (descriptors[i], buffer, bytes_read) < 0)
202 error (0, errno, "%s", files[i]);
203 /* Don't close stdout. That's done in main. */
204 if (descriptors[i] != 1)
205 close (descriptors[i]);
212 if (bytes_read == -1)
214 error (0, errno, "read error");
218 /* Close the files, but not standard output. */
219 for (i = 1; i <= nfiles; i++)
220 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
222 error (0, errno, "%s", files[i]);