Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ukma!tut.cis.ohio-state.edu!att!cbnewsl!mpl From: mpl@cbnewsl.ATT.COM (michael.p.lindner) Newsgroups: comp.unix.questions Subject: Re: Finding a stream's filename Summary: can do Message-ID: <1279@cbnewsl.ATT.COM> Date: 27 Jul 89 21:11:08 GMT References: <440@uop.uop.EDU> Organization: AT&T Bell Laboratories Lines: 50 In article <440@uop.uop.EDU>, nsayer@uop.EDU (Nick Sayer) writes: > Can a process either 1) find the filename associated with an fopened > or opened file (and then then let me do it on stdout) or 2) find > out what device it's running on (less nice). > > --------------------------------------------------------------------- > Nick Sayer | ...{ apple!cheers | pacbell!cogent }!quack!mrapple It can be done. Do an fstat(2) on the file descriptor of interest. That gives you the inode number. Then open /dev/ and read the entries until you find the matching inode number. Mike Lindner attunix!mpl AT&T Bell Laboratories 190 River Rd. Summit, NJ 07901 #include #include #include char * find_tty() { static char name[DIRSIZ + 1]; int fd; struct stat sbuf; struct direct dbuf; if (fstat(1, &sbuf)) { /* something has gone wrong */ perror("fstat of stdout"); exit(1); } if ((fd = open("/dev", 0)) < 0) { perror("open of /dev"); exit(1); } name[0] = '\0'; while (read(fd, &dbuf, sizeof(dbuf)) == sizeof(dbuf)) { if (dbuf.d_ino == sbuf.st_ino) { strncpy(name, dbuf.d_name, DIRSIZ); name[DIRSIZ] = '\0'; break; } } close(fd); return name; }