-
Notifications
You must be signed in to change notification settings - Fork 0
/
meu_exec.c
79 lines (76 loc) · 1.76 KB
/
meu_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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "meu_exec.h"
void executaComando(char **args, int tam)
{
int pipefd[2] = {-1, -1};
int rc = fork();
if (rc < 0)
{
printf("Erro!\n");
return;
}
if (rc == 0)
{
if (tam > 1)
{
pipe(pipefd);
int rc2 = fork();
if (rc2 < 0)
{
printf("Erro!\n");
return;
}
else if (rc2 == 0)
{
char **argsFilho = args;
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[1]);
//printf("PASSEI POR AQUI\n");
int status2 = execvp(argsFilho[0], argsFilho);
printf("\n");
if (status2 < 0)
{
printf("Erro!\n");
exit(1);
}
}
else
{
dup2(pipefd[1], 1);
int status = execvp(args[0], args);
printf("\n");
if (status < 0)
{
printf("Erro!\n");
exit(1);
}
wait(0);
}
}
else
{
if (args == NULL)
exit(1);
//int status =
execvp(args[0], args);
printf("\n");
// if (status < 0)
// {
// printf("Erro ao executar programa\n");
// exit(1);
// }
}
}
else
{
if (strcmp(args[1], "&") == 0)
printf("Processo filho: [%d]\n", rc);
else
wait(NULL);
}
}