1. 程式人生 > >根據system函式返回值確定命令是否執行成功

根據system函式返回值確定命令是否執行成功

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>

int system_cmd(char *cmd)
{
    pid_t status;

    status = system(cmd);

    if (-1 == status)
    {
        printf("create child process fail!\n");
        return -1;
    }
    else
    {
        //printf("exit status value = [0x%x]\n", status);

        if (WIFEXITED(status))
        {
            if (0 != WEXITSTATUS(status))
            {
                printf("run shell script fail.\n");
                return -1;
            }

        }
        else
        {
            printf("exit status = [%d]\n", WEXITSTATUS(status));
            return -1;
        }
    }

}


int main()
{
    system_cmd("twe");
    return 0;
}