Sanitize environment.
[coreutils.git] / src / tee.c
blob3639366b2f8d44cde4b10e11e16be16f4b4db2f5
1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-2004 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 "error.h"
29 /* The official name of this program (e.g., no `g' prefix). */
30 #define PROGRAM_NAME "tee"
32 #define AUTHORS "Mike Parker", "Richard M. Stallman", "David MacKenzie"
34 static bool tee (int nfiles, const char **files);
36 /* If true, append to output files rather than truncating them. */
37 static bool append;
39 /* If true, ignore interrupts. */
40 static bool ignore_interrupts;
42 /* The name that this program was run with. */
43 char *program_name;
45 static struct option const long_options[] =
47 {"append", no_argument, NULL, 'a'},
48 {"ignore-interrupts", no_argument, NULL, 'i'},
49 {GETOPT_HELP_OPTION_DECL},
50 {GETOPT_VERSION_OPTION_DECL},
51 {NULL, 0, NULL, 0}
54 void
55 usage (int status)
57 if (status != EXIT_SUCCESS)
58 fprintf (stderr, _("Try `%s --help' for more information.\n"),
59 program_name);
60 else
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 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
77 exit (status);
80 int
81 main (int argc, char **argv)
83 bool ok;
84 int optc;
86 initialize_main (&argc, &argv);
87 program_name = argv[0];
88 setlocale (LC_ALL, "");
89 bindtextdomain (PACKAGE, LOCALEDIR);
90 textdomain (PACKAGE);
92 atexit (close_stdout);
94 append = false;
95 ignore_interrupts = false;
97 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
99 switch (optc)
101 case 'a':
102 append = true;
103 break;
105 case 'i':
106 ignore_interrupts = true;
107 break;
109 case_GETOPT_HELP_CHAR;
111 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
113 default:
114 usage (EXIT_FAILURE);
118 if (ignore_interrupts)
119 signal (SIGINT, SIG_IGN);
121 /* Do *not* warn if tee is given no file arguments.
122 POSIX requires that it work when given no arguments. */
124 ok = tee (argc - optind, (const char **) &argv[optind]);
125 if (close (STDIN_FILENO) != 0)
126 error (EXIT_FAILURE, errno, _("standard input"));
128 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
131 /* Copy the standard input into each of the NFILES files in FILES
132 and into the standard output.
133 Return true if successful. */
135 static bool
136 tee (int nfiles, const char **files)
138 FILE **descriptors;
139 char buffer[BUFSIZ];
140 ssize_t bytes_read;
141 int i;
142 bool ok = true;
143 const char *mode_string = (append ? "a" : "w");
145 descriptors = xnmalloc (nfiles + 1, sizeof *descriptors);
147 /* Move all the names `up' one in the argv array to make room for
148 the entry for standard output. This writes into argv[argc]. */
149 for (i = nfiles; i >= 1; i--)
150 files[i] = files[i - 1];
152 SET_BINARY2 (0, 1);
154 /* In the array of NFILES + 1 descriptors, make
155 the first one correspond to standard output. */
156 descriptors[0] = stdout;
157 files[0] = _("standard output");
158 SETVBUF (stdout, NULL, _IONBF, 0);
160 for (i = 1; i <= nfiles; i++)
162 descriptors[i] = (STREQ (files[i], "-")
163 ? stdout
164 : fopen (files[i], mode_string));
165 if (descriptors[i] == NULL)
167 error (0, errno, "%s", files[i]);
168 ok = false;
170 else
172 SETVBUF (descriptors[i], NULL, _IONBF, 0);
173 SET_BINARY (fileno (descriptors[i]));
177 while (1)
179 bytes_read = read (0, buffer, sizeof buffer);
180 #ifdef EINTR
181 if (bytes_read < 0 && errno == EINTR)
182 continue;
183 #endif
184 if (bytes_read <= 0)
185 break;
187 /* Write to all NFILES + 1 descriptors.
188 Standard output is the first one. */
189 for (i = 0; i <= nfiles; i++)
190 if (descriptors[i]
191 && fwrite (buffer, 1, bytes_read, descriptors[i]) != bytes_read)
193 error (0, errno, "%s", files[i]);
194 descriptors[i] = NULL;
195 ok = false;
199 if (bytes_read == -1)
201 error (0, errno, _("read error"));
202 ok = false;
205 /* Close the files, but not standard output. */
206 for (i = 1; i <= nfiles; i++)
207 if (descriptors[i] && fclose (descriptors[i]) != 0)
209 error (0, errno, "%s", files[i]);
210 ok = false;
213 free (descriptors);
215 return ok;