Reputation: 2077
I am using the system()
command in C to execute commands like sc query mysql
or net start mysql
.
If the parameter is null pointer then it returns 1 if the cmd processor is OK, otherwise it returns 0. On successful command execution it returns 0.
My question is: Can I get a list of its return values? Like what it will return if the command is invalid or what the return value on unsuccessful execution will be? I want to do different things depending on the return value of system()
.
Upvotes: 17
Views: 105002
Reputation: 16627
All you need to do is man system
to know more about system()
DESCRIPTION system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.
RETURN VALUE The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127). If the value of command is NULL, system() returns nonzero if the shell is available, and zero if not.
Upvotes: 11
Reputation: 17595
As the docs
state system() return -1
if creating the new process for the command to be executed fails, otherwise it returns the exit code of the command executed. this is the same value you can retrieve using echo $?
in unix or echo %ERRORLEVEL%
in windows after executing the same command in a shell. So if you want to handle the return values you have to look at what the commands executed return.
Upvotes: 13
Reputation: 229344
system() returns the exit code of the process you start.
The exit codes generally only have the convention that an exit code of 0 means success and non-zero means failure. For the actual meaning of the various exit codes, they're specific to each program, ofthen at the whim of the programmer. You will have to look up the documentation of the particular program you're running (Though more often than not, it's not documented, so you will have to read through the source code)
Upvotes: 6