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