K
Keith Thompson
jacob navia said:I am not saying that the standard should specify
all possible errors. I am only saying that certain RANGES
of error codes should be specified so that portable programs
could test for i/o errors. This is all the more evident in
fprintf:
r = fprintf(...);
if (r >= MIN_PRINTF_IOERROR && r < MAX_PRINTF_IOERROR ) {
// Error handling for i/o errors
}
That would be portable now you see?
if (r >= MIN_PRINTF_FORMAT_ERROR && r < MAX_PRINTF_FORMAT_ERROR)
// handle printf format errors
What do you DO now to test for fprintf errors?
Well, you have to code:
r = fprintf(....);
#ifdef LINUX
#ifdef TRIO_PRINTF
if (r == ...)
#elif GCC_PRINTF
if (r ==...)
#elif defined(WINDOWS)
#ifdef LCC_WIN32
if (r == ...)
#elif defined(WATCOM)
#elif defined (GCC)
#endif
etc etc, ad nauseum!
Actually, that's unlikely to work.
<SOMEWHAT_OT>
There is no GCC printf. gcc is a compiler, not a complete
implementation; it uses whatever runtime library is provided by the
underlying system. On some systems, that may be the GNU libc.
</SOMEWHAT_OT>
I'm fairly sure that many of the printf() implementations out there
don't use the returned value to distinguish among different kinds of
errors.
A quick look at a small number of implementations (documentation and
source) indicates that some (most?) versions are documented merely to
return a negative value on error; the actual value returned is likely
to be -1. Since this behavior conforms to the standard, I'd say it's
perfectly acceptable.
If you want to propose requiring printf() to return different values
for different kinds of errors, go ahead. Another approach would be to
require printf() to set errno to some meaningful value on errors; I'm
not sure which is better.
C's error handling is, in many ways, weak. Some functions indicate
errors via their returned values, which are too easy to ignore; others
set errno (which has its own problems). I'm not convinced that making
incremental improvements in particular functions is going to be all
that useful. I wouldn't mind seeing some kind of exception handling
mechanism in a future C standard, though not one as elaborate as what
C++ has. But I'm not sure it could be done cleanly without breaking
backward compatibility.
Again, comp.std.c is a better place to have this discussion.