OSDN Git Service

improve error handling of ttyname_r and isatty
authorBenjamin Peterson <benjamin@python.org>
Thu, 13 Sep 2018 21:23:42 +0000 (14:23 -0700)
committerRich Felker <dalias@aerifal.cx>
Sat, 15 Sep 2018 05:58:03 +0000 (01:58 -0400)
POSIX allows ttyname(_r) and isatty to return EBADF if passed file
descriptor is invalid.

maintainer's note: these are optional ("may fail") errors, but it's
non-conforming for ttyname_r to return ENOTTY when it failed for a
different reason.

src/unistd/isatty.c
src/unistd/ttyname_r.c

index c8badaf..75a9c18 100644 (file)
@@ -1,9 +1,13 @@
 #include <unistd.h>
+#include <errno.h>
 #include <sys/ioctl.h>
 #include "syscall.h"
 
 int isatty(int fd)
 {
        struct winsize wsz;
-       return !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz);
+       unsigned long r = syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz);
+       if (r == 0) return 1;
+       if (errno != EBADF) errno = ENOTTY;
+       return 0;
 }
index cb364c2..82acb75 100644 (file)
@@ -9,7 +9,7 @@ int ttyname_r(int fd, char *name, size_t size)
        char procname[sizeof "/proc/self/fd/" + 3*sizeof(int) + 2];
        ssize_t l;
 
-       if (!isatty(fd)) return ENOTTY;
+       if (!isatty(fd)) return errno;
 
        __procfdname(procname, fd);
        l = readlink(procname, name, size);