-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexec.c
190 lines (166 loc) · 3.28 KB
/
exec.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "exec.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //chamadas de sistema
#include <errno.h> //para tratar os erros
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
void ExecCommandLine(commandline *cmd)
{
int pid;
int fd[2];
int fd2[2];
int status = -1;
commandline *node = cmd;
CheckExit(cmd->current);
if (cmd->next == NULL) //single command
{
pid = fork();
if (pid == -1)
{
printf("Erro ao criar um novo processo!\nSaindo...");
exit(2);
}
else if (pid == 0)
{
ExecCommand(cmd->current);
}
return;
}
do
{
CheckExit(node->current);
if (node->nextOperator == '>' || node->nextOperator == '<')
{
node = ExecArchiveCommand(node); //executa o comando de arquivo e retorna o no atual
}
else
{
if (node->current->id % 2 != 0)
pipe(fd);
else
pipe(fd2);
switch (fork())
{
case -1:
printf("Falha ao criar novo processo!\nSaindo...");
exit(EXIT_FAILURE);
case 0:
//caso seja o primeiro comando
if (node->current->id == 0)
{
dup2(fd2[1], 1);
}
else if (node->current->id == node->qntCommands) //caso seja o último comando
{
if (node->qntCommands % 2 != 0)
dup2(fd[0], STDIN_FILENO);
else
dup2(fd2[0], STDIN_FILENO);
}
else //caso seja os demais comandos
{
if (node->current->id % 2 != 0)
{
dup2(fd2[0], STDIN_FILENO);
dup2(fd[1], STDOUT_FILENO);
}
else
{
dup2(fd[0], STDIN_FILENO);
dup2(fd2[1], STDOUT_FILENO);
}
}
ExecCommand(node->current);
exit(0);
default:
if (node->current->id == 0) //caso seja o primeiro comando
{
close(fd2[1]);
}
else if (node->current->id == node->qntCommands) //caso seja o último comando
{
if (node->qntCommands % 2 != 0)
{
close(fd[0]);
}
else
{
close(fd2[0]);
}
}
else
{
if (node->current->id % 2 != 0)
{
close(fd2[0]);
close(fd[1]);
}
else
{
close(fd[0]);
close(fd2[1]);
}
}
waitpid(pid, NULL, 0);
}
}
if (node->next == NULL)
break;
node = node->next;
} while (node != NULL);
setbuf(stdin, NULL);
}
void ExecCommand(command *cmd)
{
if (-1 == execvp(cmd->args[0], cmd->args))
{
perror("Error: ");
printf("\n");
exit(2);
}
}
commandline *ExecArchiveCommand(commandline *cmd)
{
int fdArchive;
int pid = fork();
if (pid == -1)
{
printf("Erro ao criar um processo!\nSaindo...");
exit(2);
}
else if (pid == 0)
{
if (cmd->nextOperator == '>')
{
fdArchive = open(cmd->next->current->args[0], O_CREAT | O_TRUNC | O_WRONLY, 0600);
dup2(fdArchive, STDOUT_FILENO);
}
else if (cmd->nextOperator == '<')
{
fdArchive = open(cmd->next->current->args[0], O_RDONLY, 0600);
dup2(fdArchive, STDIN_FILENO);
close(fdArchive);
fdArchive = open(cmd->next->current->args[0], O_CREAT | O_TRUNC | O_WRONLY, 0600);
dup2(fdArchive, STDOUT_FILENO);
}
if (execvp(cmd->current->args[0], cmd->current->args) == -1)
{
perror("Error: ");
printf("\n");
exit(2);
}
}
wait(NULL);
close(fdArchive);
return cmd->next;
}
void CheckExit(command *current)
{
if (strncmp(current->args[0], "exit", 4) == 0)
{
printf("Ty, feel free to come back!\n");
exit(0);
}
}