1 /* ttyname.c POSIX 4.7.2
2 * char *ttyname(int fildes);
4 * Determines name of a terminal device.
15 PRIVATE
char base
[] = "/dev";
16 PRIVATE
char path
[sizeof(base
) + 1 + NAME_MAX
]; /* extra 1 for '/' */
18 PUBLIC
char *ttyname(fildes
)
26 /* Simple first test: file descriptor must be a character device */
27 if (fstat(fildes
, &tty_stat
) < 0 || !S_ISCHR(tty_stat
.st_mode
))
30 /* Open device directory for reading */
31 if ((devices
= opendir(base
)) == (DIR *) NULL
)
34 /* Scan the entries for one that matches perfectly */
35 while ((entry
= readdir(devices
)) != (struct dirent
*) NULL
) {
36 if (tty_stat
.st_ino
!= entry
->d_ino
)
40 strcat(path
, entry
->d_name
);
41 if (stat(path
, &dev_stat
) < 0 || !S_ISCHR(dev_stat
.st_mode
))
43 if (tty_stat
.st_ino
== dev_stat
.st_ino
&&
44 tty_stat
.st_dev
== dev_stat
.st_dev
&&
45 tty_stat
.st_rdev
== dev_stat
.st_rdev
) {