maint: move two small functions, so we can remove a fwd decl
[coreutils.git] / src / tee.c
blob0518b07cfaa7a94be565e20c712906b9f332e82f
1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 1985, 1990-2006, 2008-2011 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
17 /* Mike Parker, Richard M. Stallman, and David MacKenzie */
19 #include <config.h>
20 #include <sys/types.h>
21 #include <signal.h>
22 #include <getopt.h>
24 #include "system.h"
25 #include "error.h"
26 #include "fadvise.h"
27 #include "stdio--.h"
28 #include "xfreopen.h"
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "tee"
33 #define AUTHORS \
34 proper_name ("Mike Parker"), \
35 proper_name ("Richard M. Stallman"), \
36 proper_name ("David MacKenzie")
38 static bool tee_files (int nfiles, const char **files);
40 /* If true, append to output files rather than truncating them. */
41 static bool append;
43 /* If true, ignore interrupts. */
44 static bool ignore_interrupts;
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 != EXIT_SUCCESS)
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 fputs (_("\
73 \n\
74 If a FILE is -, copy again to standard output.\n\
75 "), stdout);
76 emit_ancillary_info ();
78 exit (status);
81 int
82 main (int argc, char **argv)
84 bool ok;
85 int optc;
87 initialize_main (&argc, &argv);
88 set_program_name (argv[0]);
89 setlocale (LC_ALL, "");
90 bindtextdomain (PACKAGE, LOCALEDIR);
91 textdomain (PACKAGE);
93 atexit (close_stdout);
95 append = false;
96 ignore_interrupts = false;
98 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
100 switch (optc)
102 case 'a':
103 append = true;
104 break;
106 case 'i':
107 ignore_interrupts = true;
108 break;
110 case_GETOPT_HELP_CHAR;
112 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
114 default:
115 usage (EXIT_FAILURE);
119 if (ignore_interrupts)
120 signal (SIGINT, SIG_IGN);
122 /* Do *not* warn if tee is given no file arguments.
123 POSIX requires that it work when given no arguments. */
125 ok = tee_files (argc - optind, (const char **) &argv[optind]);
126 if (close (STDIN_FILENO) != 0)
127 error (EXIT_FAILURE, errno, _("standard input"));
129 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
132 /* Copy the standard input into each of the NFILES files in FILES
133 and into the standard output.
134 Return true if successful. */
136 static bool
137 tee_files (int nfiles, const char **files)
139 FILE **descriptors;
140 char buffer[BUFSIZ];
141 ssize_t bytes_read;
142 int i;
143 bool ok = true;
144 char const *mode_string =
145 (O_BINARY
146 ? (append ? "ab" : "wb")
147 : (append ? "a" : "w"));
149 descriptors = xnmalloc (nfiles + 1, sizeof *descriptors);
151 /* Move all the names `up' one in the argv array to make room for
152 the entry for standard output. This writes into argv[argc]. */
153 for (i = nfiles; i >= 1; i--)
154 files[i] = files[i - 1];
156 if (O_BINARY && ! isatty (STDIN_FILENO))
157 xfreopen (NULL, "rb", stdin);
158 if (O_BINARY && ! isatty (STDOUT_FILENO))
159 xfreopen (NULL, "wb", stdout);
161 fadvise (stdin, FADVISE_SEQUENTIAL);
163 /* In the array of NFILES + 1 descriptors, make
164 the first one correspond to standard output. */
165 descriptors[0] = stdout;
166 files[0] = _("standard output");
167 setvbuf (stdout, NULL, _IONBF, 0);
169 for (i = 1; i <= nfiles; i++)
171 descriptors[i] = (STREQ (files[i], "-")
172 ? stdout
173 : fopen (files[i], mode_string));
174 if (descriptors[i] == NULL)
176 error (0, errno, "%s", files[i]);
177 ok = false;
179 else
180 setvbuf (descriptors[i], NULL, _IONBF, 0);
183 while (1)
185 bytes_read = read (0, buffer, sizeof buffer);
186 if (bytes_read < 0 && errno == EINTR)
187 continue;
188 if (bytes_read <= 0)
189 break;
191 /* Write to all NFILES + 1 descriptors.
192 Standard output is the first one. */
193 for (i = 0; i <= nfiles; i++)
194 if (descriptors[i]
195 && fwrite (buffer, bytes_read, 1, descriptors[i]) != 1)
197 error (0, errno, "%s", files[i]);
198 descriptors[i] = NULL;
199 ok = false;
203 if (bytes_read == -1)
205 error (0, errno, _("read error"));
206 ok = false;
209 /* Close the files, but not standard output. */
210 for (i = 1; i <= nfiles; i++)
211 if (!STREQ (files[i], "-")
212 && descriptors[i] && fclose (descriptors[i]) != 0)
214 error (0, errno, "%s", files[i]);
215 ok = false;
218 free (descriptors);
220 return ok;