home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / ttyname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.6 KB  |  78 lines

  1. /*  ttyname(3)
  2.  */
  3. #include "lib.h"
  4.  
  5. #include <sys/types.h>
  6. #include <sys/dir.h>
  7. #include <sys/stat.h>
  8.  
  9. #ifndef NULL
  10. #ifdef __STDC__
  11. #define  NULL     (void *) 0
  12. #else
  13. #define  NULL     (char *) 0
  14. #endif
  15. #endif
  16.     
  17. #define  DEV    "/dev/"
  18.     
  19. static char file_name[40];
  20.  
  21.  
  22. char *ttyname( tty_file_desc )
  23. int tty_file_desc;
  24. {
  25.     int dev_dir_desc;
  26.     struct direct dir_entry;
  27.     struct stat tty_stat;
  28.     struct stat dev_stat;
  29.     
  30.     /*  Make sure the supplied file descriptor is for a terminal  */
  31.     
  32.     if ( fstat(tty_file_desc, &tty_stat) < 0 )
  33.     return( (char *)NULL );
  34.     
  35.     if ( (tty_stat.st_mode & S_IFMT) != S_IFCHR )
  36.     return( (char *)NULL );
  37.     
  38.     
  39.     /*  Open /dev for reading  */
  40.     
  41.     if ( (dev_dir_desc = open(DEV,0)) < 0 )
  42.     return( (char *)NULL );
  43.     
  44.     while( read(dev_dir_desc, (char *) &dir_entry,
  45.         (unsigned int)sizeof (struct direct)) > 0 ) 
  46.     {
  47.     /*  Find an entry in /dev with the same inode number  */
  48.     
  49.     if ( tty_stat.st_ino != dir_entry.d_ino )
  50.         continue;
  51.     
  52.     /*  Put the name of the device in static storage  */
  53.     
  54.     strcpy( file_name, DEV );
  55.     strncat( file_name, dir_entry.d_name,
  56.         (unsigned int)sizeof (dir_entry.d_name) );
  57.     
  58.     /*  Be absolutely certain the inodes match  */
  59.     
  60.       if ( stat(file_name, &dev_stat) < 0 )
  61.         continue;
  62.     
  63.       if ( (dev_stat.st_mode & S_IFMT) != S_IFCHR )
  64.         continue;
  65.     
  66.       if ( tty_stat.st_ino  == dev_stat.st_ino  &&
  67.         tty_stat.st_dev  == dev_stat.st_dev  &&
  68.         tty_stat.st_rdev == dev_stat.st_rdev )
  69.     {
  70.         close( dev_dir_desc );
  71.         return( file_name );
  72.     }
  73.     }
  74.     
  75.     close( dev_dir_desc );
  76.     return( (char *)NULL );
  77. }
  78.