The system() function seems to be returning 128 times the exit code I get from the process it’s evoking.
From the man page:
RETURN VALUE
The value returned is -1 on error (e.g., fork(2)
failed), and the return status of the command other‐
wise.
Here is what I’ve got.
$ ls tinker.c
tinker.c
$ echo $?
0
$ ls notexisting
ls: cannot access notexisting: No such file or directory
$ echo $?
2
$ cat tinker.c
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("%d\n", system("ls tinker.c"));
printf("%d\n", system("ls notexisting"));
return 0;
}
$ gcc tinker.c -o tinker
$ ./tinker
tinker.c
0
ls: cannot access notexisting: No such file or directory
512
The first call indicates that I’m not getting failures but the return codes look nothing like what I’m reading from the man page. What’m I doing wrong?
From POSIX
system(3):To get the return code, you need to use the
WEXITSTATUSmacro.