On 02/19/2015 01:34 AM, Eric Blake wrote: > But I'm still 60:40 in favor of fixing it as > a bug fix, for consistency with other POSIX implementations.
Furthermore, what would be the alternative? It is clear now that GNU tee violates an explicitly mentioned aspect in the POSIX specification ... without proper documenting it. To summarize, these are our options: a) leave as is. b) guard deviating behavior by POSIXLY_CORRECT. c) revert the special-casing of "-" as meaning stdout. d) improve documentation and state why GNU tee consciously violates an explicit POSIX rule. a) is lame, b) was already considered suboptimal, and for d) I don't see much other argument than "it's like it is - although it's probably not useful". Therefore, I'd personally also vote for c). If there are some odd scripts out there relying on this, then it might not be hard to fix, e.g. (for text files): - ... | tee - | ... + ... | sed p | ... or - ... | tee out1 - out2 > out3 + ... | tee out1 >(sed p > out3) > out2 Attached is a patch to talk about. BTW: tee(1) seems to be the only coreutils utility which accepts "-" as stdout, isn't it? Have a nice day, Berny
>From 15e3305905d256a13b6d85a09c199c321d369ba8 Mon Sep 17 00:00:00 2001 From: Bernhard Voelker <m...@bernhard-voelker.de> Date: Thu, 19 Feb 2015 01:41:50 +0100 Subject: [PATCH] tee: treat '-' operand as file name as mandated by POSIX Since v5.2.1-1247-g8dafbe5, tee(1) treated '-' as stdout while POSIX explicitly requires to treat this as a file name. Revert this change, as the interleaved output - due to sending another copy of input to stdout - is not considered to be useful. Discussed in http://lists.gnu.org/archive/html/coreutils/2015-02/msg00085.html * src/tee.c (tee_files): Remove the special handling for "-" operands. (usage): Remove the corresponding sentence. * doc/coreutils.texi (common options): Remove the "tee -" example. (tee invocation): Document that tee(1) now treats "-" as a file name. * tests/misc/tee.sh: Add a test case. * NEWS (Changes in behavior): Mention the change. --- NEWS | 4 ++++ doc/coreutils.texi | 17 +++++++++-------- src/tee.c | 9 ++------- tests/misc/tee.sh | 7 +++++++ 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/NEWS b/NEWS index b6795aa..8597357 100644 --- a/NEWS +++ b/NEWS @@ -73,6 +73,10 @@ GNU coreutils NEWS -*- outline -*- tee will exit early if there are no more writable outputs. + tee does not treat the file operand '-' as meaning standard output any longer, + for better conformance to POSIX. This feature was added in coreutils-5.3.0. + + ** Improvements cp,install,mv will convert smaller runs of NULs in the input to holes, diff --git a/doc/coreutils.texi b/doc/coreutils.texi index bb652ac..8d050c2 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -752,11 +752,10 @@ operands even if they begin with @samp{-}. For example, @samp{sort -- @cindex standard input @cindex standard output A single @samp{-} operand is not really an option, though it looks like one. It -stands for standard input, or for standard output if that is clear from -the context. For example, @samp{sort -} reads from standard input, -and is equivalent to plain @samp{sort}, and @samp{tee -} writes an -extra copy of its input to standard output. Unless otherwise -specified, @samp{-} can appear as any operand that requires a file +stands for a file operand, and some tools treat it as standard input, or as +standard output if that is clear from the context. For example, @samp{sort -} +reads from standard input, and is equivalent to plain @samp{sort}. Unless +otherwise specified, a @samp{-} can appear as any operand that requires a file name. @menu @@ -13180,9 +13179,11 @@ If a file being written to does not already exist, it is created. If a file being written to already exists, the data it previously contained is overwritten unless the @option{-a} option is used. -A @var{file} of @samp{-} causes @command{tee} to send another copy of -input to standard output, but this is typically not that useful as the -copies are interleaved. +In previous versions of GNU coreutils (v5.3.0 - v8.23), a @var{file} of @samp{-} +caused @command{tee} to send another copy of input to standard output. +However, as the interleaved output was not very useful, @command{tee} now +conforms to POSIX which explicitly mandates it to treat @samp{-} as a file +with such name. The program accepts the following options. Also see @ref{Common options}. diff --git a/src/tee.c b/src/tee.c index bfe1b69..feb4026 100644 --- a/src/tee.c +++ b/src/tee.c @@ -68,10 +68,6 @@ Copy standard input to each FILE, and also to standard output.\n\ "), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); - fputs (_("\ -\n\ -If a FILE is -, copy again to standard output.\n\ -"), stdout); emit_ancillary_info (PROGRAM_NAME); } exit (status); @@ -169,9 +165,8 @@ tee_files (int nfiles, const char **files) for (i = 1; i <= nfiles; i++) { - descriptors[i] = (STREQ (files[i], "-") - ? stdout - : fopen (files[i], mode_string)); + /* Do not treat "-" specially - as mandated by POSIX. */ + descriptors[i] = fopen (files[i], mode_string); if (descriptors[i] == NULL) { error (0, errno, "%s", files[i]); diff --git a/tests/misc/tee.sh b/tests/misc/tee.sh index 5f2eeda..85597c5 100755 --- a/tests/misc/tee.sh +++ b/tests/misc/tee.sh @@ -31,6 +31,13 @@ for n in 0 $nums; do done done +# Ensure tee treats '-' as the name of a file, as mandated by POSIX. +# Between v5.3.0 and v8.23, a '-' argument caused tee to send another +# copy of input to standard output. +tee - <sample >out 2>err || fail=1 +compare sample ./- || fail=1 +compare sample out || fail=1 +compare /dev/null err || fail # Ensure tee exits early if no more writable outputs if test -w /dev/full && test -c /dev/full; then -- 2.1.4