/*
|
/*
|
* ttyname_r() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name
|
* ttyname_r() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name
|
*
|
*
|
* ttyname.c,v 1.7 2002/04/03 14:22:58 joel Exp
|
* ttyname.c,v 1.7 2002/04/03 14:22:58 joel Exp
|
*/
|
*/
|
|
|
#if HAVE_CONFIG_H
|
#if HAVE_CONFIG_H
|
#include "config.h"
|
#include "config.h"
|
#endif
|
#endif
|
|
|
#include <sys/types.h>
|
#include <sys/types.h>
|
#include <sys/stat.h>
|
#include <sys/stat.h>
|
#include <fcntl.h>
|
#include <fcntl.h>
|
#include <dirent.h>
|
#include <dirent.h>
|
#include <termios.h>
|
#include <termios.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
#include <string.h>
|
#include <string.h>
|
#include <paths.h>
|
#include <paths.h>
|
#include <_syslist.h>
|
#include <_syslist.h>
|
#include <errno.h>
|
#include <errno.h>
|
|
|
#include <rtems/libio_.h>
|
#include <rtems/libio_.h>
|
#include <rtems/seterr.h>
|
#include <rtems/seterr.h>
|
|
|
int ttyname_r(
|
int ttyname_r(
|
int fd,
|
int fd,
|
char *name,
|
char *name,
|
int namesize
|
int namesize
|
)
|
)
|
{
|
{
|
struct stat sb;
|
struct stat sb;
|
struct termios tty;
|
struct termios tty;
|
struct dirent *dirp;
|
struct dirent *dirp;
|
DIR *dp;
|
DIR *dp;
|
struct stat dsb;
|
struct stat dsb;
|
char *rval;
|
char *rval;
|
|
|
/* Must be a terminal. */
|
/* Must be a terminal. */
|
if (tcgetattr (fd, &tty) < 0)
|
if (tcgetattr (fd, &tty) < 0)
|
rtems_set_errno_and_return_minus_one(EBADF);
|
rtems_set_errno_and_return_minus_one(EBADF);
|
|
|
/* Must be a character device. */
|
/* Must be a character device. */
|
if (fstat (fd, &sb) || !S_ISCHR (sb.st_mode))
|
if (fstat (fd, &sb) || !S_ISCHR (sb.st_mode))
|
rtems_set_errno_and_return_minus_one(EBADF);
|
rtems_set_errno_and_return_minus_one(EBADF);
|
|
|
if ((dp = opendir (_PATH_DEV)) == NULL)
|
if ((dp = opendir (_PATH_DEV)) == NULL)
|
rtems_set_errno_and_return_minus_one(EBADF);
|
rtems_set_errno_and_return_minus_one(EBADF);
|
|
|
for (rval = NULL; (dirp = readdir (dp)) != NULL ;)
|
for (rval = NULL; (dirp = readdir (dp)) != NULL ;)
|
{
|
{
|
if (dirp->d_ino != sb.st_ino)
|
if (dirp->d_ino != sb.st_ino)
|
continue;
|
continue;
|
strcpy (name + sizeof (_PATH_DEV) - 1, dirp->d_name);
|
strcpy (name + sizeof (_PATH_DEV) - 1, dirp->d_name);
|
if (stat (name, &dsb) || sb.st_dev != dsb.st_dev ||
|
if (stat (name, &dsb) || sb.st_dev != dsb.st_dev ||
|
sb.st_ino != dsb.st_ino)
|
sb.st_ino != dsb.st_ino)
|
continue;
|
continue;
|
rval = name;
|
rval = name;
|
break;
|
break;
|
}
|
}
|
(void) closedir (dp);
|
(void) closedir (dp);
|
return 0;
|
return 0;
|
}
|
}
|
|
|
static char buf[sizeof (_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
|
static char buf[sizeof (_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
|
|
|
/*
|
/*
|
* ttyname() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name
|
* ttyname() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name
|
*/
|
*/
|
|
|
char *ttyname(
|
char *ttyname(
|
int fd
|
int fd
|
)
|
)
|
{
|
{
|
if ( !ttyname_r( fd, buf, sizeof(buf) ) )
|
if ( !ttyname_r( fd, buf, sizeof(buf) ) )
|
return buf;
|
return buf;
|
return NULL;
|
return NULL;
|
}
|
}
|
|
|
|
|