-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathms_exec.c
More file actions
executable file
·72 lines (65 loc) · 2.06 KB
/
ms_exec.c
File metadata and controls
executable file
·72 lines (65 loc) · 2.06 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
71
72
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jheo <jheo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 21:09:00 by jinsecho #+# #+# */
/* Updated: 2024/12/22 17:42:56 by jheo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void exec_fd_dup(t_plst *tmp)
{
if (tmp->heredoc_true)
dup2(tmp->heredoc_fd[0], STDIN_FILENO);
if (tmp->rdr_true)
dup2(tmp->file_fd, STDOUT_FILENO);
}
void exec_exit_sts(t_cmd *cmd, t_plst *tmp, int pid, int exit_sts)
{
int sig;
if (ms_builtin_func(cmd, tmp))
{
pid = (int)fork();
if (pid == 0)
{
ms_term_reset(cmd, 0);
cmd_path_cat_exec(cmd, tmp);
}
ms_term_set(cmd, 1);
waitpid(pid, &exit_sts, 0);
if (WIFEXITED(exit_sts))
cmd->sts.process_status = WEXITSTATUS(exit_sts);
if (WIFSIGNALED(exit_sts))
{
sig = WTERMSIG(exit_sts);
if (sig == SIGINT)
ft_putstr_fd("^C\n", STDOUT_FILENO);
else if (sig == SIGQUIT)
ft_putstr_fd("^\\Quit (core dumped)\n", STDOUT_FILENO);
cmd->sts.process_status = sig + 128;
}
ms_term_set(cmd, 0);
}
}
void cmd_exec(t_cmd *cmd, t_plst *tmp)
{
t_fd fd;
fd.input = dup(STDIN_FILENO);
fd.output = dup(STDOUT_FILENO);
exec_fd_dup(tmp);
exec_exit_sts(cmd, tmp, 1, 0);
close(tmp->file_fd);
dup2(fd.input, STDIN_FILENO);
dup2(fd.output, STDOUT_FILENO);
}
int ms_exec(t_cmd *cmd)
{
if (cmd->sts.pipe_true == 0)
cmd_exec(cmd, cmd->pipe_lst);
else
cmd_pipe_exec(cmd, cmd->pipe_lst);
return (0);
}