[hackers] [ubase] Check if the ttymaj/ttymin match is actually a tty || sin

From: <git_AT_suckless.org>
Date: Fri, 16 Jan 2015 17:10:49 +0100 (CET)

commit 8a95422ff14aed8c8cd7cfad51913db90493a50c
Author: sin <sin_AT_2f30.org>
Date: Fri Jan 16 16:02:25 2015 +0000

    Check if the ttymaj/ttymin match is actually a tty

diff --git a/libutil/tty.c b/libutil/tty.c
index 9078e37..f8159d9 100644
--- a/libutil/tty.c
+++ b/libutil/tty.c
_AT_@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include <fcntl.h>
 #include <dirent.h>
 #include <limits.h>
 #include <stdio.h>
_AT_@ -25,6 +26,7 @@ ttytostr(int tty_maj, int tty_min, char *str, size_t n)
         struct dirent *dp;
         DIR *dirp;
         char path[PATH_MAX];
+ int fd;
 
         switch (tty_maj) {
         case 136:
_AT_@ -60,14 +62,21 @@ ttytostr(int tty_maj, int tty_min, char *str, size_t n)
                 }
 
                 if (stat(path, &sb) < 0) {
- weprintf("stat %s:", dp->d_name);
+ weprintf("stat %s:", path);
                         return -1;
                 }
 
                 if ((int)major(sb.st_rdev) == tty_maj &&
                     (int)minor(sb.st_rdev) == tty_min) {
- strlcpy(str, dp->d_name, n);
- break;
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ continue;
+ if (isatty(fd)) {
+ strlcpy(str, dp->d_name, n);
+ close(fd);
+ break;
+ }
+ close(fd);
                 }
         }
 
Received on Fri Jan 16 2015 - 17:10:49 CET

This archive was generated by hypermail 2.3.0 : Fri Jan 16 2015 - 17:12:10 CET