system() 函数似乎返回了我从它所调用的进程中获得的退出代码的 128 倍。
从手册页:
返回值 如果出错(例如,fork(2) 失败)返回的值为 -1,否则返回命令的状态。
这就是我所拥有的。
$ 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
第一次调用表明我没有失败,但返回代码看起来与我从手册页中读取的内容完全不同。我在做什么错?