]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
tee: tune when later -p overrides earlier
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 4 Mar 2023 20:18:18 +0000 (12:18 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 4 Mar 2023 22:49:45 +0000 (14:49 -0800)
* 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.

src/tee.c

index dcf773e162723b648835591b6167234e5d4cb9ae..4fc6722d4b2c0d909d7f7265fa4211e6fda10d35 100644 (file)
--- a/src/tee.c
+++ b/src/tee.c
@@ -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;