#include <SupportDefs.h>
#include <Drivers.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <errno_private.h>
int
ttyname_r(int fd, char *buffer, size_t bufferSize)
{
struct stat fdStat;
if (fstat(fd, &fdStat) < 0)
return ENOTTY;
if (!S_ISCHR(fdStat.st_mode) || !isatty(fd))
return ENOTTY;
if (ioctl(fd, B_GET_PATH_FOR_DEVICE, buffer, bufferSize) < 0)
return errno;
return 0;
}
char *
ttyname(int fd)
{
static char pathname[MAXPATHLEN];
int err = ttyname_r(fd, pathname, sizeof(pathname));
if (err != 0) {
__set_errno(err);
return NULL;
}
return pathname;
}