Getting the pathname from a FILE*.

Leo de Wit leo at philmds.UUCP
Sun Jul 10 07:02:32 AEST 1988


In article <651 at umb.umb.edu> karl at umb.umb.edu (Karl Berry.) writes:
>
>The title says it all. Am I missing something obvious?
>The pathname doesn't seem to be a field in either the
>_iobuf or the structure returned by stat.
>ttyname or ctermid will give you the pathname
>of your terminal, but I want the pathname of an
>arbitrary FILE* I have fopen. Or am I missing some
>reason why this is not feasible?

This is not trivial. Think for instance of multiple links to a file
(either hard or symbolic links); which name will you use? Or what if
one of the directories in the path has multiple links - apart from the
trivial ones (for example /usr/include/sys, which on our system ==
/usr/sys/h) ? Or what if the FILE * is connected to a pipe? Or what if
the file is already gone (i.e. unlinked but still open) ?

VMS has a function for it: fgetname().
On Unix you could fstat the file descriptor (the char _file member from
the _iobuf struct). This gives you the inode number; now go look for
the filename(s).

What is the use? If you have opened the file, you used the filename 
already (unless the open file is inherited from a parent process).

                   Leo.



More information about the Comp.unix.wizards mailing list