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