D
Dik T. Winter
>
> For stdin stdout and stderr "..stdin.." "..stdout.." and "..stderr.."
> is returned.
ksh: ls -a
.. .. ..stderr.. ..stdin.. ..stdout..
ksh:
>
> For stdin stdout and stderr "..stdin.." "..stdout.." and "..stderr.."
> is returned.
Ben Bacarisse said:How would you deal with hard links? Soft links (aliases)? Streams
opened by functions other than fopen() or freopen()? Are you
returning simply the file name, or the full path name (if you're
talking about error messages, the full path name would be useful)? If
there is no name associated with the file, would you return an empty
string or NULL? How would you distinguish between streams that had no
name to begin with vs. streams where the name could not be stored due
to an error?
This is one of the odd argument where I find myself on both sides. I
think these problems miss the point -- it always up the programmer to
know if a file name makes any sense, and it seems all Jacob is
suggesting is that he store what use at the point of the open call and
offer to return it later[1]. (Personally I would not copy it. I'd
store the pointer used in the fopen/freopen and hand that back when
asked.)
[1] If he is proposing something stronger (like trying to find some
canonical representation of the opened file's name and storing that)
then I agree that these criticisms are valid, but I thought (at least
at first) he was simply suggesting that the pointer be stored.
For tempfile() should return... the name of the temporary file of course
For stdin it will return an invalid file name (either NULL or
"....stdin...." or similar.
For stdout/stderr the same.
fname receives a FILE *. not a file descriptor
For stdin stdout and stderr "..stdin.." "..stdout.." and "..stderr.."
is returned.
Nick Keighley said:is this documented in the standard? There's mention in this
thread of OSs that support 10,000 (or more) open files. Do they
create 10,000 file structures on startup? Sounds a bit wasteful...
In that case it might be better to consistently return a string and
never NULL. For example, "..unknown.." or "..closed..".
Are filenames starting ".." impossible? They aren't on other systems,
so if you have an idea that this might be adopted elsewhere you might
want to use something different, though I can't see a nice solution.
I repeat: fname returns the first argument of fopen.
Richard Heathfield said:Ben Bacarisse said:
[...] it seems all Jacob is
suggesting is that he store what use at the point of the open call and
offer to return it later[1]. (Personally I would not copy it. I'd
store the pointer used in the fopen/freopen and hand that back when
asked.)
Alas, that won't work. Consider:
size_t n = strlen(argv[1] + sizeof ".txt";
char *name = malloc(n);
if(name != NULL && sprintf(name, "%s.txt", argv[1]))
{
FILE *fp = fopen(name, "w");
free(name);
if(fp != NULL)
{
char *whatever = malloc(n);
if(whatever != NULL)
{
sprintf(whatever, "%s.foo", argv[1]);
Perfectly legal C, but now name no longer points anywhere legal. The second
call to malloc may well return the same address as the first call (because
there is an intervening free and the requested size is after all the same
as before). So a call to _fname or whatever, at this point, would return
the wrong name.
Antoninus Twink said:Jacob has said that he will return NULL rather than the empty string if
the filename isn't available, so he could adopt the convention that if
_fname(fp) isn't null, and if *_fname(fp) is '\0', then _fname(fp)+1
points to a "special string" like "..stdin.." or whatever.
This will be
fine unless you want to port to a system where filenames can contain
'\0' or where filenames can be zero-length.
[/QUOTE]I repeat: fname returns the first argument of fopen.
So, what possible advantage does this have over saving that name
yourself?
Isn't that the C philosophy: you don't pay for what you don't want?CBFalconer said:Exactly. So now you see the savings in not having fname.
Ian said:Isn't that the C philosophy: you don't pay for what you don't want?
How often do people want to associate a file name with a FILE object?
I
can't think of a time when I have and I certainly would want either the
time or space overhead on a embedded system.
Quite, the disk region is distinct from one of its possible names. Asjacob said:Every time
fopen(filename,mode);
A FILE object *is* an association between a name and a disk region.
A buffer has to be allocated and and the name copied. True this mayThere is no time or overhead involved here. The passed pointer is copied
and that is it. The overhead is minimal compared to all the fields,
buffers and other things fopen must do.
Which would be incredibly hard to do given the large number of fileThis is a first step in a more general set of functions to get
the properties of a file.
Isn't that the C philosophy: you don't pay for what you don't want?
How often do people want to associate a file name with a FILE object?
I
can't think of a time when I have and I certainly would want either the
time or space overhead on a embedded system.
Every time
fopen(filename,mode);
A FILE object *is* an association between a name and a disk region.
There is no time or overhead involved here. The passed pointer is copied
and that is it.
The overhead is minimal compared to all the fields,
buffers and other things fopen must do.
This is a first step in a more general set of functions to get
the properties of a file.
The name is know at the point where the file is opened, otherwise itNick said:sorry, no. What is the saving? (I'm not, incidently, in favour
of Jacob's proposal- but I think the criticism should
be rational and clear).
every time there's an error reading a file.
Nick Keighleywrote:
The name is know at the point where the file is opened, otherwise it
would be a little difficult opening it...
Oops..Nick said:I said *reading* not opening. And you snipped the example
Nick Keighley said:I said *reading* not opening. And you snipped the example
It's called "memory Nick". Try "remembering" the name in a "variable" or
similar. It's complicated but good
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.