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