-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin.c
73 lines (67 loc) · 1.96 KB
/
builtin.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/03 10:49:26 by zhabri #+# #+# */
/* Updated: 2023/01/12 15:27:17 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin(char *cmd)
{
int i;
char **cmd_split;
static const char *builtins[8] = \
{"echo", "cd", "export", "env", "unset", "exit", "pwd", NULL};
i = 0;
cmd_split = ft_split_quotes(cmd, " \t");
while (cmd_split && cmd_split[0] && builtins[i])
{
if (!ft_strncmp(builtins[i], cmd_split[0], ft_strlen(builtins[i]) + 1))
{
free_tab(cmd_split);
return (i);
}
i++;
}
free_tab(cmd_split);
return (-1);
}
void init_builtin_tab(t_builtin *tab[7])
{
tab[0] = echo;
tab[1] = cd;
tab[2] = export;
tab[3] = env;
tab[4] = unset;
tab[5] = exit_child;
tab[6] = pwd;
}
void call_builtin(int built_in, t_cmd *cmd, int *pipes, int *children_pid)
{
int i;
int fd_out;
t_builtin *tab[7];
init_builtin_tab(tab);
if (cmd->fd_out != -3)
fd_out = cmd->fd_out;
else
{
if (cmd->cmd_idx % 2 == 0)
fd_out = pipes[3];
else
fd_out = pipes[1];
}
i = -1;
while (++i < 4)
if (pipes[i] != fd_out)
close(pipes[i]);
free(children_pid);
if (cmd->fd_in != -1 && cmd->fd_out != -1)
tab[built_in](cmd->str, fd_out);
clean_exit(NULL);
exit(1);
}