On 06/03/2014 08:50 AM, Bernhard Voelker wrote: > Q: Why does 'true --help >/dev/full' return 1? > > Longer story: > I was wondering why 'false --version' exits with 1, so looking > up the POSIX specification [0] made it clear: > > The false utility shall always exit with a value other than zero. > > So I looked at src/true.c and found the atexit handler detecting > write errors [1, line 67]: > > atexit (close_stdout); > > ... despite true's spec [2] which is quite clear about the exit code, too: > > The true utility shall return with exit code zero. > > This looks like a (well, little) contradiction to me: the options > --help and --version are GNU extensions and therefore may or may > not lead to a different exit code than specified for the normal > operation of the tool, but I'd expect behaving them the same for > all utilities (under all circumstances): either let 'false --version' > return 0 (because the GNU extension operation succeeded), or let > 'true --version >/dev/full' return 0 (because POSIX says that 'true' > should always return 0). > WDYT? > > [0] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/false.html > [1] > http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/true.c?id=1239ac573d#n67 > [2] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/true.html
Heh I never thought I'd be discussing the logic in true/false :) There is an inconsistency here with the (GNU) options. Now I don't think anyone would ever be using `env true --version > blah` in a script, but it would be better to be consistent here and have the exit code dependent on the name rather than the environment. I also see that the test script for `false` was only checking the shell builtin :/ The attached fixes up both issues in your name. OK to push? thanks, Pádraig.
>From 83d52a061db9350bf97cd7c8c49d727b7e931662 Mon Sep 17 00:00:00 2001 From: Bernhard Voelker <[email protected]> Date: Tue, 3 Jun 2014 10:50:09 +0100 Subject: [PATCH] true: always exit with status 0 even if can't write * src/true.c (main): Init exit_failure appropriately. * tests/misc/false-status.sh: Fix so we're testing the tool and not the shell builtin. Add a case for true also. --- src/true.c | 1 + tests/misc/false-status.sh | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/true.c b/src/true.c index 219f98c..4ac319b 100644 --- a/src/true.c +++ b/src/true.c @@ -64,6 +64,7 @@ main (int argc, char **argv) bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); + initialize_exit_failure (EXIT_STATUS); atexit (close_stdout); if (STREQ (argv[1], "--help")) diff --git a/tests/misc/false-status.sh b/tests/misc/false-status.sh index b943040..9be6892 100755 --- a/tests/misc/false-status.sh +++ b/tests/misc/false-status.sh @@ -1,5 +1,6 @@ #!/bin/sh # ensure that false exits nonzero even with --help or --version +# and vice versa for true # Copyright (C) 2003-2014 Free Software Foundation, Inc. @@ -19,7 +20,12 @@ . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src print_ver_ false -false --version > /dev/null && fail=1 -false --help > /dev/null && fail=1 +env false --version > /dev/null && fail=1 +env false --help > /dev/null && fail=1 + +if test -w /dev/full && test -c /dev/full; then + env true --version > /dev/full || fail=1 + env true --help > /dev/full || fail=1 +fi Exit $fail -- 1.7.7.6
