I need execute a command which redirects output to /dev/null. How to execute commands like this using exec in C ?
My command is ls logs/ >/dev/null 2>/dev/null. To execute this command, I am first splitting the command with space as delimiter and then executing using exec. When executing >/dev/null 2>dev/null are passed as arguments to the script. How we can avoid this ?
I don't want to use system command to overcome this problem.
CODE :
static int
command_execute(char* command) {
char **arguments = g_strsplit(command, " ", 32);
pid_t child_pid;
if( !arguments ) {
status = COMMAND_PARSE_ERROR;
goto EXIT;
}
if( (child_pid = fork() ) == -1 ) {
status = CHILD_FORK_ERROR;
goto EXIT;
}
if( child_pid == 0 ) {
execvp(arguments[0], arguments);
} else {
waitpid(child_pid, &status, 0);
if(status) {
goto EXIT;
}
}
}