-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththe_executer.c
More file actions
124 lines (117 loc) · 3.5 KB
/
the_executer.c
File metadata and controls
124 lines (117 loc) · 3.5 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
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
114
115
116
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* the_executer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aouchaad <aouchaad@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/22 19:32:14 by aouchaad #+# #+# */
/* Updated: 2023/06/23 15:12:18 by aouchaad ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void init_var(t_variables *var, t_command_line **tmp2, \
t_redirection **tmp1, int **p)
{
(*p) = NULL;
var->i = 0;
var->fd_out = 1;
var->comm_count = 0;
var->error = 0;
(*tmp1) = (*tmp2)->redirection;
while ((*tmp2))
{
var->comm_count++;
(*tmp2) = (*tmp2)->next;
}
if (var->comm_count > 1)
(*p) = malloc (sizeof(int) * (var->comm_count - 1) * 2);
}
void pipe_creation(t_variables *var, int **p)
{
while (var->i < var->comm_count - 1)
{
if (pipe((*p) + (var->i * 2)) == -1)
{
perror("pipe: ");
exit (1);
}
var->i++;
}
}
int check_redirection(t_redirection **tmp1, t_variables *var, \
int **p, t_glob **glob)
{
if ((*tmp1)->type == read_fromf)
{
var->error = check_exist_and_permission((*tmp1)->file, (*tmp1)->type);
if (var->error == -1)
{
free((*p));
(*p) = NULL;
(*glob)->exit_stat = 1;
return (1);
}
}
else if ((*tmp1)->type == write_inf || (*tmp1)->type == append)
{
var->fd_out = check_exist_and_permission((*tmp1)->file, (*tmp1)->type);
if (var->fd_out == -1)
{
free((*p));
(*p) = NULL;
(*glob)->exit_stat = 1;
return (1);
}
}
(*tmp1) = (*tmp1)->next;
return (0);
}
void builtins(t_variables *var, t_glob **glob, int *p)
{
if (var->fd_out != 1)
close(var->fd_out);
if (ft_strcmp((*glob)->command_line->command, "echo") == 0)
(*glob)->exit_stat = ft_echo((*glob)->command_line->args);
else if (ft_strcmp((*glob)->command_line->command, "env") == 0)
(*glob)->exit_stat = ft_env((*glob)->envp);
else if (ft_strcmp((*glob)->command_line->command, "pwd") == 0)
(*glob)->exit_stat = ft_pwd((*glob)->envp);
else if (ft_strcmp((*glob)->command_line->command, "export") == 0)
(*glob)->exit_stat = ft_export((*glob)->command_line->args, glob);
else if (ft_strcmp((*glob)->command_line->command, "exit") == 0)
(*glob)->exit_stat = ft_exit((*glob)->command_line->args, glob);
else if (ft_strcmp((*glob)->command_line->command, "unset") == 0)
(*glob)->exit_stat = ft_unset(glob, (*glob)->command_line->args);
else if (ft_strcmp((*glob)->command_line->command, "cd") == 0)
(*glob)->exit_stat = ft_cd(glob);
else
non_builtins(glob, var->comm_count, p);
}
void the_executer(t_glob **glob)
{
int *p;
t_command_line *tmp2;
t_redirection *tmp1;
t_variables var;
tmp2 = (*glob)->command_line;
init_var(&var, &tmp2, &tmp1, &p);
if (p != NULL)
pipe_creation(&var, &p);
if (var.comm_count == 1)
{
while (tmp1)
{
if (check_redirection(&tmp1, &var, &p, glob) == 1)
return ;
}
var.g = dup(1);
dup2(var.fd_out, 1);
builtins(&var, glob, p);
dup2(var.g, 1);
}
else
non_builtins(glob, var.comm_count, p);
free(p);
p = NULL;
}