-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_shell.c
49 lines (45 loc) · 862 Bytes
/
execute_shell.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
#include "shell.h"
/**
* execute_commands - executes the commands passed to the shell
* @shell: struct containing data fed to the shell
*
* Return: 0 on success
*/
int execute_commands(shell_data *shell)
{
pid_t child_pid;
int status, code = 0;
char *path = NULL;
code = is_exe(shell);
if (code == -1)
return (1);
if (code == 0)
{
path = handle_path(shell);
if (check_execute_permissions(path, shell) == 1)
return (1);
}
child_pid = fork();
if (child_pid == 0)
{
if (code == 0)
path = handle_path(shell);
else
path = shell->tokens[0];
execve(path + code, shell->tokens, shell->env);
}
else if (child_pid < 0)
{
perror(shell->exe);
return (1);
}
else
{
wait(&status);
if (WIFEXITED(status))
errno = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
errno = 128 + WTERMSIG(status);
}
return (1);
}