-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecute.c
More file actions
70 lines (64 loc) · 2.04 KB
/
execute.c
File metadata and controls
70 lines (64 loc) · 2.04 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aamoros- <aamoros-@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/23 09:09:36 by aamoros- #+# #+# */
/* Updated: 2025/07/07 19:36:34 by aamoros- ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../inc/minishell.h"
static char *validate_and_get_path(t_shell *shell, char **cmd_args, char **env)
{
char *path;
if (!cmd_args || !cmd_args[0])
ft_exit_child(&shell, 1);
if (ft_strchr(cmd_args[0], '/'))
{
if (access(cmd_args[0], F_OK) == -1)
{
ft_perror(shell, "nsfod", cmd_args[0]);
ft_exit_child(&shell, 127);
}
if (access(cmd_args[0], X_OK) == -1)
{
ft_perror(shell, "Permission denied", cmd_args[0]);
ft_exit_child(&shell, 126);
}
return (cmd_args[0]);
}
path = get_cmd_paths(cmd_args[0], env);
if (!path)
{
ft_perror(shell, "cnf", cmd_args[0]);
ft_exit_child(&shell, 127);
}
return (path);
}
void exec_cmd(t_shell *shell, char **cmd_args, char **env)
{
char *path;
path = validate_and_get_path(shell, cmd_args, env);
if (execve(path, cmd_args, env) == -1)
{
ft_perror(shell, "execve failed", cmd_args[0]);
ft_exit_child(&shell, 127);
}
}
void execute(t_shell *shell, char **cmd_args, char **env)
{
if (!shell->commands->next)
{
signal(SIGINT, SIG_DFL);
setup_redirection(shell, true);
exec_cmd(shell, cmd_args, env);
}
else
{
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_IGN);
handle_pipes(shell, env);
}
}