1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-2006 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 */
20 #include <sys/types.h>
28 /* The official name of this program (e.g., no `g' prefix). */
29 #define PROGRAM_NAME "tee"
31 #define AUTHORS "Mike Parker", "Richard M. Stallman", "David MacKenzie"
33 static bool tee_files (int nfiles
, const char **files
);
35 /* If true, append to output files rather than truncating them. */
38 /* If true, ignore interrupts. */
39 static bool ignore_interrupts
;
41 /* The name that this program was run with. */
44 static struct option
const long_options
[] =
46 {"append", no_argument
, NULL
, 'a'},
47 {"ignore-interrupts", no_argument
, NULL
, 'i'},
48 {GETOPT_HELP_OPTION_DECL
},
49 {GETOPT_VERSION_OPTION_DECL
},
56 if (status
!= EXIT_SUCCESS
)
57 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
61 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name
);
63 Copy standard input to each FILE, and also to standard output.\n\
65 -a, --append append to the given FILEs, do not overwrite\n\
66 -i, --ignore-interrupts ignore interrupt signals\n\
68 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
69 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
72 If a FILE is -, copy again to standard output.\n\
74 emit_bug_reporting_address ();
80 main (int argc
, char **argv
)
85 initialize_main (&argc
, &argv
);
86 program_name
= argv
[0];
87 setlocale (LC_ALL
, "");
88 bindtextdomain (PACKAGE
, LOCALEDIR
);
91 atexit (close_stdout
);
94 ignore_interrupts
= false;
96 while ((optc
= getopt_long (argc
, argv
, "ai", long_options
, NULL
)) != -1)
105 ignore_interrupts
= true;
108 case_GETOPT_HELP_CHAR
;
110 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
113 usage (EXIT_FAILURE
);
117 if (ignore_interrupts
)
118 signal (SIGINT
, SIG_IGN
);
120 /* Do *not* warn if tee is given no file arguments.
121 POSIX requires that it work when given no arguments. */
123 ok
= tee_files (argc
- optind
, (const char **) &argv
[optind
]);
124 if (close (STDIN_FILENO
) != 0)
125 error (EXIT_FAILURE
, errno
, _("standard input"));
127 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);
130 /* Copy the standard input into each of the NFILES files in FILES
131 and into the standard output.
132 Return true if successful. */
135 tee_files (int nfiles
, const char **files
)
142 char const *mode_string
=
144 ? (append
? "ab" : "wb")
145 : (append
? "a" : "w"));
147 descriptors
= xnmalloc (nfiles
+ 1, sizeof *descriptors
);
149 /* Move all the names `up' one in the argv array to make room for
150 the entry for standard output. This writes into argv[argc]. */
151 for (i
= nfiles
; i
>= 1; i
--)
152 files
[i
] = files
[i
- 1];
154 if (O_BINARY
&& ! isatty (STDIN_FILENO
))
155 freopen (NULL
, "rb", stdin
);
156 if (O_BINARY
&& ! isatty (STDOUT_FILENO
))
157 freopen (NULL
, "wb", stdout
);
159 /* In the array of NFILES + 1 descriptors, make
160 the first one correspond to standard output. */
161 descriptors
[0] = stdout
;
162 files
[0] = _("standard output");
163 setvbuf (stdout
, NULL
, _IONBF
, 0);
165 for (i
= 1; i
<= nfiles
; i
++)
167 descriptors
[i
] = (STREQ (files
[i
], "-")
169 : fopen (files
[i
], mode_string
));
170 if (descriptors
[i
] == NULL
)
172 error (0, errno
, "%s", files
[i
]);
176 setvbuf (descriptors
[i
], NULL
, _IONBF
, 0);
181 bytes_read
= read (0, buffer
, sizeof buffer
);
183 if (bytes_read
< 0 && errno
== EINTR
)
189 /* Write to all NFILES + 1 descriptors.
190 Standard output is the first one. */
191 for (i
= 0; i
<= nfiles
; i
++)
193 && fwrite (buffer
, 1, bytes_read
, descriptors
[i
]) != bytes_read
)
195 error (0, errno
, "%s", files
[i
]);
196 descriptors
[i
] = NULL
;
201 if (bytes_read
== -1)
203 error (0, errno
, _("read error"));
207 /* Close the files, but not standard output. */
208 for (i
= 1; i
<= nfiles
; i
++)
209 if (!STREQ (files
[i
], "-")
210 && descriptors
[i
] && fclose (descriptors
[i
]) != 0)
212 error (0, errno
, "%s", files
[i
]);