#include <stdlib.h>
int system(const char *command);
* If command is NULL, then a nonzero value if a shell is available, or 0 if no shell is available.
* If a child process could not be created, or its status could not be retrieved, the return value is -1.
* If a shell could not be executed in the child process, then the return value is as though the child shell terminated by calling _exit(2) with the status 127.
* If all system calls succeed, then the return value is the termination status of the child shell used to execute command. (The termination status of a shell is the
termination status of the last command it executes.)
In the last two cases, 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).
system() does not affect the wait status of any other children.
tianyxu@TIANYXU-M-V83Z glibc % git grep __WEXITSTATUS
bits/waitstatus.h:#define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)
bits/waitstatus.h:#define __WSTOPSIG(status) __WEXITSTATUS(status)
posix/sys/wait.h:# define WEXITSTATUS(status) __WEXITSTATUS (status)
stdlib/stdlib.h:# define WEXITSTATUS(status) __WEXITSTATUS (status)
The status stored by waitpid() encodes both the reason that the child process was terminated and the exit code. The reason is stored in the least-significant byte (obtained by status & 0xff), and the exit code is stored in the next byte (masked by status & 0xff00 and extracted by WEXITSTATUS()). When the process terminates normally, the reason is 0 and so WEXITSTATUS is just equivalent to shifting by 8 (or dividing by 256). However, if the process is killed by a signal (such as SIGSEGV), there is no exit code, and you have to use WTERMSIG to extract the signal number from the reason byte.
比如如果system执行grep但是没抓住字符,它的返回值就是256,WEXITSTATUS(rc)就是1。