From: Paul Eggert Date: Fri, 20 Jun 2025 18:53:21 +0000 (-0700) Subject: tty: better fix for Bug#26371 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aec89a3e7dfaed1545b2dcaa8525df21cf61a37e;p=thirdparty%2Fcoreutils.git tty: better fix for Bug#26371 * src/tty.c (TTY_USAGE): Rename from TTY_FAILURE, since this is used only for usage failures. All uses changed. (TTY_TTYNAME_FAILURE): New constant. (main): Remove no-longer-needed assignment of ENOENT to errno. Make status-setting clearer too. Report an error if ttyname fails even though stdin is a terminal, instead of silently pretending that stdin is not a terminal. * tests/tty/tty.sh: Test for this issue. This should fix Bug#78244. --- diff --git a/NEWS b/NEWS index 8f9ac26d06..50f27cd87a 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,11 @@ GNU coreutils NEWS -*- outline -*- 'sort +0.18446744073709551615R input' on 64 bit systems. [bug introduced in coreutils-7.2] + tty now exits with status 4 with a special diagnostic if ttyname + fails even though standard input is a tty. Formerly it quietly + pretended that standard input was not a tty. + [This bug was present in "the beginning".] + ** Improvements stty supports setting arbitrary baud rates on supported systems, diff --git a/THANKS.in b/THANKS.in index 57ace387e0..8c97a81388 100644 --- a/THANKS.in +++ b/THANKS.in @@ -120,6 +120,7 @@ Chris Lesniewski ctl@mit.edu Chris Sylvain csylvain@umm.edu Chris Yeo cyeo@biking.org Christi Alice Scarborough christi@chiark.greenend.org.uk +Christian Brauner christian.brauner@canonical.com Christian Harkort christian.harkort@web.de Christian Jullien eligis@orange.fr Christian Krackowizer ckrackowiz@std.schuler-ag.com diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 7556571d7d..fc62c6d8d6 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -16020,6 +16020,7 @@ Exit status: 1 if standard input is a non-terminal file 2 if given incorrect arguments 3 if a write error occurs +4 if the terminal's name cannot be determined @end display diff --git a/src/tty.c b/src/tty.c index b01f1a207a..d5dea5200b 100644 --- a/src/tty.c +++ b/src/tty.c @@ -33,8 +33,9 @@ enum { TTY_STDIN_NOTTY = 1, - TTY_FAILURE = 2, - TTY_WRITE_ERROR = 3 + TTY_USAGE = 2, + TTY_WRITE_ERROR = 3, + TTY_TTYNAME_FAILURE = 4 }; /* The official name of this program (e.g., no 'g' prefix). */ @@ -103,26 +104,29 @@ main (int argc, char **argv) case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); default: - usage (TTY_FAILURE); + usage (TTY_USAGE); } } if (optind < argc) { error (0, 0, _("extra operand %s"), quote (argv[optind])); - usage (TTY_FAILURE); + usage (TTY_USAGE); } - errno = ENOENT; - if (silent) return isatty (STDIN_FILENO) ? EXIT_SUCCESS : TTY_STDIN_NOTTY; - int status = EXIT_SUCCESS; + int status; char const *tty = ttyname (STDIN_FILENO); - if (! tty) + if (tty) + status = EXIT_SUCCESS; + else { + int ttyname_err = errno; + if (isatty (STDIN_FILENO)) + error (TTY_TTYNAME_FAILURE, ttyname_err, "ttyname"); tty = _("not a tty"); status = TTY_STDIN_NOTTY; } diff --git a/tests/tty/tty.sh b/tests/tty/tty.sh index 8201b42ebd..b2b8aa25ad 100755 --- a/tests/tty/tty.sh +++ b/tests/tty/tty.sh @@ -20,8 +20,14 @@ . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src print_ver_ tty +tty_works_on_stdin=false + if test -t 0; then - tty || fail=1 + if tty; then + tty_works_on_stdin=true + else + test $? -eq 4 || fail=1 + fi tty -s || fail=1 fi @@ -34,7 +40,7 @@ returns_ 2 tty a || fail=1 returns_ 2 tty -s a || fail=1 if test -w /dev/full && test -c /dev/full; then - if test -t 0; then + if $tty_works_on_stdin; then returns_ 3 tty >/dev/full || fail=1 fi returns_ 3 tty /dev/full || fail=1