In a book about Linux and Unix programming, I´ve found this statement (emphasize mine):
"All functions (
exec()
-family) return-1
in the case of an error. Otherwise at successful execution there is no return back to the calling program. Thus, it is redundant to check the return value; you can directly continue with the error routine."
This seems to fit with the according Linux man page:
"RETURN VALUE - The
exec()
functions return only if an error has occurred. The return value is-1
, anderrno
is set to indicate the error."
Thus, If the current process image was successfully replaced with a new process image, there should be no return. Only if there was happen an error at the creation of the new image, one of the exec()
-functions actually returns (-1
).
Means, in turn, instead of, f.e.:
if ( execl("/bin/foo", "foo", "bar", (char *) NULL) == -1 )
{
perror("Execution of foo failed");
// further error routine.
}
One can simply do:
execl("/bin/foo", "foo", "bar", (char *) NULL);
perror("Execution of foo failed");
// further error routine.
But why do the functions of the
exec()
-family have a return value then in general, if the further program execution of the caller already indicates that an error has happen?Wouldn´t a simple
return;
statement at the end of the functions and a respective return type ofvoid
be sufficient?Is it just to fit the common method/practice to always checking return values or has it a different purpose?
Related: