获取system()返回值

850阅读 0评论2015-03-26 644924073
分类:C/C++

三个宏:
  WIFEXITED(int status) 
    子进程正常退出("exit"或"_exit"),此宏返回非0
  WEXOTSTATUS(int status)
    获得子进程exit()返回的结束代码,一般先要用WIFEXITED判断是否正常结束。
  WIFSIGNALED(int status)
    如果子进程是因为信号而结束则此宏值为真。

  1. int get_system_return(const char *cmd)
  2. {
  3.     int status = system(cmd);
  4.     if (WIFEXITED(status))
  5.     {
  6.         return WEXITSTATUS(status);
  7.     }
  8.     return -1;
  9. }
上一篇:awk 截取字符
下一篇:shell中 dd 命令