logo       

Re: FILE Pointer: msg#00266

gcc.help

Subject: Re: FILE Pointer

On Tue, May 27, 2003 at 09:58:30PM -0700, Muthukumar Ratty wrote:
> On Tue, 27 May 2003, Eljay Love-Jensen wrote:
>
> > Hi Bharathi,
> >
> > >How to get the filename from the filepointer?
> >
> > You cannot.
>
>
> Actually, if it is Linux there is a way to find it (may be in *BSD too?).
> First get the fd entry using "fileno", and then use that in "getdents" to
> get the name.
>
> Look at
> 1. fileno (3) and
> 2. getdents (2)

Actually in general this approach has problems. The file pointed to by the
file descriptor may have been deleted, but the file is still available for use
until the last file descriptor is closed, when the space is recovered. If the
file is not deleted, there is no guarantee that it is located in the current
directory (getdents uses a file descriptor of a directory, not a plain file,
and is what ls uses), so you will need to walk the entire filesystem to look
for the file. Finally with hardlinks, there may be more than one name for the
file.

If you need a name for a file descriptor under Linux, and you have the /proc
filesystem mounted, you can always use:

char *fname (FILE *stream) {
static char name[256];
sprintf (name, "/proc/%d/fd/%d", getpid (), fileno (stream));
return name;
}

--
Michael Meissner
email: meissner@xxxxxxxxxxxxxxxxx
http://www.the-meissners.org



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise