file descriptors --> filenames

mark at elsie.UUCP mark at elsie.UUCP
Sat Mar 3 02:03:32 AEST 1984


There is one way you could get a file name from a file descriptor, but it will
be *SLOW*. Use fstat(fd,(struct stat *) buf) to get the inode number and
device for the file (see stat(2) of the manual). Then use ncheck(8) to get the
full path name of the file. E.g.:

fp = popen("ncheck -i '#inode' 'file sys'", "r");
fgets(buf,100,fp);
fgets(buf,100,fp);
pclose(fp);

The second fgets call should retrieve the full file name. Ncheck is slow, it
may take a minute or two to return (it has to do a lot of searching). It
will not, of course, work for fd's that are to devices or pipes. In general,
as others have commented, it is far better to keep the file name around for
latter reference.

-- 
Mark J. Miller
NIH/NCI/DCE/LEC
UUCP:	decvax!harpo!seismo!rlgvax!cvl!elsie!mark
Phone:	(301) 496-5688



More information about the Comp.unix mailing list