Serve La said:
How about let filesize("/var/adm/messages") and others UB?? On some
systems it could return correct values on others an error
fflush(stdin) is UB so i dont see a reason why filesize should have
every single filetype perfectly defined.
And here in clc people will have 1 more reason to tell others that
demons will fly out of their nose when they try filesize() on
/dev/random or something!
Presumably filesize("/dev/random") could behave sensibly without
invoking undefined behavior. The system could detect that it's a
"character special file" and return an error indication. An
implementation of filesize() might fall back to reading every byte of
the file in some cases, but only when necessary. There's nothing
special about filesize("/var/adm/messages"); it's just an ordinary
file whose size happens to change a lot.
The set of functions in the C standard library is not a carefully
engineered coherent interface. The choice of which functions to
include and which to exclude has been largely arbitrary. strdup() is
fairly useful and commonly provided, but it was excluded by the
standard committee back in 1989. gets() is positively dangerous, but
it was included because of historical precedent (and was finally
deprecated nearly 20 years later). strtok() has its own set of
problems. The time interfaced is incomplete (mktime() is the inverse
of localtime(), but no inverse for gmtime() is provided, and asctime()
unnecessarily invokes undefined behavior for out-of-range arguments).
And so forth.
I'll note that Ada, which was first standardized several years before
C was, does have a function to obtain the current size of a file (it
takes Ada's equivalent of a FILE*, not a file name). The designers of
Ada were as concerned with portability as the designers of C; Ada,
like C, runs on everything from small embedded systems to PCs to
mainframes and supercomputers.
A filesize() function, with all its limitations, *could* have been
included in the standard. It wasn't. If it had been, it might have
been useful in some contexts, but you'd have to be very careful in
using it. I suspect that a function that takes a FILE* would be more
useful than one that takes a file name; perhaps both could have been
provided.
But there's always POSIX, which *does* provide this functionality via
the stat() and fstat() functions. The fact is that most, perhaps all,
systems on which filesize() would make sense provide it via an
extension. Yes, it's inconvenient to have to use different interfaces
on different systems.
I have no strong opinion on whether a filesize() function *should*
have been in the C standard. The fact is that it isn't. The lack has
not been a serious problem, since the functionality is generally
available when needed. And adding it to the standard now would be a
long and slow process -- one that is not particularly helped (or hurt)
by talking about it here.
If you really want filesize() in the C standard, one way to start
making that happen is to write a proposal and post it to comp.std.c
for discussion.