-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms_execution.c
94 lines (86 loc) · 2.43 KB
/
ms_execution.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_execution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlana <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/19 13:00:12 by obeedril #+# #+# */
/* Updated: 2022/03/14 16:18:02 by dlana ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int find_last_redir(t_data *data, int i)
{
int j;
j = 0;
while (data->cmd[i].redir[j] != 0)
{
if (data->cmd[i].redir[j] == 3 || data->cmd[i].redir[j] == 4)
data->cmd[i].last_redir = j;
j++;
}
return (data->cmd[i].last_redir);
}
static void ms_execution_2(t_data *data, int i, char **line)
{
int j;
j = 0;
data->cmd[i].last_redir = -1;
data->cmd[i].bad_file = NO;
if (data->cmd[i].count_redir != 0)
{
ms_open_file(&data->cmd[i], data);
ms_redirect(&data->cmd[i]);
find_last_redir(data, i);
}
if (data->num_cmd > 1)
ms_pipe(data, i);
if (data->cmd[i].bad_file == NO)
ms_our_cmd(data, i, line);
if (data->cmd[i].count_redir != 0)
{
while (j < data->cmd[i].count_redir)
{
if (data->cmd[i].redir[j] == 5)
unlink(data->cmd[i].file[j]);
j++;
}
}
}
void ms_return_stdio(int *stdio)
{
if (dup2(stdio[1], 1) == -1)
perror("dup2 ");
if (dup2(stdio[0], 0) == -1)
perror("dup2 ");
close(stdio[0]);
close(stdio[1]);
}
void ms_execution(t_data *data, char **line)
{
int i;
int stdio[2];
i = 0;
if (data->num_error != 0)
return ;
stdio[0] = dup(0);
stdio[1] = dup(1);
data->pid = 0;
ms_malloc_arr_int(&data->pid, data->num_cmd);
while (i < data->num_cmd)
{
if (ms_check_name(data, i) == -1)
return ;
ms_execution_2(data, i, line);
if (data->num_cmd > 1)
dup2(stdio[1], STDOUT_FILENO);
i++;
}
data->pid[i] = 0;
ms_return_stdio(stdio);
if (data->num_cmd > 1 || (data->build_in == NO && data->num_cmd == 1
&& !data->num_error))
ms_exe_signal(data);
ms_free_int_arr(&data->pid);
}