-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_execute.c
61 lines (55 loc) · 2.17 KB
/
ft_execute.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
55
56
57
58
59
60
61
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenamar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/10 11:23:42 by abenamar #+# #+# */
/* Updated: 2023/09/15 10:36:36 by abenamar ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void ft_close_here_documents(t_list *lst)
{
while (ft_tkn_count(lst, "<<", 1))
{
if (!ft_strncmp(lst->content, "<<", 3))
{
lst = lst->next;
close(ft_atoi(lst->content));
}
lst = lst->next;
}
}
static uint8_t ft_default_signals(void)
{
struct sigaction dfl_act;
dfl_act.sa_handler = SIG_DFL;
dfl_act.sa_flags = 0;
if (sigemptyset(&(dfl_act.sa_mask)) == -1)
return (ft_perror("dfl_act sigemptyset: "), 0);
if (sigaction(SIGINT, &dfl_act, NULL) == -1)
return (ft_perror("SIGINT sigaction: "), 0);
if (sigaction(SIGQUIT, &dfl_act, NULL) == -1)
return (ft_perror("SIGQUIT sigaction: "), 0);
return (1);
}
void ft_execute(t_list **tkns, t_list **env)
{
int code;
if (!ft_redirect(tkns, env))
(ft_close_here_documents(*tkns), ft_lstclear(tkns, &free), \
ft_lstclear(env, &free), exit(EXIT_FAILURE));
ft_close_here_documents(*tkns);
if (!ft_default_signals())
(ft_lstclear(tkns, &free), ft_lstclear(env, &free), exit(EXIT_FAILURE));
if (!(*tkns) || !ft_strncmp((*tkns)->content, "|", 2))
(ft_lstclear(tkns, &free), ft_lstclear(env, &free), exit(EXIT_SUCCESS));
code = ft_builtin((*tkns)->content, env, 0);
if (code == 127 && g_signum != SIGTERM)
code = ft_execve((*tkns)->content, env);
ft_lstclear(tkns, &free);
ft_lstclear(env, &free);
exit(code);
}