On 17/02/15 23:14, Bernhard Voelker wrote:
> "tee -" duplicates stdin to stdout (maybe in interleaved order)
> since v5.2.1-1247-g8dafbe5; this behavior is documented as such.
> 
> But POSIX explicitly mandates different behavior:
> 
>   http://pubs.opengroup.org/onlinepubs/9699919799/utilities/tee.html
> 
>   If a file operand is '-', it shall refer to a file named -;
>   implementations shall not treat it as meaning standard output.
> 
> I assume it's too late to revert the behavior to adhere to POSIX
> rules as it may break existing use cases.
> Shall we either document that the GNU tee implementation violates
> POSIX, or should we go for a change to POSIX to allow this (or is
> there already such a discussion)?

I noticed that but didn't really want to start a discussion about it :)

The conformance issue was introduced with:
  http://git.sv.gnu.org/gitweb/?p=coreutils.git;a=commit;h=v5.2.1-1244-g4eafd1b
  http://git.sv.gnu.org/gitweb/?p=coreutils.git;a=commit;h=v5.2.1-1247-g8dafbe5

The reason I inferred from documentation is that the change was made for 
consistency,
but the point was also made that it wasn't that useful.
Are there any use cases for interleaved multiplied data?
One can always refer to a '-' file like ./- anyway,
so having '-' referring to stdout doesn't reduce functionality in any way.

I suppose we could change the behavior depending on $POSIXLY_CORRECT,
which is done in the attached.  I'd be 60:40 _against_ applying it.

cheers,
Pádraig.
From 7d629481d48982c3ab7882afbdea8f38cfe96524 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email protected]>
Date: Tue, 17 Feb 2015 23:50:17 +0000
Subject: [PATCH] tee: treat '-' as a regular file with $POSIXLY_CORRECT

* src/tee.c (usage): Adjust according to getenv("POSIXLY_CORRECT").
(main): Likewise.
---
 src/tee.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/tee.c b/src/tee.c
index 4f185d6..b9390b9 100644
--- a/src/tee.c
+++ b/src/tee.c
@@ -44,6 +44,9 @@ static bool append;
 /* If true, ignore interrupts. */
 static bool ignore_interrupts;
 
+/* Whether '-' means stdout.  */
+static bool posixly_correct;
+
 enum write_error
   {
     write_error_sigpipe,      /* traditional behavior, sigpipe enabled.  */
@@ -95,7 +98,8 @@ Copy standard input to each FILE, and also to standard output.\n\
 "), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
-      fputs (_("\
+      if (! posixly_correct)
+        fputs (_("\
 \n\
 If a FILE is -, copy again to standard output.\n\
 "), stdout);
@@ -130,6 +134,8 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
+  posixly_correct = !!getenv ("POSIXLY_CORRECT");
+
   append = false;
   ignore_interrupts = false;
 
@@ -219,7 +225,7 @@ tee_files (int nfiles, const char **files)
 
   for (i = 1; i <= nfiles; i++)
     {
-      descriptors[i] = (STREQ (files[i], "-")
+      descriptors[i] = (! posixly_correct && STREQ (files[i], "-")
                         ? stdout
                         : fopen (files[i], mode_string));
       if (descriptors[i] == NULL)
-- 
2.1.0

Reply via email to