浏览代码

isatty: don't collapse all non-EBADF errors to ENOTTY

linux puts hung-up ttys in a state where ioctls produce EIO, and may
do the same for other types of devices in error or shutdown states.
such an error clearly does not mean the device is not a tty, but it
also can't reliably establish that the device is a tty, so the only
safe thing to do seems to be reporting the error. programs that don't
check errno will conclude that the device is not a tty, which is no
different from what happens now, but at least they gain the option to
differentiate between the cases.

commit c84971995b3a6d5118f9357c040572f4c78bcd55 introduced the errno
collapsing behavior, but prior to that, errno was not set at all by
isatty.
Rich Felker 3 月之前
父节点
当前提交
c94a0c16f0
共有 1 个文件被更改,包括 2 次插入4 次删除
  1. 2 4
      src/unistd/isatty.c

+ 2 - 4
src/unistd/isatty.c

@@ -6,8 +6,6 @@
 int isatty(int fd)
 {
 	struct winsize wsz;
-	unsigned long r = syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz);
-	if (r == 0) return 1;
-	if (errno != EBADF) errno = ENOTTY;
-	return 0;
+	/* +1 converts from error status (0/-1) to boolean (1/0) */
+	return syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz) + 1;
 }