summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2023-03-04 12:18:18 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2023-03-04 14:49:45 -0800
commit0789451237b90324a5d1f5c1b9fbaa5099f4c072 (patch)
treec6ef3a4a3ba31d975603b25e45e2c85dfb2a8a71
parent76b5843ff99977107580cd581213ea2b5b3a81a7 (diff)
downloadcoreutils-0789451237b90324a5d1f5c1b9fbaa5099f4c072.tar.gz
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.
-rw-r--r--src/tee.c27
1 files changed, 9 insertions, 18 deletions
diff --git a/src/tee.c b/src/tee.c
index dcf773e16..4fc6722d4 100644
--- 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;