Fixed compilation with local changes (to be commited later).

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5263 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2003-11-06 00:10:03 +00:00
parent b86315d90a
commit ea2aba5dc9
+18 -20
View File
@@ -3,6 +3,9 @@
** Distributed under the terms of the OpenBeOS License. ** Distributed under the terms of the OpenBeOS License.
*/ */
#include <SupportDefs.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@@ -15,66 +18,61 @@
#define DEV_TTY "/dev/tt/" #define DEV_TTY "/dev/tt/"
/* /** return the full pathname of the tty device
* ttyname - return the full pathname of the tty device * (NULL if not a tty or an error occurs)
* (NULL if not a tty or an error occurs)
*/ */
char * char *
ttyname (int fd) ttyname(int fd)
{ {
static char pathname[MAXPATHLEN]; static char pathname[MAXPATHLEN];
char *stub = pathname + sizeof(DEV_TTY) - 1; char *stub = pathname + sizeof(DEV_TTY) - 1;
int stubLen = sizeof(pathname) - sizeof(DEV_TTY); int stubLen = sizeof(pathname) - sizeof(DEV_TTY);
struct stat fdStat; struct stat fdStat;
DIR *dir; DIR *dir;
bool found = false; bool found = false;
// first, some sanity checks: // first, some sanity checks:
if (!isatty(fd)) if (!isatty(fd))
return NULL; return NULL;
if (fstat(fd, &fdStat) < 0) if (fstat(fd, &fdStat) < 0)
return NULL; return NULL;
if (!S_ISCHR(fdStat.st_mode)) if (!S_ISCHR(fdStat.st_mode))
return NULL; return NULL;
// start with the root tty directory at /dev // start with the root tty directory at /dev
strcpy(pathname, DEV_TTY); strcpy(pathname, DEV_TTY);
if ((dir = opendir(pathname)) != NULL) { if ((dir = opendir(pathname)) != NULL) {
// find a matching entry in the directory: // find a matching entry in the directory:
// we must match both the inode for the file // we must match both the inode for the file
// and the device that the file resides on // and the device that the file resides on
struct dirent *e; struct dirent *e;
struct stat entryStat; struct stat entryStat;
while ((e = readdir(dir)) != NULL) { while ((e = readdir(dir)) != NULL) {
// try to match the file's inode // try to match the file's inode
if (e->d_ino != fdStat.st_ino) if (e->d_ino != fdStat.st_ino)
continue; continue;
// construct the entry's full filename and // construct the entry's full filename and
// call stat() to retrieve the inode info // call stat() to retrieve the inode info
strncpy(stub, e->d_name, stubLen); strncpy(stub, e->d_name, stubLen);
if (stat(pathname, &entryStat) < 0) if (stat(pathname, &entryStat) < 0)
continue; continue;
if (entryStat.st_ino == fdStat.st_ino && if (entryStat.st_ino == fdStat.st_ino &&
entryStat.st_dev == fdStat.st_dev) { entryStat.st_dev == fdStat.st_dev) {
found = true; found = true;
break; break;
} }
} }
closedir(dir); closedir(dir);
} }
return found ? pathname : NULL; return found ? pathname : NULL;
} }