File name from file description

P

poddar

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
 
M

Mark McIntyre

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
 
E

Eric Sosman

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>
 
P

poddar

So in that case is it possible to get

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

amit
 
B

Ben Pfaff

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 assume, by file descriptor, you mean a FILE.
No, C provides no way to get a file name given only a FILE.
 
M

Martin Ambuhl

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>
 
M

Mark McIntyre

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
 
S

SM Ryan

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

In general, no. Some systems allow the file name to be moved
or deleted without invalidating the FILE pointer.
 
F

Flash Gordon

Dik said:
And in many cases also not in a non-standard way. What if there is no
longer a file name associated with the file?

I never claimed there was. On some systems there is, on some there might
be, on some you will get potentially misleading information and on some
there might not be. That's an implementation specific issue that I did
not address because this is not the place for it.
 
K

Kenneth Brody

Dik T. Winter said:
And in many cases also not in a non-standard way. What if there is no
longer a file name associated with the file?

And what if there never was a filename to begin with? (Think stdio and
pipes on *nix.)

However, if you don't mind a subset of FILE*'s, and have control over the
source, a wrapper around fopen()/fclose() can handle this situation. Save
the FILE* and filename somewhere, and then write a function to look through
the saved list and return the corresponding filename.

BTDTGTTS.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top