1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85, 90, 91, 92, 93, 94, 95, 1996 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Mike Parker, Richard M. Stallman, and David MacKenzie */
22 #include <sys/types.h>
32 static int tee __P ((int nfiles, const char **files));
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 nonzero, display usage information and exit. */
46 /* If nonzero, 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 fprintf (stderr, _("Try `%s --help' for more information.\n"),
66 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
68 Copy standard input to each FILE, and also to standard output.\n\
70 -a, --append append to the given FILEs, do not overwrite\n\
71 -i, --ignore-interrupts ignore interrupt signals\n\
72 --help display this help and exit\n\
73 --version output version information and exit\n\
80 main (int argc, char **argv)
85 program_name = argv[0];
86 setlocale (LC_ALL, "");
87 bindtextdomain (PACKAGE, LOCALEDIR);
91 ignore_interrupts = 0;
93 while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
106 ignore_interrupts = 1;
116 printf ("tee - %s\n", PACKAGE_VERSION);
123 if (ignore_interrupts)
126 struct sigaction sigact;
128 sigact.sa_handler = SIG_IGN;
129 sigemptyset (&sigact.sa_mask);
131 sigaction (SIGINT, &sigact, NULL);
132 #else /* !_POSIX_SOURCE */
133 signal (SIGINT, SIG_IGN);
134 #endif /* _POSIX_SOURCE */
137 errs = tee (argc - optind, (const char **) &argv[optind]);
139 error (1, errno, _("standard input"));
141 error (1, errno, _("standard output"));
145 /* Copy the standard input into each of the NFILES files in FILES
146 and into the standard output.
147 Return 0 if successful, 1 if any errors occur. */
150 tee (int nfiles, const char **files)
154 register int bytes_read, i, ret = 0, mode;
156 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
158 mode = O_WRONLY | O_CREAT;
164 /* Move all the names `up' one in the argv array to make room for
165 the entry for standard output. This writes into argv[argc]. */
166 for (i = nfiles; i >= 1; i--)
167 files[i] = files[i - 1];
169 /* In the array of NFILES + 1 descriptors, make
170 the first one correspond to standard output. */
172 files[0] = _("standard output");
174 for (i = 1; i <= nfiles; i++)
176 descriptors[i] = open (files[i], mode, 0666);
177 if (descriptors[i] == -1)
179 error (0, errno, "%s", files[i]);
186 bytes_read = read (0, buffer, sizeof buffer);
188 if (bytes_read < 0 && errno == EINTR)
194 /* Write to all NFILES + 1 descriptors.
195 Standard output is the first one. */
196 for (i = 0; i <= nfiles; i++)
198 if (descriptors[i] != -1
199 && full_write (descriptors[i], buffer, bytes_read) < 0)
201 error (0, errno, "%s", files[i]);
202 /* Don't close stdout. That's done in main. */
203 if (descriptors[i] != 1)
204 close (descriptors[i]);
211 if (bytes_read == -1)
213 error (0, errno, _("read error"));
217 /* Close the files, but not standard output. */
218 for (i = 1; i <= nfiles; i++)
219 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
221 error (0, errno, "%s", files[i]);