-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_executer.c
113 lines (104 loc) · 2.91 KB
/
my_executer.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* my_executer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jgarlic <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/02 22:52:17 by jgarlic #+# #+# */
/* Updated: 2021/12/02 22:52:18 by jgarlic ### ########.fr */
/* */
/* ************************************************************************** */
#include "my_minishell.h"
void my_exec_starter(t_command *cmd, t_shell *shell)
{
int i;
char *full_path;
char *temp;
if (ft_strchr(cmd->args[0], '/') != NULL)
execve(cmd->args[0], cmd->args, shell->env);
else if (my_check_builtin(cmd, shell, 1) != 0)
exit(0);
else if (shell->path != NULL)
{
temp = ft_strjoin("/", cmd->args[0]);
i = 0;
while (shell->path[i] != NULL)
{
full_path = ft_strjoin(shell->path[i], temp);
execve(full_path, cmd->args, shell->env);
free(full_path);
i++;
}
free(temp);
}
my_exec_error(cmd);
}
t_list *my_main_proc(t_executor *ex, t_list *list)
{
if (ex->fd_input_pipe != -1)
close(ex->fd_input_pipe);
ex->fd_input_pipe = -1;
if (ex->cmd->push_pipe == 1)
{
ex->fd_input_pipe = ex->fd[STDIN_FILENO];
close(ex->fd[STDOUT_FILENO]);
}
return (list->next);
}
void my_child_proc(t_executor *ex, t_shell *shell)
{
if (ex->cmd->push_pipe == 1)
{
dup2(ex->fd[STDOUT_FILENO], STDOUT_FILENO);
close(ex->fd[STDOUT_FILENO]);
close(ex->fd[STDIN_FILENO]);
}
if (ex->fd_input_pipe != -1)
{
dup2(ex->fd_input_pipe, STDIN_FILENO);
close(ex->fd_input_pipe);
}
if (ex->cmd->input_fd != -1)
{
dup2(ex->cmd->input_fd, STDIN_FILENO);
close(ex->cmd->input_fd);
}
if (ex->cmd->output_fd != -1)
{
dup2(ex->cmd->output_fd, STDOUT_FILENO);
close(ex->cmd->output_fd);
}
my_exec_starter(ex->cmd, shell);
}
void my_async_exec(t_list *list, t_shell *shell)
{
t_executor ex;
ex.fd_input_pipe = -1;
while (list != NULL)
{
ex.cmd = list->content;
if (ex.cmd->args[0] == NULL)
return ;
if (ex.cmd->push_pipe == 1)
pipe(ex.fd);
ex.pid = fork();
if (ex.pid != 0)
list = my_main_proc(&ex, list);
else
my_child_proc(&ex, shell);
}
my_global_static(1, 1);
while (wait(&shell->last_code) != -1)
(void)0;
my_global_static(1, 0);
}
void my_cmd_controller(char *cmd, t_shell *shell)
{
t_list *list;
list = my_cmd_parser(cmd, shell);
if (!(ft_lstsize(list) == 1
&& my_check_builtin(list->content, shell, 0) != 0))
my_async_exec(list, shell);
ft_lstclear(&list, my_free_cmd);
}