(jm_FILE_SYSTEM_USAGE): Add `[]' between use of
[coreutils.git] / src / tee.c
blob48c42d1735477f2d9c087100c413aa86d5e49525
1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-2000 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 atexit (close_stdout);
92 append = 0;
93 ignore_interrupts = 0;
95 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
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 case_GETOPT_HELP_CHAR;
112 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
114 default:
115 usage (1);
119 if (ignore_interrupts)
121 #ifdef _POSIX_SOURCE
122 struct sigaction sigact;
124 sigact.sa_handler = SIG_IGN;
125 sigemptyset (&sigact.sa_mask);
126 sigact.sa_flags = 0;
127 sigaction (SIGINT, &sigact, NULL);
128 #else /* !_POSIX_SOURCE */
129 signal (SIGINT, SIG_IGN);
130 #endif /* _POSIX_SOURCE */
133 /* Don't let us be killed if one of the output files is a pipe that
134 doesn't consume all its input. */
135 #ifdef _POSIX_SOURCE
137 struct sigaction sigact;
139 sigact.sa_handler = SIG_IGN;
140 sigemptyset (&sigact.sa_mask);
141 sigact.sa_flags = 0;
142 sigaction (SIGPIPE, &sigact, NULL);
144 #else
145 signal (SIGPIPE, SIG_IGN);
146 #endif
148 /* Do *not* warn if tee is given no file arguments.
149 POSIX requires that it work when given no arguments. */
151 errs = tee (argc - optind, (const char **) &argv[optind]);
152 if (close (0) != 0)
153 error (1, errno, _("standard input"));
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 SET_BINARY2 (0, 1);
180 /* In the array of NFILES + 1 descriptors, make
181 the first one correspond to standard output. */
182 descriptors[0] = stdout;
183 files[0] = _("standard output");
184 SETVBUF (stdout, NULL, _IONBF, 0);
186 for (i = 1; i <= nfiles; i++)
188 descriptors[i] = fopen (files[i], mode_string);
189 if (descriptors[i] == NULL)
191 error (0, errno, "%s", files[i]);
192 ret = 1;
194 else
196 SETVBUF (descriptors[i], NULL, _IONBF, 0);
197 SET_BINARY (fileno (descriptors[i]));
201 while (1)
203 bytes_read = read (0, buffer, sizeof buffer);
204 #ifdef EINTR
205 if (bytes_read < 0 && errno == EINTR)
206 continue;
207 #endif
208 if (bytes_read <= 0)
209 break;
211 /* Write to all NFILES + 1 descriptors.
212 Standard output is the first one. */
213 for (i = 0; i <= nfiles; i++)
215 if (descriptors[i] != NULL)
216 fwrite (buffer, bytes_read, 1, descriptors[i]);
220 if (bytes_read == -1)
222 error (0, errno, _("read error"));
223 ret = 1;
226 /* Close the files, but not standard output. */
227 for (i = 1; i <= nfiles; i++)
228 if (descriptors[i] != NULL
229 && (ferror (descriptors[i]) || fclose (descriptors[i]) == EOF))
231 error (0, errno, "%s", files[i]);
232 ret = 1;
235 free (descriptors);
237 return ret;