【发布时间】:2012-11-06 16:05:55
【问题描述】:
system() 函数似乎返回了我从它所调用的进程中获得的退出代码的 128 倍。
来自手册页:
返回值 错误时返回的值为 -1(例如,fork(2) 失败),以及命令的返回状态 other- 明智的。
这就是我所拥有的。
$ 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
第一次调用表明我没有遇到失败,但返回代码看起来与我从手册页中读取的内容完全不同。我做错了什么?
【问题讨论】: