poddar said:
I am new to this group. I had just one small question
Is it possible to get the file name back from file descriptor.
i.e.
I have written a function to overload the libc write using LD_PRELOAD
my_write(int fd, void *buf, size_t nbytes)
In this function is it possible to find the filename from "fd".
integer file descriptors ('int fd') are not part of C. Those are part
of your implementation's library (most often a Unix-like OS's system
library). In C without such extensions, file I/O is always done
throught FILE * streams, not file descriptors. There is no portable way
to recover any information from that FILE (usually a struct) to which
the FILE * points. The obvious way for my_write to know the name of the
file is for you to pass it as an argument.
You probably want to ask your question in a newsgroup for your platform,
but ...
<OT?
Some systems that support the (non-standard) file descriptor approach
to I/O supply (non-standard) functions with names like __get_fd_name()
which do what you want. You might also want to check for (non-standard)
functions like stat(), fstat(), and lstat() which sometimes uses the
(non-standard) file descriptor to fill in a (non-standard) 'struct
sbuf'. Those structs, even on OS's that support them and the associated
functions, do not typically have the information you want in a
straight-forward way. In any case, any questions about these belong in
a newsgroup for your implementation or OS.
</OT>