1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-02-11 18:02:13 +02:00

tee: tune when later -p overrides earlier

* src/tee.c (pipe_check): Make this a local var instead
of a static var.  This suppresses a -Wmaybe-uninitialized
diagnostic with gcc 12.2.1 20221121 (Red Hat 12.2.1-4).
(main): Don’t set pipe_check unnecessarily if a later
-p option overrides an earlier one that wants pipe_check.
Problem discovered when I investigated the GCC warning.
This commit is contained in:
Paul Eggert
2023-03-04 12:18:18 -08:00
parent 76b5843ff9
commit 0789451237

View File

@@ -38,7 +38,7 @@
proper_name ("Richard M. Stallman"), \
proper_name ("David MacKenzie")
static bool tee_files (int nfiles, char **files);
static bool tee_files (int nfiles, char **files, bool);
/* If true, append to output files rather than truncating them. */
static bool append;
@@ -46,9 +46,6 @@ static bool append;
/* If true, ignore interrupts. */
static bool ignore_interrupts;
/* If true, detect if next output becomes broken while waiting for input. */
static bool pipe_check;
enum output_error
{
output_error_sigpipe, /* traditional behavior, sigpipe enabled. */
@@ -122,9 +119,6 @@ writing to non pipe outputs.\n\
int
main (int argc, char **argv)
{
bool ok;
int optc;
initialize_main (&argc, &argv);
set_program_name (argv[0]);
setlocale (LC_ALL, "");
@@ -136,6 +130,7 @@ main (int argc, char **argv)
append = false;
ignore_interrupts = false;
int optc;
while ((optc = getopt_long (argc, argv, "aip", long_options, NULL)) != -1)
{
switch (optc)
@@ -154,12 +149,6 @@ main (int argc, char **argv)
output_error_args, output_error_types);
else
output_error = output_error_warn_nopipe;
/* Detect and close a broken pipe output when ignoring EPIPE. */
if (output_error == output_error_warn_nopipe
|| output_error == output_error_exit_nopipe)
pipe_check = true;
break;
case_GETOPT_HELP_CHAR;
@@ -177,14 +166,16 @@ main (int argc, char **argv)
if (output_error != output_error_sigpipe)
signal (SIGPIPE, SIG_IGN);
/* No need to poll outputs if input is always ready for reading. */
if (pipe_check && !iopoll_input_ok (STDIN_FILENO))
pipe_check = false;
/* Whether to detect and close a broken pipe output.
There is no need if the input is always ready for reading. */
bool pipe_check = ((output_error == output_error_warn_nopipe
|| output_error == output_error_exit_nopipe)
&& iopoll_input_ok (STDIN_FILENO));
/* Do *not* warn if tee is given no file arguments.
POSIX requires that it work when given no arguments. */
ok = tee_files (argc - optind, &argv[optind]);
bool ok = tee_files (argc - optind, &argv[optind], pipe_check);
if (close (STDIN_FILENO) != 0)
die (EXIT_FAILURE, errno, "%s", _("standard input"));
@@ -232,7 +223,7 @@ fail_output (FILE **descriptors, char **files, int i)
Return true if successful. */
static bool
tee_files (int nfiles, char **files)
tee_files (int nfiles, char **files, bool pipe_check)
{
size_t n_outputs = 0;
FILE **descriptors;