-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_executor.c
More file actions
41 lines (36 loc) · 878 Bytes
/
command_executor.c
File metadata and controls
41 lines (36 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "shell.h"
/**
* exec_cmd - Executes a command with arguments and environment.
* @command: The full path to the command.
* @args: An array of strings representing the command and its arguments.
* @env: An array of strings representing the environment.
*
* Returns: On success, it does not return. On failure, -1 is returned.
*/
int exec_cmd(const char *command, char *const args[], char *const env[])
{
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
return -1;
}
else if (pid == 0)
{
if (execve(command, args, env) == -1)
{
perror(command);
_exit(EXIT_FAILURE);
}
}
else
{
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
{
return -1;
}
}
return 0;
}