f



File name from file description

Hi,

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".

thanks
amit

0
poddar007 (16)
3/28/2006 7:45:02 PM
comp.lang.c 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

9 Replies
797 Views

Similar Articles

[PageSpeed] 21

On 28 Mar 2006 11:45:02 -0800, in comp.lang.c , "poddar"
<poddar007@gmail.com> wrote:

>Hi,
>
>I am new to this group. I had just one small question
>
>Is it possible to get the file name back from file descriptor.

C doesn't have file descriptors. You want a unix programming group if
you're using write() and its friends. 

Mark McIntyre
-- 
"Debugging is twice as hard as writing the code in the first place. 
 Therefore, if you write the code as cleverly as possible, you are, 
 by definition, not smart enough to debug it."
--Brian Kernighan
0
markmcintyre (4555)
3/28/2006 7:55:35 PM

poddar wrote On 03/28/06 14:45,:
> Hi,
> 
> I am new to this group.

    If you had taken the trouble to read the group for
a few days before leaping in with great haste and little
care, you would have known that your question is off-topic
here.

> I had just one small question
> 
> Is it possible to get the file name back from file descriptor.

<off-topic>

    No.

</off-topic>

-- 
Eric.Sosman@sun.com

0
Eric.Sosman (4552)
3/28/2006 8:05:35 PM
So in that case is it possible to get

file name from file pointer (i.e. FILE *fp)

amit

0
poddar007 (16)
3/28/2006 8:07:36 PM
"poddar" <poddar007@gmail.com> writes:

> 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 assume, by file descriptor, you mean a FILE.
No, C provides no way to get a file name given only a FILE.
-- 
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
0
blp (3955)
3/28/2006 8:08:37 PM
Sorry,

I will take care next time

amit

0
poddar007 (16)
3/28/2006 8:12:35 PM
poddar wrote:

> 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>
0
mambuhl (2203)
3/28/2006 8:13:18 PM
On 28 Mar 2006 12:07:36 -0800, in comp.lang.c , "poddar"
<poddar007@gmail.com> wrote:

>So in that case is it possible to get
>
>file name from file pointer (i.e. FILE *fp)

No, you need to keep track of that yourself, or use OS-specific
language extensions. 
Mark McIntyre
-- 
"Debugging is twice as hard as writing the code in the first place. 
 Therefore, if you write the code as cleverly as possible, you are, 
 by definition, not smart enough to debug it."
--Brian Kernighan
0
markmcintyre (4555)
3/28/2006 8:28:32 PM
"poddar" <poddar007@gmail.com> writes:
> Sorry,
>
> I will take care next time

And take care to read <http://cfaj.freeshell.org/google/>.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
0
kst-u (21963)
3/28/2006 8:50:09 PM
poddar wrote:
> So in that case is it possible to get
> 
> file name from file pointer (i.e. FILE *fp)

No, not in any standard way.
-- 
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
0
spam331 (4048)
3/28/2006 9:29:57 PM
Reply: