aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2025-06-20 11:53:21 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2025-06-20 12:15:27 -0700
commitaec89a3e7dfaed1545b2dcaa8525df21cf61a37e (patch)
tree5792c2d949af23d11ffe2e92a9f12da2b34622f7
parenttests: stty: adjust tests for arbitary speeds (diff)
downloadcoreutils-aec89a3e7dfaed1545b2dcaa8525df21cf61a37e.tar.gz
coreutils-aec89a3e7dfaed1545b2dcaa8525df21cf61a37e.zip
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.
-rw-r--r--NEWS5
-rw-r--r--THANKS.in1
-rw-r--r--doc/coreutils.texi1
-rw-r--r--src/tty.c20
-rwxr-xr-xtests/tty/tty.sh10
5 files changed, 27 insertions, 10 deletions
diff --git a/NEWS b/NEWS
index 8f9ac26d0..50f27cd87 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 57ace387e..8c97a8138 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 7556571d7..fc62c6d8d 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 b01f1a207..d5dea5200 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 8201b42eb..b2b8aa25a 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/null >/dev/full || fail=1