Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return value of system() in C

Tags:

c

return

system

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().

like image 691
Ronin Avatar asked Dec 28 '11 09:12

Ronin


People also ask

What is the return value of system?

The return value of system() is one of the following: * 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 and errno is set to indicate the error.

What does the system function return C?

The system() function is a part of the C/C++ standard library. It is used to pass the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed. <stdlib.

How can I get system call return value?

The return value is the return value from the system call, unless the system call failed. In that case, syscall returns -1 and sets errno to an error code that the system call returned. Note that system calls do not return -1 when they succeed. If you specify an invalid sysno , syscall returns -1 with errno = ENOSYS .

What does system () do in R?

🔗 Base system()/system2() R will convert this into the shell command that writes output to a temporary file, such as below. After the command exits with success, R tries to read the file and return the content.


2 Answers

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.

like image 71
A4L Avatar answered Oct 27 '22 14:10

A4L


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.

like image 23
Sangeeth Saravanaraj Avatar answered Oct 27 '22 14:10

Sangeeth Saravanaraj