Skip to content

Commit c44dc0b

Browse files
authored
Use TIOCGWINSZ instead of TCGETS to detect terminals (#97)
TCGETS shares its ioctl number with SNDCTL_TMR_TIMEBASE of the OSS sound API, so it may succeed (and even change the device mode) on certain non-tty devices. TIOCGWINSZ has no such conflict and succeeds on any tty. musl's isatty does the same. Fixes #76
1 parent 4bc9b75 commit c44dc0b

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ package isatty
88
import "golang.org/x/sys/unix"
99

1010
// IsTerminal return true if the file descriptor is terminal.
11+
// TIOCGWINSZ is used instead of TCGETS because TCGETS shares its ioctl
12+
// number with SNDCTL_TMR_TIMEBASE of the OSS sound API, so it may succeed
13+
// (and even change the device mode) on non-tty devices. musl's isatty does
14+
// the same.
1115
func IsTerminal(fd uintptr) bool {
12-
_, err := unix.IoctlGetTermios(int(fd), unix.TCGETS)
16+
_, err := unix.IoctlGetWinsize(int(fd), unix.TIOCGWINSZ)
1317
return err == nil
1418
}
1519

0 commit comments

Comments
 (0)