1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 1985, 1990, 1991 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},
73 fprintf (status == 0 ? stdout : stderr, "\
74 Usage: %s [OPTION]... [FILE]...\n\
79 fprintf (stderr, "Try `%s --help' for more information.\n",
85 -a, --append append to the given FILEs, do not overwrite\n\
86 -i, --ignore-interrupts ignore interrupt signals\n\
87 --help display this help and exit\n\
88 --version output version information and exit\n\
102 program_name = argv[0];
104 ignore_interrupts = 0;
106 while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
119 ignore_interrupts = 1;
129 printf ("%s\n", version_string);
136 if (ignore_interrupts)
138 #ifdef _POSIX_VERSION
139 struct sigaction sigact;
141 sigact.sa_handler = SIG_IGN;
142 sigemptyset (&sigact.sa_mask);
144 sigaction (SIGINT, &sigact, NULL);
145 #else /* !_POSIX_VERSION */
146 signal (SIGINT, SIG_IGN);
147 #endif /* _POSIX_VERSION */
150 errs = tee (argc - optind, &argv[optind]);
152 error (1, errno, "standard input");
154 error (1, errno, "standard output");
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. */
169 register int bytes_read, i, ret = 0, mode;
172 descriptors = (int *) xmalloc (nfiles * sizeof (int));
173 mode = O_WRONLY | O_CREAT;
179 for (i = 0; i < nfiles; i++)
181 descriptors[i] = open (files[i], mode, 0666);
182 if (descriptors[i] == -1)
184 error (0, errno, "%s", files[i]);
189 while ((bytes_read = read (0, buffer, sizeof buffer)) > 0)
191 xwrite (1, buffer, bytes_read);
192 for (i = 0; i < nfiles; i++)
193 if (descriptors[i] != -1)
194 xwrite (descriptors[i], buffer, bytes_read);
196 if (bytes_read == -1)
198 error (0, errno, "read error");
202 for (i = 0; i < nfiles; i++)
203 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
205 error (0, errno, "%s", files[i]);