Hi folks,
from within my program, I call another program using
int retval=system(command);
The called program then returns (under certain conditions) with an exit code, e.g. 13:
exit(13);
However, the value of retval in the first program does not coincide with the latter value. Can someone please explain me how to recover this value in a simple manner?
Thanks a lot!
You have to read the man page for system(3) which refers you to the man page for wait(2). The status contains various info encoded. The bits from 8 upward are the status of the child’s exit. There are some macros mentioned in wait(2) that you should use to separate the pieces of info.
checkfrogger wrote:
> Hi folks,
>
> from within my program, I call another program using
>
> Code:
> --------------------
>
> int retval=system(command);
>
> --------------------
>
> The called program then returns (under certain conditions) with an exit
> code, e.g. 13:
>
> Code:
> --------------------
>
> exit(13);
>
> --------------------
>
> However, the value of retval in the first program does not coincide
> with the latter value. Can someone please explain me how to recover this
> value in a simple manner?
As ever, the man pages contain the information that you seek …
system(3) - Linux man page
“The value returned is -1 on error (e.g. fork() failed), and the return
status of the command otherwise. This latter return status is in the
format specified in wait(2).”
wait(2) - Linux man page
"If status is not NULL, wait() and waitpid() store status information in
the int to which it points. This integer can be inspected with the
following macros (which take the integer itself as an argument, not a
pointer to it, as is done in wait() and waitpid()!):
WIFEXITED(status)
returns true if the child terminated normally, that is, by calling
exit(3) or _exit(2), or by returning from main().
WEXITSTATUS(status)
returns the exit status of the child. This consists of the least
significant 16-8 bits of the status argument that the child specified in
a call to exit() or _exit() or as the argument for a return statement in
main(). This macro should only be employed if WIFEXITED returned true.
WIFSIGNALED(status)
returns true if the child process was terminated by a signal.
WTERMSIG(status)
returns the number of the signal that caused the child process to
terminate. This macro should only be employed if WIFSIGNALED returned true.
WCOREDUMP(status)
returns true if the child produced a core dump. This macro should
only be employed if WIFSIGNALED returned true. This macro is not
specified in POSIX.1-2001 and is not available on some Unix
implementations (e.g., AIX, SunOS). Only use this enclosed in #ifdef
WCOREDUMP … #endif.
WIFSTOPPED(status)
returns true if the child process was stopped by delivery of a
signal; this is only possible if the call was done using WUNTRACED or
when the child is being traced (see ptrace(2)).
WSTOPSIG(status)
returns the number of the signal which caused the child to stop.
This macro should only be employed if WIFSTOPPED returned true.
WIFCONTINUED(status)
(Since Linux 2.6.10) returns true if the child process was resumed
by delivery of SIGCONT."
So the answer to your question is WEXITSTATUS(status)