473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

Mar 28 '06 #1
13 2380
On 28 Mar 2006 11:45:02 -0800, in comp.lang.c , "poddar"
<po*******@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
Mar 28 '06 #2
So in that case is it possible to get

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

amit

Mar 28 '06 #3
"poddar" <po*******@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_
Mar 28 '06 #4


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>

--
Er*********@sun.com

Mar 28 '06 #5
Sorry,

I will take care next time

amit

Mar 28 '06 #6
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>
Mar 28 '06 #7
On 28 Mar 2006 12:07:36 -0800, in comp.lang.c , "poddar"
<po*******@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
Mar 28 '06 #8
"poddar" <po*******@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) ks***@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.
Mar 28 '06 #9
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
Mar 28 '06 #10
"poddar" <po*******@gmail.com> wrote:
# 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.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
So....that would make Bethany part black?
Mar 28 '06 #11
In article <if************@news.flash-gordon.me.uk> Flash Gordon <sp**@flash-gordon.me.uk> writes:
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.


And in many cases also not in a non-standard way. What if there is no
longer a file name associated with the file?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Mar 29 '06 #12
Dik T. Winter wrote:
In article <if************@news.flash-gordon.me.uk> Flash Gordon <sp**@flash-gordon.me.uk> writes:
> 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.


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.
--
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
Mar 29 '06 #13
"Dik T. Winter" wrote:

In article <if************@news.flash-gordon.me.uk> Flash Gordon <sp**@flash-gordon.me.uk> writes:
> 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.


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:Th*************@gmail.com>

Mar 29 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: j erickson | last post by:
with the following xsl and xml file, the display of the gif file with the <image/url> tag works. However, the gif file in the <description> tag using the name attribute "src" won't make the correct...
0
by: delphiconsultingguy | last post by:
Hi all, Spent WAAAYYY too much time trying to figure this out because there's not many good examples out there, so in the interest of sparing y'all from suff'rin same, I've pasted it into...
3
by: Eric Lilja | last post by:
Sorry for asking so many questions, but I've just started and need to get some things working so I can do the task that is before me. Consider this (validating) schema: <?xml version="1.0"?>...
4
by: BorisBoshond | last post by:
Hi all, Hope someone is able and willing to help me with following problem. I received a xsd file from another company, our company i supposed to return xml based on that xsd. Problem is that I...
21
nathj
by: nathj | last post by:
Hi, I am currently working on a new site that offers various files for download. The file information is stored in a MySQL database and the page is produced in PHP depending on how the user got...
2
by: hotflash | last post by:
Hi All, I found the best pure ASP code to upload a file to either server and/or MS Access Database. It works fine for me however, there is one thing that I don't like and have tried to fix but...
4
cassbiz
by: cassbiz | last post by:
Could use some help here. This script is carrying over an image just fine but the text isn't coming over. can you see why it is not working???? from the form I want to carry over two lines of...
2
by: freddukes | last post by:
Okay... I'm a PHP noob but I have a good background in C++ and Python... Now all I want to do is iterate through the following file, and appending all products to an array, with information stored...
1
by: raghudr | last post by:
Hi all, I am parsing an .xml file.My main intention is to retrieve the field value:- "Name Value" which is "rag" and store it in a List. Fot that i wrote code like this: //i am using...
0
by: Hags007 | last post by:
I have a XML file I am working with. This file has been created by hand and I now need to develop a PHP script that will create it in the same format. Here is what I have thus far: $query =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.