popen() 函数通过创建一个管道,调用 fork 产生一个子进程,执行一个 shell 以运行命令来开启一个进程。这个进程必须由 pclose() 函数关闭,而不是 fclose() 函数。pclose() 函数关闭标准 I/O 流,等待命令执行结束,然后返回 shell 的终止状态。如果 shell 不能被执行,则 pclose() 返回的终止状态与 shell 已执行 exit 一样。
pclose返回成功后,可以通过 WIFEXITED, WEXITSTATUS 来获取所执行命令的返回值。
1 //execute shell command
2 int shexec(const char *cmd, char res[][512], int count)
3 {
4 printf("shexec, cmd: %s\n", cmd);
5
6 FILE *pp = popen(cmd, "r");
7 if(!pp) {
8 printf("error, cannot popen cmd: %s\n", cmd);
9 return -1;
10 }
11 int i = 0;
12 char tmp[512];
13 while(fgets(tmp, sizeof(tmp), pp) != NULL) {
14 if(tmp[strlen(tmp)-1] == '\n') {
15 tmp[strlen(tmp)-1] = '\0';
16 }
17 printf("%d.get return results: %s\n", i, tmp);
18 strcpy(res[i], tmp);
19 i++;
20 if(i >= count) {
21 printf("get enough results, return\n");
22 break;
23 }
24 }
25
26 int rv = pclose(pp);
27 printf("ifexited: %d\n", WIFEXITED(rv));
28
29 if (WIFEXITED(rv))
30 {
31 printf("subprocess exited, exit code: %d\n", WEXITSTATUS(rv));
32 }
33
34 return i;
35 }
popen不能返回shell执行错误的结果,可以通过把cmd命令的error信息输出到文件来获取执行状态。
char cmd[64];
char res[10][512];
strcpy(cmd, "ls -l 2> err");
int cnt = shexec(cmd, res, 10);
解析err文件的内容可以得到执行命令失败的状态。