Unix C - Portable WEXITSTATUS Unix C - Portable WEXITSTATUS unix unix

Unix C - Portable WEXITSTATUS


OpenBSD's implementation of WEXITSTATUS uses the address-of operator (unary &) on its argument, effectively requiring that its argument have storage. You are calling it with the return value of a function, which doesn't have storage, so the compiler complains.

It is unclear whether OpenBSD's WEXITSTATUS is POSIX-compliant, but the problem can be easily worked around by assigning the return value of pclose() to a variable:

    int status = pclose(proc);    printf("Exit code: %d\n", WEXITSTATUS(status));


As a detail that could go unnoticed for some people arriving here, BSD object code needs the library:

#include <sys/wait.h>

I was too compiling to Linux and BSD, and WEXITSTATUS worked OK without the need for that library (I don't know why) when compiling to Linux (using gcc), but failed when compiling to BSD (using clang).


If your application died or was otherwise killed, the return status is bogus. You need to check the status to see if the exit value is even valid. See the man page for waitpid.

if(WIFEXITED(status)){     use WEXITSTATUS(status);} else if (WIFSIGNALED(status)) {     use WTERMSIG(status);} else {     oh oh}