-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.c
76 lines (71 loc) · 2.29 KB
/
process.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dkham <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/05 19:47:13 by dkham #+# #+# */
/* Updated: 2023/06/09 17:11:22 by dkham ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void child_process(t_shell *my_shell, t_pipes *head, char **env, int i)
{
set_signal_child(my_shell);
handle_input_redirection(my_shell, i);
handle_output_redirection(my_shell);
if (is_builtin(head->simple_cmd->word[0]))
{
builtin(my_shell, head, i);
exit(0);
}
else
handle_external_command(my_shell, head, env);
exit(1);
}
void handle_input_redirection(t_shell *my_shell, int i)
{
if (my_shell->fd_in != 0)
{
if (dup2(my_shell->fd_in, 0) == -1)
exit(1);
close(my_shell->fd_in);
}
else if (my_shell->pipe_fd[0] != 0 && i != 1)
{
if (dup2(my_shell->prev_pipe_fd_0, 0) == -1)
exit(1);
close(my_shell->prev_pipe_fd_0);
}
}
void handle_output_redirection(t_shell *my_shell)
{
if (my_shell->fd_out != 1)
{
if (dup2(my_shell->fd_out, 1) == -1)
exit(1);
close(my_shell->fd_out);
}
else if (my_shell->pipe_fd[1] != 1 && my_shell->last_cmd_flag != 1)
{
if (dup2(my_shell->pipe_fd[1], 1) == -1)
exit(1);
close(my_shell->pipe_fd[1]);
}
if (my_shell->pipe_fd[0] != 0)
close(my_shell->pipe_fd[0]);
}
void parent_process(t_shell *my_shell, int i)
{
set_parent_signal(my_shell);
if (my_shell->fd_in != 0)
close(my_shell->fd_in);
if (my_shell->fd_out != 1)
close(my_shell->fd_out);
if (my_shell->pipe_fd[1] != 1)
close(my_shell->pipe_fd[1]);
if (i != 1 && my_shell->prev_pipe_fd_0 != -1)
close(my_shell->prev_pipe_fd_0);
my_shell->prev_pipe_fd_0 = my_shell->pipe_fd[0];
}