*** empty log message ***
[coreutils.git] / src / tee.c
blobc71797a47f376df7522c566f53c01f0f2a8a6caf
1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-2001 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 "closeout.h"
28 #include "error.h"
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "tee"
33 #define AUTHORS N_ ("Mike Parker, Richard M. Stallman, and David MacKenzie")
35 static int tee PARAMS ((int nfiles, const char **files));
37 /* If nonzero, append to output files rather than truncating them. */
38 static int append;
40 /* If nonzero, ignore interrupts. */
41 static int ignore_interrupts;
43 /* The name that this program was run with. */
44 char *program_name;
46 static struct option const long_options[] =
48 {"append", no_argument, NULL, 'a'},
49 {"ignore-interrupts", no_argument, NULL, 'i'},
50 {GETOPT_HELP_OPTION_DECL},
51 {GETOPT_VERSION_OPTION_DECL},
52 {NULL, 0, NULL, 0}
55 void
56 usage (int status)
58 if (status != 0)
59 fprintf (stderr, _("Try `%s --help' for more information.\n"),
60 program_name);
61 else
63 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
64 fputs (_("\
65 Copy standard input to each FILE, and also to standard output.\n\
66 \n\
67 -a, --append append to the given FILEs, do not overwrite\n\
68 -i, --ignore-interrupts ignore interrupt signals\n\
69 "), stdout);
70 fputs (HELP_OPTION_DESCRIPTION, stdout);
71 fputs (VERSION_OPTION_DESCRIPTION, stdout);
72 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
74 exit (status);
77 int
78 main (int argc, char **argv)
80 int errs;
81 int optc;
83 program_name = argv[0];
84 setlocale (LC_ALL, "");
85 bindtextdomain (PACKAGE, LOCALEDIR);
86 textdomain (PACKAGE);
88 atexit (close_stdout);
90 append = 0;
91 ignore_interrupts = 0;
93 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
95 switch (optc)
97 case 0:
98 break;
100 case 'a':
101 append = 1;
102 break;
104 case 'i':
105 ignore_interrupts = 1;
106 break;
108 case_GETOPT_HELP_CHAR;
110 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
112 default:
113 usage (1);
117 if (ignore_interrupts)
119 #ifdef _POSIX_SOURCE
120 struct sigaction sigact;
122 sigact.sa_handler = SIG_IGN;
123 sigemptyset (&sigact.sa_mask);
124 sigact.sa_flags = 0;
125 sigaction (SIGINT, &sigact, NULL);
126 #else /* !_POSIX_SOURCE */
127 signal (SIGINT, SIG_IGN);
128 #endif /* _POSIX_SOURCE */
131 /* Don't let us be killed if one of the output files is a pipe that
132 doesn't consume all its input. */
133 #ifdef _POSIX_SOURCE
135 struct sigaction sigact;
137 sigact.sa_handler = SIG_IGN;
138 sigemptyset (&sigact.sa_mask);
139 sigact.sa_flags = 0;
140 sigaction (SIGPIPE, &sigact, NULL);
142 #else
143 signal (SIGPIPE, SIG_IGN);
144 #endif
146 /* Do *not* warn if tee is given no file arguments.
147 POSIX requires that it work when given no arguments. */
149 errs = tee (argc - optind, (const char **) &argv[optind]);
150 if (close (0) != 0)
151 error (1, errno, _("standard input"));
153 exit (errs);
156 /* Copy the standard input into each of the NFILES files in FILES
157 and into the standard output.
158 Return 0 if successful, 1 if any errors occur. */
160 static int
161 tee (int nfiles, const char **files)
163 FILE **descriptors;
164 char buffer[BUFSIZ];
165 int bytes_read, i;
166 int ret = 0;
167 const char *mode_string = (append ? "a" : "w");
169 descriptors = (FILE **) xmalloc ((nfiles + 1) * sizeof (descriptors[0]));
171 /* Move all the names `up' one in the argv array to make room for
172 the entry for standard output. This writes into argv[argc]. */
173 for (i = nfiles; i >= 1; i--)
174 files[i] = files[i - 1];
176 SET_BINARY2 (0, 1);
178 /* In the array of NFILES + 1 descriptors, make
179 the first one correspond to standard output. */
180 descriptors[0] = stdout;
181 files[0] = _("standard output");
182 SETVBUF (stdout, NULL, _IONBF, 0);
184 for (i = 1; i <= nfiles; i++)
186 descriptors[i] = fopen (files[i], mode_string);
187 if (descriptors[i] == NULL)
189 error (0, errno, "%s", files[i]);
190 ret = 1;
192 else
194 SETVBUF (descriptors[i], NULL, _IONBF, 0);
195 SET_BINARY (fileno (descriptors[i]));
199 while (1)
201 bytes_read = read (0, buffer, sizeof buffer);
202 #ifdef EINTR
203 if (bytes_read < 0 && errno == EINTR)
204 continue;
205 #endif
206 if (bytes_read <= 0)
207 break;
209 /* Write to all NFILES + 1 descriptors.
210 Standard output is the first one. */
211 for (i = 0; i <= nfiles; i++)
213 if (descriptors[i] != NULL)
214 fwrite (buffer, bytes_read, 1, descriptors[i]);
218 if (bytes_read == -1)
220 error (0, errno, _("read error"));
221 ret = 1;
224 /* Close the files, but not standard output. */
225 for (i = 1; i <= nfiles; i++)
226 if (descriptors[i] != NULL
227 && (ferror (descriptors[i]) || fclose (descriptors[i]) == EOF))
229 error (0, errno, "%s", files[i]);
230 ret = 1;
233 free (descriptors);
235 return ret;