*** empty log message ***
[coreutils.git] / src / tee.c
blob4eb3585b08453f2917da13d798c79f7c7e42e075
1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-1999 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 "Mike Parker, Richard M. Stallman, and David MacKenzie"
35 int full_write ();
37 static int tee PARAMS ((int nfiles, const char **files));
39 /* If nonzero, append to output files rather than truncating them. */
40 static int append;
42 /* If nonzero, ignore interrupts. */
43 static int ignore_interrupts;
45 /* The name that this program was run with. */
46 char *program_name;
48 static struct option const long_options[] =
50 {"append", no_argument, NULL, 'a'},
51 {"ignore-interrupts", no_argument, NULL, 'i'},
52 {GETOPT_HELP_OPTION_DECL},
53 {GETOPT_VERSION_OPTION_DECL},
54 {NULL, 0, NULL, 0}
57 void
58 usage (int status)
60 if (status != 0)
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
62 program_name);
63 else
65 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
66 printf (_("\
67 Copy standard input to each FILE, and also to standard output.\n\
68 \n\
69 -a, --append append to the given FILEs, do not overwrite\n\
70 -i, --ignore-interrupts ignore interrupt signals\n\
71 --help display this help and exit\n\
72 --version output version information and exit\n\
73 "));
74 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
76 exit (status);
79 int
80 main (int argc, char **argv)
82 int errs;
83 int optc;
85 program_name = argv[0];
86 setlocale (LC_ALL, "");
87 bindtextdomain (PACKAGE, LOCALEDIR);
88 textdomain (PACKAGE);
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 /* FIXME: warn if tee is given no file arguments.
147 In that case it's just a slow imitation of `cat.' */
149 errs = tee (argc - optind, (const char **) &argv[optind]);
150 if (close (0) != 0)
151 error (1, errno, _("standard input"));
153 close_stdout ();
155 exit (errs);
158 /* Copy the standard input into each of the NFILES files in FILES
159 and into the standard output.
160 Return 0 if successful, 1 if any errors occur. */
162 static int
163 tee (int nfiles, const char **files)
165 FILE **descriptors;
166 char buffer[BUFSIZ];
167 int bytes_read, i;
168 int ret = 0;
169 const char *mode_string = (append ? "a" : "w");
171 descriptors = (FILE **) xmalloc ((nfiles + 1) * sizeof (descriptors[0]));
173 /* Move all the names `up' one in the argv array to make room for
174 the entry for standard output. This writes into argv[argc]. */
175 for (i = nfiles; i >= 1; i--)
176 files[i] = files[i - 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);
198 while (1)
200 bytes_read = read (0, buffer, sizeof buffer);
201 #ifdef EINTR
202 if (bytes_read < 0 && errno == EINTR)
203 continue;
204 #endif
205 if (bytes_read <= 0)
206 break;
208 /* Write to all NFILES + 1 descriptors.
209 Standard output is the first one. */
210 for (i = 0; i <= nfiles; i++)
212 if (descriptors[i] != NULL)
213 fwrite (buffer, bytes_read, 1, descriptors[i]);
217 if (bytes_read == -1)
219 error (0, errno, _("read error"));
220 ret = 1;
223 /* Close the files, but not standard output. */
224 for (i = 1; i <= nfiles; i++)
225 if (descriptors[i] != NULL
226 && (ferror (descriptors[i]) || fclose (descriptors[i]) == EOF))
228 error (0, errno, "%s", files[i]);
229 ret = 1;
232 free (descriptors);
234 return ret;