-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.c
More file actions
127 lines (113 loc) · 2.11 KB
/
executor.c
File metadata and controls
127 lines (113 loc) · 2.11 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
125
126
127
#include "ft_microshell.h"
char **ft_set_args(t_tkn *head)
{
int nb_args = 0;
t_tkn *o_head = head;
while (head && head->type == WORD)
{
head = head->next;
nb_args++;
}
char **args = ft_calloc(sizeof(char *) * (nb_args + 1));
int i = 0;
head = o_head;
while (i < nb_args)
{
args[i] = ft_strdup(head->str);
head = head->next;
i++;
}
return (args);
}
t_tkn *last_before_cmd(t_tkn *head)
{
while (head && head->type == WORD)
head = head->next;
while (head && head->type == OP_STOP
&& head->next && head->next->type == OP_STOP)
head = head->next;
return (head);
}
void execute_cd(t_cmd *cmd)
{
int i = 0;
while (cmd->args[i])
i++;
if (i != 2)
ft_putstr_fd(2, "cd: wrong arguments\n");
else
{
int res = chdir(cmd->args[1]);
if (res == -1)
ft_putstr_fd(2, "cd: can't change dir\n");
}
}
void execute_cmd(t_cmd *cmd, char **envp)
{
if (!strncmp(cmd->path, "cd", 3))
{
execute_cd(cmd);
return;
}
if (cmd->path[0] != '/')
{
size_t path_len = ft_strlen(cmd->path);
char *new_path = ft_calloc(3 + path_len);
new_path[0] = '.';
new_path[1] = '/';
size_t i = 0;
while (i < path_len)
{
new_path[i + 2] = cmd->path[i];
i++;
}
free(cmd->path);
cmd->path = new_path;
}
pid_t new_pid = fork();
// error
if (new_pid == -1)
{
ft_putstr_fd(2, "can't fork... too bad\n");
exit(1);
}
// child
if (new_pid == 0)
{
int result = execve(cmd->path, cmd->args, envp);
if (result == -1)
{
ft_putstr_fd(2, "can't execute... too bad\n");
exit(1);
}
}
// parent
else
{
int stat_loc = 0;
waitpid(new_pid, &stat_loc, 0);
}
}
void executor(t_tkn *head, char **envp)
{
t_tkn *prev = NULL;
int stdin_copy = ft_dup(0);
int stdout_copy = ft_dup(1);
while (head)
{
t_cmd *cmd = ft_calloc(sizeof(t_cmd));
cmd->path = ft_strdup(head->str);
cmd->args = ft_set_args(head);
if (prev && prev->type == OP_PIPE)
cmd->redir_in = true;
prev = last_before_cmd(head);
if (prev && prev->type == OP_PIPE)
cmd->redir_out = true;
redir(cmd, stdin_copy, stdout_copy);
execute_cmd(cmd, envp);
if (prev)
head = prev->next;
else
head = NULL;
}
}