-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.c
139 lines (130 loc) · 3.25 KB
/
executor.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "functions.h"
#include "42-libft/libft.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
static int opens(t_command *cmd, size_t *i)
{
int fd;
if (!ft_strncmp(cmd->rdrs[*i], ">>", 2))
{
fd = open(cmd->rdrs[++(*i)], O_CREAT | O_APPEND | O_WRONLY, 0777);
if (fd == -1)
return (perror("open"), -1);
cmd->fd[1] = fd;
}
else if (!ft_strncmp(cmd->rdrs[*i], "<", 1))
{
fd = open(cmd->rdrs[++(*i)], O_RDONLY, 0777);
if (fd == -1)
return (perror("open"), -1);
cmd->fd[0] = fd;
}
else if (!ft_strncmp(cmd->rdrs[*i], ">", 1))
{
fd = open(cmd->rdrs[++(*i)], O_CREAT | O_TRUNC | O_WRONLY, 0777);
if (fd == -1)
return (perror("open"), -1);
cmd->fd[1] = fd;
}
return (0);
}
static int redirection_touch(t_command *cmd)
{
size_t i;
i = 0;
while (cmd->rdrs[i])
{
if (opens(cmd, &i) == -1)
return (0);
i++;
}
return (1);
}
int set_fd(t_command *cmd)
{
int fd[2];
while (cmd)
{
if (cmd->rdrs)
{
if (!redirection_touch(cmd))
return (0);
}
if (cmd->where_p == R_P || cmd->where_p == B_P)
{
if (pipe(fd) == -1)
return (perror("pipe"), 0);
cmd->fd[1] = fd[1];
if (cmd->next)
cmd->next->fd[0] = fd[0];
}
cmd = cmd->next;
}
return (1);
}
static void official_executer(t_command *cmds, t_main *shell, int i)
{
cmds->pid = fork();
if (cmds->pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if (cmds->pid == 0)
{
if (i == 0 && cmds->fd[1] != STDOUT_FILENO)
{
dup2(cmds->fd[1], STDOUT_FILENO);
close(cmds->fd[1]);
}
else if (i > 0)
{
close(cmds->prev->fd[1]);
dup2(cmds->fd[0], STDIN_FILENO);
if (cmds->fd[1] != STDOUT_FILENO)
dup2(cmds->fd[1], STDOUT_FILENO);
}
execve(cmds->cmd_and_path, cmds->value, shell->env_for_execve_function);
perror("execve");
exit(EXIT_FAILURE);
}
}
int executor(t_main *shell)
{
t_command *cmds;
int i = 0;
cmds = shell->cmd;
if (shell->control == 0)
return 1;
if (!set_fd(cmds))
return 0;
shell->paths = get_cmd(shell->envs);
if (!shell->paths)
return 0; // close resources if needed
while (cmds)
{
if (is_builtin(cmds, shell))
;
else if (accessibility(cmds, shell))
official_executer(cmds, shell, i);
else
{
return (ft_putstr_fd("ft_sh: command not found: ", 2),
ft_putstr_fd(cmds->value[0], 2),
ft_putchar_fd('\n', 2),
free_double(shell->paths), 1);
}
if (cmds->fd[1] != STDOUT_FILENO)
close(cmds->fd[1]);
if (cmds->prev && cmds->prev->fd[0] != STDIN_FILENO)
close(cmds->prev->fd[0]);
i++;
cmds = cmds->next;
}
wait(NULL);
return (free_double(shell->paths), free_command(shell->cmd), 1);
}