日期:2014-05-16  浏览次数:20834 次

popen 用法中:an exit status of 127 ?
如: if ((outPut = popen (command, "r")) != NULL){
  printf("output != NULL \n");
  }

 即使命令 command 是错误的 ,output 也不为 NULL ! 查看 man popen 后得知 output 为 NULL 的三种情况:

 The popen() function returns NULL if the fork(2) or pipe(2) calls fail, or if it cannot allocate memory.

 现在我希望判断 command 是否执行出错怎么办[color=#FF0000][/color],如 我 本想 ls 却写错给了 la 给 command。 

 答案也许在下面那句:
 Failure to execute the shell is indistinguishable from the shell’s 
 failure to execute command, or an  
 immediate exit of the command. The only hint is an exit status of 127. 
 那我如何获得 The only hint is an exit status of 127 ? 进而推出 命令执行出错

------解决方案--------------------
不同命令出错的提示是不一样的
不存在的命令在不同系统下的执行结果貌似也不同

------解决方案--------------------
这个127是popen实现中子进程退出码,一般命令出错stream中的输出内容和在shell中执行这条命令的结果相同,可以判断是不是出现not found这样的结果来判断是否出错。
------解决方案--------------------
同意3L
------解决方案--------------------
看看popen源代码吧,这个比较靠谱