maint: update all copyright year number ranges
[platform/upstream/coreutils.git] / src / tee.c
1 /* tee - read from standard input and write to standard output and files.
2    Copyright (C) 1985-2013 Free Software Foundation, Inc.
3
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 3 of the License, or
7    (at your option) any later version.
8
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.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Mike Parker, Richard M. Stallman, and David MacKenzie */
18
19 #include <config.h>
20 #include <sys/types.h>
21 #include <signal.h>
22 #include <getopt.h>
23
24 #include "system.h"
25 #include "error.h"
26 #include "fadvise.h"
27 #include "stdio--.h"
28 #include "xfreopen.h"
29
30 /* The official name of this program (e.g., no 'g' prefix).  */
31 #define PROGRAM_NAME "tee"
32
33 #define AUTHORS \
34   proper_name ("Mike Parker"), \
35   proper_name ("Richard M. Stallman"), \
36   proper_name ("David MacKenzie")
37
38 static bool tee_files (int nfiles, const char **files);
39
40 /* If true, append to output files rather than truncating them. */
41 static bool append;
42
43 /* If true, ignore interrupts. */
44 static bool ignore_interrupts;
45
46 static struct option const long_options[] =
47 {
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}
53 };
54
55 void
56 usage (int status)
57 {
58   if (status != EXIT_SUCCESS)
59     emit_try_help ();
60   else
61     {
62       printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
63       fputs (_("\
64 Copy standard input to each FILE, and also to standard output.\n\
65 \n\
66   -a, --append              append to the given FILEs, do not overwrite\n\
67   -i, --ignore-interrupts   ignore interrupt signals\n\
68 "), stdout);
69       fputs (HELP_OPTION_DESCRIPTION, stdout);
70       fputs (VERSION_OPTION_DESCRIPTION, stdout);
71       fputs (_("\
72 \n\
73 If a FILE is -, copy again to standard output.\n\
74 "), stdout);
75       emit_ancillary_info ();
76     }
77   exit (status);
78 }
79
80 int
81 main (int argc, char **argv)
82 {
83   bool ok;
84   int optc;
85
86   initialize_main (&argc, &argv);
87   set_program_name (argv[0]);
88   setlocale (LC_ALL, "");
89   bindtextdomain (PACKAGE, LOCALEDIR);
90   textdomain (PACKAGE);
91
92   atexit (close_stdout);
93
94   append = false;
95   ignore_interrupts = false;
96
97   while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
98     {
99       switch (optc)
100         {
101         case 'a':
102           append = true;
103           break;
104
105         case 'i':
106           ignore_interrupts = true;
107           break;
108
109         case_GETOPT_HELP_CHAR;
110
111         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
112
113         default:
114           usage (EXIT_FAILURE);
115         }
116     }
117
118   if (ignore_interrupts)
119     signal (SIGINT, SIG_IGN);
120
121   /* Do *not* warn if tee is given no file arguments.
122      POSIX requires that it work when given no arguments.  */
123
124   ok = tee_files (argc - optind, (const char **) &argv[optind]);
125   if (close (STDIN_FILENO) != 0)
126     error (EXIT_FAILURE, errno, _("standard input"));
127
128   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
129 }
130
131 /* Copy the standard input into each of the NFILES files in FILES
132    and into the standard output.
133    Return true if successful.  */
134
135 static bool
136 tee_files (int nfiles, const char **files)
137 {
138   FILE **descriptors;
139   char buffer[BUFSIZ];
140   ssize_t bytes_read;
141   int i;
142   bool ok = true;
143   char const *mode_string =
144     (O_BINARY
145      ? (append ? "ab" : "wb")
146      : (append ? "a" : "w"));
147
148   descriptors = xnmalloc (nfiles + 1, sizeof *descriptors);
149
150   /* Move all the names 'up' one in the argv array to make room for
151      the entry for standard output.  This writes into argv[argc].  */
152   for (i = nfiles; i >= 1; i--)
153     files[i] = files[i - 1];
154
155   if (O_BINARY && ! isatty (STDIN_FILENO))
156     xfreopen (NULL, "rb", stdin);
157   if (O_BINARY && ! isatty (STDOUT_FILENO))
158     xfreopen (NULL, "wb", stdout);
159
160   fadvise (stdin, FADVISE_SEQUENTIAL);
161
162   /* In the array of NFILES + 1 descriptors, make
163      the first one correspond to standard output.   */
164   descriptors[0] = stdout;
165   files[0] = _("standard output");
166   setvbuf (stdout, NULL, _IONBF, 0);
167
168   for (i = 1; i <= nfiles; i++)
169     {
170       descriptors[i] = (STREQ (files[i], "-")
171                         ? stdout
172                         : fopen (files[i], mode_string));
173       if (descriptors[i] == NULL)
174         {
175           error (0, errno, "%s", files[i]);
176           ok = false;
177         }
178       else
179         setvbuf (descriptors[i], NULL, _IONBF, 0);
180     }
181
182   while (1)
183     {
184       bytes_read = read (0, buffer, sizeof buffer);
185       if (bytes_read < 0 && errno == EINTR)
186         continue;
187       if (bytes_read <= 0)
188         break;
189
190       /* Write to all NFILES + 1 descriptors.
191          Standard output is the first one.  */
192       for (i = 0; i <= nfiles; i++)
193         if (descriptors[i]
194             && fwrite (buffer, bytes_read, 1, descriptors[i]) != 1)
195           {
196             error (0, errno, "%s", files[i]);
197             descriptors[i] = NULL;
198             ok = false;
199           }
200     }
201
202   if (bytes_read == -1)
203     {
204       error (0, errno, _("read error"));
205       ok = false;
206     }
207
208   /* Close the files, but not standard output.  */
209   for (i = 1; i <= nfiles; i++)
210     if (!STREQ (files[i], "-")
211         && descriptors[i] && fclose (descriptors[i]) != 0)
212       {
213         error (0, errno, "%s", files[i]);
214         ok = false;
215       }
216
217   free (descriptors);
218
219   return ok;
220 }