In theory, it always seems to me that system() should do this:
Originally Posted by
man system
The value returned is -1 on error (e.g. fork(2) failed), and the
return status of the command otherwise.
In practice, this does not work (try it). However, if you use fork/exec/waitpid the "status pointer" (2nd arg) might work.
Here's a hack method that will do it:
Code:
!#/bin/bash
./test
echo $?
Here "test" is a local executable (eg, hello world), and the correct return value is reported. So you could run the process via such a script and grab the return value on stdout. Alternately you could export an environment variable and access that.
But that is definitely an unportable hack method. Curious to see if other people have the same experience with system:
Code:
#include <stdio.h>
int main(int argc, const char *argv[]) {
int x = system("./test");
printf("%d\n",x);
return 0;
}
I do not get a correct value, wheras the bash script does.