Alain Ketterlin <> writes:
> "BartC" <> writes:
>> Can I use the return value provided to exit() for my own purposes?
>>
>> (This is so that I can pick up the exit-value when I invoke the program from
>> another. BTW what's the best way of invoking a program under Linux so that
>> this value can be retrieved?)
>
> Under Linux (and POSIX), the exit value has no predefined meaning (but a
> limited range, and conventions).
exit(0) and exit(EXIT_SUCCESS) are well defined, both by C and by
POSIX, as an indication of success. exit(EXIT_FAILURE) denotes
failure. (EXIT_FAILURE is typically 1 on POSIX systems, but the
POSIX standard doesn't require that.)
> If you control the whole chain, you're
> probably invoking from a parent process with fork/exec*. Then the parent
> can use wait() to retrieve the exit code (after having checked whether
> the child terminated by calling exit()). If you use higher-level
> interfaces to launch the program (e.g., system()), you may loose some
> accuracy
The value returned by system() is well defined for POSIX; it's the same
as the result returned by waitpid(). If you evaluate
system("some_command");
and some_command calls exit(42), you can reliably get the value 42 from
the result returned by system().
Some specific commands define meanings for exit statuses other than 0 or
1. For example, the grep command exits with a status of 0 on success, 1
on failure, and 2 on error. The curl command defines 88 different error
codes, but that's an extreme case.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"