-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipes.c
78 lines (76 loc) · 2.07 KB
/
pipes.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
#include "pipes.h"
#include "execute.h"
int separate(char str[], char *pipeCommands[32])
{
int i = 0;
char *token = strtok(str, "|");
if (token != NULL)
pipeCommands[i++] = token;
while (token != NULL)
{
token = strtok(NULL, "|");
if (token != NULL)
pipeCommands[i++] = token;
}
return i;
}
void execpipe(char cmd[])
{
strcpy(his[h++ % 20], cmd);
int numpipes = 0;
for (int i = 0; i < strlen(cmd); i++)
{
if (cmd[i] == '|')
numpipes++;
}
char *pipeCommands[32];
int pipe_commands = separate(cmd, pipeCommands);
if (pipe_commands != numpipes + 1)
{
fprintf(stderr, "Invalid Syntax\n");
return;
}
int fds[pipe_commands - 1][2];
for (int i = 0; i < pipe_commands - 1; i++)
{
if (pipe(fds[i]) < 0)
{
fprintf(stderr,"Error in piping command\n");
}
if (!i)
{
int out = dup(STDOUT_FILENO);
dup2(fds[i][1], STDOUT_FILENO);
close(fds[i][1]);
strcpy(temp_pipe,pipeCommands[i]);
args = removewhitespaces(pipeCommands[i]);
execute(temp_pipe);
dup2(out, STDOUT_FILENO);
close(out);
}
else
{
int in = dup(STDIN_FILENO);
int out = dup(STDOUT_FILENO);
dup2(fds[i][1], STDOUT_FILENO);
dup2(fds[i - 1][0], STDIN_FILENO);
close(fds[i][1]);
close(fds[i - 1][0]);
strcpy(temp_pipe,pipeCommands[i]);
args = removewhitespaces(pipeCommands[i]);
execute(temp_pipe);
dup2(in, STDIN_FILENO);
dup2(out, STDOUT_FILENO);
close(in);
close(out);
}
}
int in = dup(STDIN_FILENO);
dup2(fds[pipe_commands - 2][0], STDIN_FILENO);
close(fds[pipe_commands - 2][0]);
strcpy(temp_pipe,pipeCommands[pipe_commands - 1]);
args = removewhitespaces(pipeCommands[pipe_commands - 1]);
execute(temp_pipe);
dup2(in, STDIN_FILENO);
close(in);
}