.
[coreutils.git] / src / tee.c
blobcda83fec0733b819fbed33e56cad4d014ef2e8e4
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)
7 any later version.
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 */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <signal.h>
24 #include <getopt.h>
26 #include "system.h"
27 #include "error.h"
29 char *xmalloc ();
30 int full_write ();
32 static int tee __P ((int nfiles, const char **files));
34 /* If nonzero, append to output files rather than truncating them. */
35 static int append;
37 /* If nonzero, ignore interrupts. */
38 static int ignore_interrupts;
40 /* The name that this program was run with. */
41 char *program_name;
43 /* If nonzero, display usage information and exit. */
44 static int show_help;
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},
55 {NULL, 0, NULL, 0}
58 static void
59 usage (int status)
61 if (status != 0)
62 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 program_name);
64 else
66 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
67 printf (_("\
68 Copy standard input to each FILE, and also to standard output.\n\
69 \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\
74 "));
75 puts (_("\nReport bugs to sh-utils-bugs@gnu.ai.mit.edu"));
77 exit (status);
80 int
81 main (int argc, char **argv)
83 int errs;
84 int optc;
86 program_name = argv[0];
87 setlocale (LC_ALL, "");
88 bindtextdomain (PACKAGE, LOCALEDIR);
89 textdomain (PACKAGE);
91 append = 0;
92 ignore_interrupts = 0;
94 while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
95 != EOF)
97 switch (optc)
99 case 0:
100 break;
102 case 'a':
103 append = 1;
104 break;
106 case 'i':
107 ignore_interrupts = 1;
108 break;
110 default:
111 usage (1);
115 if (show_version)
117 printf ("tee (%s) %s\n", GNU_PACKAGE, VERSION);
118 exit (0);
121 if (show_help)
122 usage (0);
124 if (ignore_interrupts)
126 #ifdef _POSIX_SOURCE
127 struct sigaction sigact;
129 sigact.sa_handler = SIG_IGN;
130 sigemptyset (&sigact.sa_mask);
131 sigact.sa_flags = 0;
132 sigaction (SIGINT, &sigact, NULL);
133 #else /* !_POSIX_SOURCE */
134 signal (SIGINT, SIG_IGN);
135 #endif /* _POSIX_SOURCE */
138 /* Don't let us be killed if one of the output files is a pipe that
139 doesn't consume all its input. */
140 #ifdef _POSIX_SOURCE
142 struct sigaction sigact;
144 sigact.sa_handler = SIG_IGN;
145 sigemptyset (&sigact.sa_mask);
146 sigact.sa_flags = 0;
147 sigaction (SIGPIPE, &sigact, NULL);
149 #else
150 signal (SIGPIPE, SIG_IGN);
151 #endif
153 errs = tee (argc - optind, (const char **) &argv[optind]);
154 if (close (0) != 0)
155 error (1, errno, _("standard input"));
156 if (close (1) != 0)
157 error (1, errno, _("standard output"));
158 exit (errs);
161 /* Copy the standard input into each of the NFILES files in FILES
162 and into the standard output.
163 Return 0 if successful, 1 if any errors occur. */
165 static int
166 tee (int nfiles, const char **files)
168 int *descriptors;
169 char buffer[BUFSIZ];
170 register int bytes_read, i, ret = 0, mode;
172 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
174 mode = O_WRONLY | O_CREAT;
175 if (append)
176 mode |= O_APPEND;
177 else
178 mode |= O_TRUNC;
180 /* Move all the names `up' one in the argv array to make room for
181 the entry for standard output. This writes into argv[argc]. */
182 for (i = nfiles; i >= 1; i--)
183 files[i] = files[i - 1];
185 /* In the array of NFILES + 1 descriptors, make
186 the first one correspond to standard output. */
187 descriptors[0] = 1;
188 files[0] = _("standard output");
190 for (i = 1; i <= nfiles; i++)
192 descriptors[i] = open (files[i], mode, 0666);
193 if (descriptors[i] == -1)
195 error (0, errno, "%s", files[i]);
196 ret = 1;
200 while (1)
202 bytes_read = read (0, buffer, sizeof buffer);
203 #ifdef EINTR
204 if (bytes_read < 0 && errno == EINTR)
205 continue;
206 #endif
207 if (bytes_read <= 0)
208 break;
210 /* Write to all NFILES + 1 descriptors.
211 Standard output is the first one. */
212 for (i = 0; i <= nfiles; i++)
214 if (descriptors[i] != -1
215 && full_write (descriptors[i], buffer, bytes_read) < 0)
217 error (0, errno, "%s", files[i]);
218 /* Don't close stdout. That's done in main. */
219 if (descriptors[i] != 1)
220 close (descriptors[i]);
221 descriptors[i] = -1;
222 ret = 1;
227 if (bytes_read == -1)
229 error (0, errno, _("read error"));
230 ret = 1;
233 /* Close the files, but not standard output. */
234 for (i = 1; i <= nfiles; i++)
235 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
237 error (0, errno, "%s", files[i]);
238 ret = 1;
241 free (descriptors);
243 return ret;