home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / libc / gen / ttyname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-01-10  |  908 b   |  50 lines

  1. /*
  2.  * ttyname(f): return "/dev/ttyXX" which the the name of the
  3.  * tty belonging to file f.
  4.  *  NULL if it is not a tty
  5.  */
  6.  
  7. #define    NULL    0
  8. #include <sys/types.h>
  9. #include <sys/dir.h>
  10. #include <sys/stat.h>
  11.  
  12. static    char    dev[]    = "/dev/";
  13. char    *strcpy();
  14. char    *strcat();
  15.  
  16. char *
  17. ttyname(f)
  18. {
  19.     struct stat fsb;
  20.     struct stat tsb;
  21.     struct direct db;
  22.     static char rbuf[32];
  23.     register df;
  24.  
  25.     if (isatty(f)==0)
  26.         return(NULL);
  27.     if (fstat(f, &fsb) < 0)
  28.         return(NULL);
  29.     if ((fsb.st_mode&S_IFMT) != S_IFCHR)
  30.         return(NULL);
  31.     if ((df = open(dev, 0)) < 0)
  32.         return(NULL);
  33.     while (read(df, (char *)&db, sizeof(db)) == sizeof(db)) {
  34.         if (db.d_ino == 0)
  35.             continue;
  36.         if (db.d_ino != fsb.st_ino)
  37.             continue;
  38.         strcpy(rbuf, dev);
  39.         strcat(rbuf, db.d_name);
  40.         if (stat(rbuf, &tsb) < 0)
  41.             continue;
  42.         if (tsb.st_dev==fsb.st_dev && tsb.st_ino==fsb.st_ino) {
  43.             close(df);
  44.             return(rbuf);
  45.         }
  46.     }
  47.     close(df);
  48.     return(NULL);
  49. }
  50.