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 */
21 #if defined (CONFIG_BROKETS)
22 /* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
32 #include <sys/types.h>
45 /* If nonzero, append to output files rather than truncating them. */
48 /* If nonzero, ignore interrupts. */
49 static int ignore_interrupts;
51 /* The name that this program was run with. */
54 /* If non-zero, display usage information and exit. */
57 /* If non-zero, print the version on standard output and exit. */
58 static int show_version;
60 static struct option const long_options[] =
62 {"append", no_argument, NULL, 'a'},
63 {"help", no_argument, &show_help, 1},
64 {"ignore-interrupts", no_argument, NULL, 'i'},
65 {"version", no_argument, &show_version, 1},
74 fprintf (stderr, "Try `%s --help' for more information.\n",
78 printf ("Usage: %s [OPTION]... [FILE]...\n", program_name);
81 -a, --append append to the given FILEs, do not overwrite\n\
82 -i, --ignore-interrupts ignore interrupt signals\n\
83 --help display this help and exit\n\
84 --version output version information and exit\n\
98 program_name = argv[0];
100 ignore_interrupts = 0;
102 while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
115 ignore_interrupts = 1;
125 printf ("tee - %s\n", version_string);
132 if (ignore_interrupts)
135 struct sigaction sigact;
137 sigact.sa_handler = SIG_IGN;
138 sigemptyset (&sigact.sa_mask);
140 sigaction (SIGINT, &sigact, NULL);
141 #else /* !_POSIX_SOURCE */
142 signal (SIGINT, SIG_IGN);
143 #endif /* _POSIX_SOURCE */
146 errs = tee (argc - optind, (const char **) &argv[optind]);
148 error (1, errno, "standard input");
150 error (1, errno, "standard output");
154 /* Copy the standard input into each of the NFILES files in FILES
155 and into the standard output.
156 Return 0 if successful, 1 if any errors occur. */
165 register int bytes_read, i, ret = 0, mode;
167 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
169 mode = O_WRONLY | O_CREAT;
175 /* Move all the names `up' one in the argv array to make room for
176 the entry for standard output. This writes into argv[argc]. */
177 for (i = nfiles; i >= 1; i--)
178 files[i] = files[i - 1];
180 /* In the array of NFILES + 1 descriptors, make
181 the first one correspond to standard output. */
183 files[0] = "standard output";
185 for (i = 1; i <= nfiles; i++)
187 descriptors[i] = open (files[i], mode, 0666);
188 if (descriptors[i] == -1)
190 error (0, errno, "%s", files[i]);
197 bytes_read = read (0, buffer, sizeof buffer);
199 if (bytes_read < 0 && errno == EINTR)
205 /* Write to all NFILES + 1 descriptors.
206 Standard output is the first one. */
207 for (i = 0; i <= nfiles; i++)
209 if (descriptors[i] != -1
210 && full_write (descriptors[i], buffer, bytes_read) < 0)
212 error (0, errno, "%s", files[i]);
213 /* Don't close stdout. That's done in main. */
214 if (descriptors[i] != 1)
215 close (descriptors[i]);
222 if (bytes_read == -1)
224 error (0, errno, "read error");
228 /* Close the files, but not standard output. */
229 for (i = 1; i <= nfiles; i++)
230 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
232 error (0, errno, "%s", files[i]);