> When should a library function return EINVAL (Invalid argument)? My
> feeling is, it's only proper when it would have been trivial for the
> user to predict the failure. But I don't know where I have that from.
> This is for a C library whose functions return 0, a system errno, or
> some negative private code. I know system errnos could be negative,
> I think it was on BeOS, but I don't care until I meet such a beast.
It's not really clear to me what you mean by "return". Do you
intend to have a return value of a function of EINVAL or do
you intend to set errno to EINVAL and return some value
from your functions that tells the user "something went
wrong and please have a look at 'errno' for the details"?
If you write your own library I'd consider "private" error
codes to be returned from functions to be the best method.
Values like EINVAL are, to me at least, associated with
'errno'. If you have error returns try to stay consistent
and either return a library specific error number (well
documented, of course;-) or return -1 on failure and set
'errno' (in which case every error should map unambiguous-
ly to the existing errno codes, so don't invent new ones -
macros starting with 'E' are actually reserved for the sys-
tem).
Don't start mixing them. What would be the use to a user of
flagging via a private code "some system error happend" with
errno being set to some value indicating why the system error
happened? The user won't know which system call failed and thus
the information from errno is rather useless for him or her (un-
less your library is a very thin layer on top of some system
calls and even a novice will have no problems figuring out
what probably happened under the hood). So better stay con-
sistent - have a set of error values returned that clearly in-
dicate what went wrong, so the user doesn't have to do guess-
work games.
> I'm looking at some EINVALs which I think ought to be something
> else. Transaction is now unusable, Arg > internal constant, etc.
I don't see it that way - EINVAL is something errno gets set
to. Don't mix that up with return values from your library.
Have an error like "MY_LIB_INVALID_ARGUMENT" as a return
value and don't "overload" what EINVAL means (i.e. the valued
of errno after a system call failed, returning -1 due to an
invalid function argument).
Regards, Jens
--
\ Jens Thoms Toerring ___
j...@toerring.de
\__________________________
http://toerring.de