diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-05-14 04:16:35 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-05-14 04:16:35 +0000 |
commit | 64bc6412188b141c010ac3b8e813b837dd991e80 (patch) | |
tree | ffa12b79ea4b13191754f54b872eb1a4f9e3a04b /libc/termios/ttyname.c |
Initial revision
Diffstat (limited to 'libc/termios/ttyname.c')
-rw-r--r-- | libc/termios/ttyname.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/libc/termios/ttyname.c b/libc/termios/ttyname.c new file mode 100644 index 000000000..897243e71 --- /dev/null +++ b/libc/termios/ttyname.c @@ -0,0 +1,45 @@ + +#include <errno.h> +#include <sys/stat.h> +#include <dirent.h> + +char * +ttyname(fd) +int fd; +{ + static char dev[] = "/dev"; + struct stat st, dst; + DIR *fp; + struct dirent *d; + static char name[NAME_MAX]; + int noerr = errno; + + if (fstat(fd, &st) < 0) + return 0; + if (!isatty(fd)) + { + errno = ENOTTY; + return 0; + } + + fp = opendir(dev); + if (fp == 0) + return 0; + strcpy(name, dev); + strcat(name, "/"); + + while ((d = readdir(fp)) != 0) + { + strcpy(name + sizeof(dev), d->d_name); + if (stat(name, &dst) == 0 + && st.st_dev == dst.st_dev && st.st_ino == dst.st_ino) + { + closedir(fp); + errno = noerr; + return name; + } + } + closedir(fp); + errno = noerr; + return 0; +} |