Search code examples
csystemwait

system() and wait() macros


I know that using the call system() is like using fork(),execl() and wait(). So the return of system() is equal to the return of wait(). My question is: how can I use wait() macros (i.e., WIFEXITED(),WEXITSTATUS(), and so on) after using system()? Because macros needs the int to which wait() points, and system() don't give me that int.


Solution

  • system() gives you that int.

    Here's POSIX:

    If command is not a null pointer, system() shall return the termination status of the command language interpreter in the format specified by waitpid().

    Here's Linux man 3 system:

    the return value is a "wait status" that can be examined using the macros described in waitpid(2). (i.e., WIFEXITED(), WEXITSTATUS(), and so on).

    It also comes with an example demonstrating this:

    while (something) {
        int ret = system("foo");
    
        if (WIFSIGNALED(ret) &&
            (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
                break;
    }