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},
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 ("%s\n", version_string);
132 if (ignore_interrupts)
134 #ifdef _POSIX_VERSION
135 struct sigaction sigact;
137 sigact.sa_handler = SIG_IGN;
138 sigemptyset (&sigact.sa_mask);
140 sigaction (SIGINT, &sigact, NULL);
141 #else /* !_POSIX_VERSION */
142 signal (SIGINT, SIG_IGN);
143 #endif /* _POSIX_VERSION */
146 errs = tee (argc - optind, &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;
168 descriptors = (int *) xmalloc (nfiles * sizeof (int));
169 mode = O_WRONLY | O_CREAT;
175 for (i = 0; i < nfiles; i++)
177 descriptors[i] = open (files[i], mode, 0666);
178 if (descriptors[i] == -1)
180 error (0, errno, "%s", files[i]);
185 while ((bytes_read = read (0, buffer, sizeof buffer)) > 0)
187 xwrite (1, buffer, bytes_read);
188 for (i = 0; i < nfiles; i++)
189 if (descriptors[i] != -1)
190 xwrite (descriptors[i], buffer, bytes_read);
192 if (bytes_read == -1)
194 error (0, errno, "read error");
198 for (i = 0; i < nfiles; i++)
199 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
201 error (0, errno, "%s", files[i]);