Exit Codes from system() not as expected

后端 未结 3 756
我在风中等你
我在风中等你 2021-01-18 19:27

The system() function seems to be returning 128 times the exit code I get from the process it\'s evoking.

From the man page:

RETURN VALUE

相关标签:
3条回答
  • 2021-01-18 19:42

    From POSIX system(3):

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

    To get the return code, you need to use the WEXITSTATUS macro.

    0 讨论(0)
  • 2021-01-18 19:58

    Your citation of the system man page misses the relevant part:

    ... This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status).

    You need to right shift the return value by 8 (at least on Linux), to get the commands exit code.

    The portable approach is to just use the macro itself.

    0 讨论(0)
  • 2021-01-18 20:00

    0 and 512 are the termination status of the command.

    If the command executes successfully, it returns a 0 value, otherwise, it returns a non-zero value. And this non-zero value is various in different OSs. In my mac os, the return value of the second system command is 256.

    You can also get answer from this question `ls` exit status.

    0 讨论(0)
提交回复
热议问题