The return value of the system () in C

I use the system() command in C to execute commands like sc query mysql or net start mysql .

If the parameter has a null pointer, it returns 1, if the cmd processor is OK, otherwise it returns 0. If the command succeeds, it returns 0.

My question is: can I get a list of return values? How is it that it will return if the command is invalid or what will be the return value if it fails? I want to do different things depending on the return value of system() .

+6
source share
3 answers

Like docs state system () return -1 , if the creation of a new process for the executed command fails, otherwise it returns the completion code of the executed command. is this the same value you can get with echo $? on unix or echo %ERRORLEVEL% in windows after executing the same command in the shell. Therefore, if you want to handle return values, you need to see what commands returned.

+8
source

All you have to do is man system to learn more about system()

DESCRIPTION system () executes the command specified in the command by calling / bin / sh -c, and returns after the command has been completed. During the execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

RETURN VALUE Return value: -1 on error (for example, fork (2) failed) and the return status of the command otherwise. This last return status is in the format indicated by pending (2). Thus, the output of the command code will be WEXITSTATUS (status). In the event that / bin / sh may fail, the exit status will be that of the command that makes the exit (127). If the command value is NULL, system () returns a non-zero value if the shell is available, and zero if not.

+11
source

system () returns the exit code for the process you are starting.

Exit codes usually only have an agreement that exit code 0 means success, and nonzero means failure. For the actual meaning of the various exit codes, they are specific to each program, and then at the whim of the programmer. You will need to find the documentation for the specific program that you are using (although most often this is not documented, so you will have to read the source code)

+2
source

Source: https://habr.com/ru/post/904588/


All Articles