]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
libgloss: fix isatty() and ttyname() — use TCGETS instead of TIOCGPGRP with NULL
authorTulio A M Mendes <[email protected]>
Sun, 19 Apr 2026 21:05:46 +0000 (18:05 -0300)
committerTulio A M Mendes <[email protected]>
Sun, 19 Apr 2026 21:05:46 +0000 (18:05 -0300)
isatty() in syscalls.c used ioctl(TIOCGPGRP, 0) which always failed
because the kernel rejects NULL user_arg with -EFAULT. This caused
bash to think stdin was not a terminal, running in non-interactive
mode (no prompt, no line editing).

Fix: use TCGETS (0x5401) with a stack-allocated termios struct instead.
TCGETS succeeds on any tty regardless of process group state — this
is the standard POSIX way to test for a terminal.

Also fix ttyname() in posix_stubs.c which had the same bug.

newlib/libgloss/adros/posix_stubs.c
newlib/libgloss/adros/syscalls.c

index ea391696d35f2c57eba6eb4af50a1b7a80d7fbbe..db32cadd33f0e054dffc06f0f5b28b9d4ef59f16 100644 (file)
@@ -431,7 +431,8 @@ int cfsetospeed(struct termios *t, speed_t speed) { (void)t; (void)speed; return
 
 char *ttyname(int fd) {
     /* Minimal: check if fd is a tty, return generic name */
-    int r = _sc3(SYS_IOCTL, fd, TTY_TIOCGPGRP, 0);
+    struct { uint32_t a, b, c, d; uint8_t e[8]; } t;
+    int r = _sc3(SYS_IOCTL, fd, TTY_TCGETS, (int)&t);
     if (r < 0) { errno = ENOTTY; return 0; }
     return "/dev/tty";
 }
index 949592b956b9cb022cf6f81d62d226a780ac97c4..a68a9e6575acae3aa6936f578634af4ccde3350d 100644 (file)
@@ -162,8 +162,12 @@ int mkdir(const char *path, mode_t mode) {
 }
 
 int isatty(int fd) {
-    /* Use ioctl TIOCGPGRP (0x540F) — if it succeeds, fd is a tty */
-    int r = _sc3(SYS_IOCTL, fd, 0x540F, 0);
+    /* Use ioctl TCGETS (0x5401) — if it succeeds, fd is a tty.
+     * TCGETS is the standard test: it succeeds on any terminal device
+     * regardless of process group state. TIOCGPGRP was unreliable because
+     * it requires a valid output pointer and a foreground pgrp to be set. */
+    struct { uint32_t a, b, c, d; uint8_t e[8]; } t;
+    int r = _sc3(SYS_IOCTL, fd, 0x5401, (int)&t);
     if (r < 0) {
         errno = ENOTTY;
         return 0;