User-defined errno range values (POSIX or Linux-specific)
The actual errno
values are not defined by the C and C++ standards. So there's no way to return a particular (positive) integer and guarantee it to not clash with the one that an implementation uses.
The C standard requires only three marco:
C11 draft, 7.5 Errors
The macros are
EDOM
EILSEQ
ERANGEwhich expand to integer constant expressions with type int, distinct positive values, and which are suitable for use in #if preprocessing directives;
So you don't know what other errno
values are defined in your implementation.
The errno
values are postive integers in standard C and POSIX. So you could use your own enumaration with negative values to define your own error numbers.
But then you can't use the strerror/perror interfaces. So you may need additional wrapper for strerror/perror to interpret your own error numbers.
Something like:
enum myErrors{
ERR1 = -1,
ERR2 = -2,
...
ERR64 = -64
};
char *my_strerror(int e)
{
if (e>=ERR1 && e<=ERR2)
return decode_myerror(e); // decode_myerror can have a map for
//your error numbers and return string representing 'e'.
else
return strerror(e);
}
and a similar one for perror
.
Note you should also set errno
to 0
before calling your "open-resource" to make sure the errno
was indeed set by your function.
I'd avoid the standard errno altogether in situations like this and define my own enumeration for errors. You could do that if your "open-resource" is not too complicated and returns too possible error codes.