-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_commands.c
54 lines (48 loc) · 908 Bytes
/
execute_commands.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
49
50
51
52
53
54
#include "shell.h"
/**
* execute_commands - to execute comands.
*
* @tokens: arguments to execute.
* @av: argument values.
* @env: environment.
*
* Return: void.
*/
void execute_commands(char **tokens, char **av, char **env)
{
pid_t child_pid;
int stat;
if (tokens == NULL || tokens[0] == NULL)
return;
if (tokens[0][0] != '.' && tokens[0][0] != '/')
{
if (custom_commands(tokens, env))
return;
else if (!get_accessable_path(tokens, env))
{
print_execute_error(2, av, tokens[0]);
return;
}
}
child_pid = fork();
if (child_pid == -1)
{
_print(2, av, " :error in creating process\n");
return;
}
if (child_pid == 0)
{
if (execve(tokens[0], tokens, env) == -1)
{
print_execute_error(2, av, tokens[0]);
free_arr(tokens);
exit(1);
}
}
else
{
do {
waitpid(child_pid, &stat, WUNTRACED);
} while (!WIFEXITED(stat) && !WIFSIGNALED(stat));
}
}