-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_functions.c
48 lines (46 loc) · 1.08 KB
/
execute_functions.c
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
41
42
43
44
45
46
47
48
#include "shell.h"
/**
* exec_command - Function executes a command on CLI
* with the given arguments.
* @argv: Array of strings for the command and its arguments.
* Return: Executable file if found
* Else: Error
*/
int exec_command(char *argv[])
{
char *command = NULL, *new_command = NULL;
pid_t child_pid = -1;
int status = 0;
if (argv == NULL || argv[0] == NULL)
return (1);
command = argv[0];
new_command = find_command_path(command);
if (new_command == NULL)
return (1);
if (argv && access(new_command, X_OK) != -1)
{
child_pid = fork();
if (child_pid == -1)
{
display_error_message(argv, "Fork Error:");
}
else if (child_pid == 0)
{
if (execve(new_command, argv, NULL) == -1)
display_error_message(argv, "Execve Error:");
exit(0);
}
else
{
if (waitpid(child_pid, &status, 0) == -1)
display_error_message(argv, "Waitpid Error:");
}
if (_strncmp(new_command, command, _strlen(new_command)) != 0)
free(new_command);
if (WIFEXITED(status))
status = WEXITSTATUS(status);
return (status);
}
free(new_command);
return (1);
}