Much simpler and safer ttyname() using B_GET_PATH_FOR_DEVICE. Also added ttyname_r for which we had a proto in the headers.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20133 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1616,6 +1616,8 @@ devfs_ioctl(fs_volume _fs, fs_vnode _vnode, fs_cookie _cookie, ulong op,
|
|||||||
*/
|
*/
|
||||||
strcpy(path, "/dev/");
|
strcpy(path, "/dev/");
|
||||||
get_device_name(vnode, path + 5, sizeof(path) - 5);
|
get_device_name(vnode, path + 5, sizeof(path) - 5);
|
||||||
|
if (length && (length <= strlen(path)))
|
||||||
|
return ERANGE;
|
||||||
return user_strlcpy((char *)buffer, path, sizeof(path));
|
return user_strlcpy((char *)buffer, path, sizeof(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,78 +1,59 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright 2003, Daniel Reinhold, [email protected]. All rights reserved.
|
** Copyright 2003, Daniel Reinhold, [email protected]. All rights reserved.
|
||||||
|
** Copyright 2007, François Revol, [email protected]. All rights reserved.
|
||||||
** Distributed under the terms of the OpenBeOS License.
|
** Distributed under the terms of the OpenBeOS License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <SupportDefs.h>
|
#include <SupportDefs.h>
|
||||||
|
#include <Drivers.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <dirent.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
|
||||||
|
/**
|
||||||
/* the root directory in /dev where all tty devices reside */
|
* give the name of a tty fd. threadsafe.
|
||||||
#define DEV_TTY "/dev/tt/"
|
* @param fd the tty to get the name from.
|
||||||
|
* @param buffer where to store the name to.
|
||||||
|
* @param bufferSize length of the buffer.
|
||||||
/** return the full pathname of the tty device
|
* @return 0 on success, -1 on error, sets errno.
|
||||||
* (NULL if not a tty or an error occurs)
|
|
||||||
*/
|
*/
|
||||||
|
int
|
||||||
|
ttyname_r(int fd, char *buffer, size_t bufferSize)
|
||||||
|
{
|
||||||
|
struct stat fdStat;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
// first, some sanity checks:
|
||||||
|
if (fstat(fd, &fdStat) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (!S_ISCHR(fdStat.st_mode) || !isatty(fd)) {
|
||||||
|
errno = ENOTTY;
|
||||||
|
return ENOTTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
// just ask devfs
|
||||||
|
return ioctl(fd, B_GET_PATH_FOR_DEVICE, name, namesize);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* give the name of a tty fd.
|
||||||
|
* @param fd the tty to get the name from.
|
||||||
|
* @return the name of the tty or NULL on error.
|
||||||
|
*/
|
||||||
char *
|
char *
|
||||||
ttyname(int fd)
|
ttyname(int fd)
|
||||||
{
|
{
|
||||||
static char pathname[MAXPATHLEN];
|
static char pathname[MAXPATHLEN];
|
||||||
char *stub = pathname + sizeof(DEV_TTY) - 1;
|
|
||||||
int stubLen = sizeof(pathname) - sizeof(DEV_TTY);
|
|
||||||
struct stat fdStat;
|
|
||||||
DIR *dir;
|
|
||||||
bool found = false;
|
|
||||||
|
|
||||||
// first, some sanity checks:
|
int err = ttyname_r(fd, pathname, sizeof(pathname));
|
||||||
if (!isatty(fd))
|
if (err < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
return pathname;
|
||||||
if (fstat(fd, &fdStat) < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (!S_ISCHR(fdStat.st_mode))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
// start with the root tty directory at /dev
|
|
||||||
strcpy(pathname, DEV_TTY);
|
|
||||||
|
|
||||||
if ((dir = opendir(pathname)) != NULL) {
|
|
||||||
// find a matching entry in the directory:
|
|
||||||
// we must match both the inode for the file
|
|
||||||
// and the device that the file resides on
|
|
||||||
struct dirent *e;
|
|
||||||
struct stat entryStat;
|
|
||||||
|
|
||||||
while ((e = readdir(dir)) != NULL) {
|
|
||||||
// try to match the file's inode
|
|
||||||
if (e->d_ino != fdStat.st_ino)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// construct the entry's full filename and
|
|
||||||
// call stat() to retrieve the inode info
|
|
||||||
strncpy(stub, e->d_name, stubLen);
|
|
||||||
|
|
||||||
if (stat(pathname, &entryStat) < 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (entryStat.st_ino == fdStat.st_ino &&
|
|
||||||
entryStat.st_dev == fdStat.st_dev) {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
closedir(dir);
|
|
||||||
}
|
|
||||||
|
|
||||||
return found ? pathname : NULL;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user