ttyname.c

Go to the documentation of this file.
00001 /* ttyname.c                                            POSIX 4.7.2
00002  *      char *ttyname(int fildes);
00003  *
00004  *      Determines name of a terminal device.
00005  */
00006 
00007 #include <lib.h>
00008 #include <sys/stat.h>
00009 #include <dirent.h>
00010 #include <fcntl.h>
00011 #include <stddef.h>
00012 #include <string.h>
00013 #include <unistd.h>
00014 
00015 PRIVATE char base[] = "/dev";
00016 PRIVATE char path[sizeof(base) + 1 + NAME_MAX]; /* extra 1 for '/' */
00017 
00018 PUBLIC char *ttyname(fildes)
00019 int fildes;
00020 {
00021   DIR *devices;
00022   struct dirent *entry;
00023   struct stat tty_stat;
00024   struct stat dev_stat;
00025 
00026   /* Simple first test: file descriptor must be a character device */
00027   if (fstat(fildes, &tty_stat) < 0 || !S_ISCHR(tty_stat.st_mode))
00028         return (char *) NULL;
00029 
00030   /* Open device directory for reading  */
00031   if ((devices = opendir(base)) == (DIR *) NULL)
00032         return (char *) NULL;
00033 
00034   /* Scan the entries for one that matches perfectly */
00035   while ((entry = readdir(devices)) != (struct dirent *) NULL) {
00036         if (tty_stat.st_ino != entry->d_ino)
00037                 continue;
00038         strcpy(path, base);
00039         strcat(path, "/");
00040         strcat(path, entry->d_name);
00041         if (stat(path, &dev_stat) < 0 || !S_ISCHR(dev_stat.st_mode))
00042                 continue;
00043         if (tty_stat.st_ino == dev_stat.st_ino &&
00044             tty_stat.st_dev == dev_stat.st_dev &&
00045             tty_stat.st_rdev == dev_stat.st_rdev) {
00046                 closedir(devices);
00047                 return path;
00048         }
00049   }
00050 
00051   closedir(devices);
00052   return (char *) NULL;
00053 }

Generated on Fri Apr 14 22:57:27 2006 for minix by  doxygen 1.4.6