2013-08-14 10:38:43 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-01-16 10:32:55 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2015-01-16 11:02:25 -05:00
|
|
|
#include <fcntl.h>
|
2015-01-16 10:32:55 -05:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <limits.h>
|
2013-08-14 10:38:43 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-01-16 10:32:55 -05:00
|
|
|
#include <unistd.h>
|
2014-06-30 14:03:41 -04:00
|
|
|
|
2013-08-14 10:38:43 -04:00
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
devtotty(int dev, int *tty_maj, int *tty_min)
|
|
|
|
{
|
|
|
|
*tty_maj = (dev >> 8) & 0xfff;
|
|
|
|
*tty_min = (dev & 0xff) | ((dev >> 12) & 0xfff00);
|
|
|
|
}
|
|
|
|
|
2015-01-16 10:32:55 -05:00
|
|
|
int
|
|
|
|
ttytostr(int tty_maj, int tty_min, char *str, size_t n)
|
2013-08-14 10:38:43 -04:00
|
|
|
{
|
2015-01-16 10:32:55 -05:00
|
|
|
struct stat sb;
|
|
|
|
struct dirent *dp;
|
|
|
|
DIR *dirp;
|
|
|
|
char path[PATH_MAX];
|
2015-01-16 11:02:25 -05:00
|
|
|
int fd;
|
2015-01-16 10:32:55 -05:00
|
|
|
|
2013-08-14 10:38:43 -04:00
|
|
|
switch (tty_maj) {
|
|
|
|
case 136:
|
2015-01-16 10:32:55 -05:00
|
|
|
snprintf(str, n, "pts/%d", tty_min);
|
|
|
|
return 0;
|
2013-08-14 10:38:43 -04:00
|
|
|
case 4:
|
2015-01-16 10:32:55 -05:00
|
|
|
snprintf(str, n, "tty%d", tty_min);
|
|
|
|
return 0;
|
2013-08-14 10:38:43 -04:00
|
|
|
default:
|
2015-01-16 10:32:55 -05:00
|
|
|
str[0] = '?';
|
|
|
|
str[1] = '\0';
|
2013-08-14 10:38:43 -04:00
|
|
|
break;
|
|
|
|
}
|
2015-01-16 10:32:55 -05:00
|
|
|
|
|
|
|
dirp = opendir("/dev");
|
|
|
|
if (!dirp) {
|
|
|
|
weprintf("opendir /dev:");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((dp = readdir(dirp))) {
|
|
|
|
if (!strcmp(dp->d_name, ".") ||
|
|
|
|
!strcmp(dp->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strlcpy(path, "/dev/", sizeof(path)) >= sizeof(path)) {
|
|
|
|
weprintf("path too long\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (strlcat(path, dp->d_name, sizeof(path)) >= sizeof(path)) {
|
|
|
|
weprintf("path too long\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(path, &sb) < 0) {
|
2015-01-16 11:02:25 -05:00
|
|
|
weprintf("stat %s:", path);
|
2015-01-16 10:32:55 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((int)major(sb.st_rdev) == tty_maj &&
|
|
|
|
(int)minor(sb.st_rdev) == tty_min) {
|
2015-01-16 11:02:25 -05:00
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
continue;
|
|
|
|
if (isatty(fd)) {
|
|
|
|
strlcpy(str, dp->d_name, n);
|
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd);
|
2015-01-16 10:32:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closedir(dirp) < 0) {
|
|
|
|
weprintf("closedir /dev:");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-08-14 10:38:43 -04:00
|
|
|
}
|