-
Notifications
You must be signed in to change notification settings - Fork 0
/
minishell_outils.c
95 lines (87 loc) · 2.51 KB
/
minishell_outils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell_outils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amaach <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/20 18:54:22 by eelaazmi #+# #+# */
/* Updated: 2021/12/22 15:57:12 by amaach ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void free_sashell(t_sashell *sashell)
{
t_sashell *tmp;
int i;
i = 0;
while (sashell)
{
tmp = sashell->next;
ft_free(sashell->tokens, 1024);
ft_free(sashell->red, 1024);
free(sashell);
sashell = tmp;
}
}
int ft_token_count(t_token *token, t_sashell *sashell)
{
while (sashell)
{
token->token_count++;
sashell = sashell->next;
}
return (token->token_count);
}
int exec_cmd(t_sashell *sashell, char **cmd, t_env *env, int i)
{
t_redir redir;
redir.index_in = 0;
redir.index_out = 0;
if (sashell->red)
if (exec_redirection(sashell, &redir))
return (1);
if (cmd && is_builtin(cmd[0]) && cmd[0][0])
{
if (exec_builtin(cmd, env) < 0)
return (1);
}
else if (cmd && i > 1 && cmd[0][0])
exec_others(cmd, env, g_.envp);
else if (cmd && i == 1 && cmd[0][0])
g_.exit_value = execo_others(cmd, env, g_.envp);
else
{
ft_putstr("minishell: command not found: \n");
g_.exit_value = 127;
}
if (redir.index_in || redir.index_out)
reset_redirection(&redir.in, &redir.out, &redir.fd);
return (g_.exit_value);
}
void minishell(t_sashell *sashell, t_env *env)
{
char **cmd;
int index;
t_token token;
index = 0;
token.token_count = 0;
cmd = sashell->tokens;
ft_token_count(&token, sashell);
if (token.token_count > 1)
g_.exit_value = exec_pipe(env, sashell, token.token_count);
else if (cmd[0])
exec_cmd(sashell, cmd, env, 1);
else if (sashell->red[0])
{
if (sashell->red[0][4] == 't' && sashell->red[0][1 == '<'])
{
cmd[0] = ft_strdup("cat");
cmd[1] = NULL;
sashell->tokens[0] = cmd[0];
exec_cmd(sashell, cmd, env, 1);
unlink(sashell->red[0] + 3);
g_.exit_value = 0;
}
}
}