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 #include <sys/types.h>
34 /* If nonzero, append to output files rather than truncating them. */
37 /* If nonzero, ignore interrupts. */
38 static int ignore_interrupts;
40 /* The name that this program was run with. */
43 /* If non-zero, display usage information and exit. */
46 /* If non-zero, print the version on standard output and exit. */
47 static int show_version;
49 static struct option const long_options[] =
51 {"append", no_argument, NULL, 'a'},
52 {"help", no_argument, &show_help, 1},
53 {"ignore-interrupts", no_argument, NULL, 'i'},
54 {"version", no_argument, &show_version, 1},
62 Usage: %s [{--help,--version}] [-ai] [--append]\n\
63 [--ignore-interrupts] [file...]\n",
76 program_name = argv[0];
78 ignore_interrupts = 0;
80 while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
93 ignore_interrupts = 1;
103 printf ("%s\n", version_string);
110 if (ignore_interrupts)
112 #ifdef _POSIX_VERSION
113 struct sigaction sigact;
115 sigact.sa_handler = SIG_IGN;
116 sigemptyset (&sigact.sa_mask);
118 sigaction (SIGINT, &sigact, NULL);
119 #else /* !_POSIX_VERSION */
120 signal (SIGINT, SIG_IGN);
121 #endif /* _POSIX_VERSION */
124 errs = tee (argc - optind, &argv[optind]);
126 error (1, errno, "standard input");
128 error (1, errno, "standard output");
132 /* Copy the standard input into each of the NFILES files in FILES
133 and into the standard output.
134 Return 0 if successful, 1 if any errors occur. */
143 register int bytes_read, i, ret = 0, mode;
146 descriptors = (int *) xmalloc (nfiles * sizeof (int));
147 mode = O_WRONLY | O_CREAT;
153 for (i = 0; i < nfiles; i++)
155 descriptors[i] = open (files[i], mode, 0666);
156 if (descriptors[i] == -1)
158 error (0, errno, "%s", files[i]);
163 while ((bytes_read = read (0, buffer, sizeof buffer)) > 0)
165 xwrite (1, buffer, bytes_read);
166 for (i = 0; i < nfiles; i++)
167 if (descriptors[i] != -1)
168 xwrite (descriptors[i], buffer, bytes_read);
170 if (bytes_read == -1)
172 error (0, errno, "read error");
176 for (i = 0; i < nfiles; i++)
177 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
179 error (0, errno, "%s", files[i]);