home *** CD-ROM | disk | FTP | other *** search
- /*
- * ttyname -- figure out the name of the terminal attached to a file
- * descriptor
- *
- * Written by Eric R. Smith and placed in the public domain.
- */
-
- #include <sys/types.h>
- #include <stat.h>
- #include <dirent.h>
- #include <string.h>
-
- extern int __mint;
-
- /* magic number alert! */
- static char tname[32];
-
- /* Find the file in directory "dir" that matches "sbuf". Returns 1
- * on success, 0 on failure. Note that "dir" is a prefix, i.e. it
- * should end with "/".
- */
-
- static int
- find_ino(dir, sb, name)
- char *dir;
- struct stat *sb;
- char *name;
- {
- char *where = name;
- DIR *drv;
- struct dirent *next;
- struct stat testsb;
-
- drv = opendir(dir);
- if (!drv) return 0;
-
- while (*dir) {
- *where++ = *dir++;
- }
-
- while (next = readdir(drv)) {
- strcpy(where, next->d_name);
- if (stat(name, &testsb))
- continue;
- if (testsb.st_dev == sb->st_dev &&
- testsb.st_ino == sb->st_ino) {
- closedir(drv);
- return 1;
- }
- }
- closedir(drv);
- return 0;
- }
-
- char *
- ttyname(fd)
- int fd;
- {
- char *name;
- struct stat sb;
- extern int isatty();
-
- if (!isatty(fd)) return (char *)0;
-
- if (__mint < 9) {
- if (fd == -2) {
- name = "/dev/aux";
- } else {
- name = "/dev/con";
- }
- strcpy(tname, name);
- return tname;
- }
-
- if (fstat(fd, &sb))
- return (char *)0;
-
- /* try the devices first */
- if (find_ino("/dev/", &sb, tname))
- return tname;
-
- /* hmmm, maybe we're a pseudo-tty */
- if (find_ino("u:/pipe/", &sb, tname))
- return tname;
-
- /* I give up */
- strcpy(tname, "/dev/tty");
- return tname;
- }
-