-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile_ExecutePiped.c
More file actions
executable file
·103 lines (81 loc) · 1.79 KB
/
File_ExecutePiped.c
File metadata and controls
executable file
·103 lines (81 loc) · 1.79 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
#include"file.h"
void ExecutePiped(char** parsedArgs,int N)
{
if(N==2)
{
int p1, p2;
int fd[2];
int Executed[2];
char *CommandOne[LineLength] ;
char *CommandTwo[LineLength];
ParseSimple(strdup(parsedArgs[0]),CommandOne," ");
ParseSimple(strdup(parsedArgs[1]),CommandTwo," ");
if (pipe(fd) < 0)
{
printf("\nPipe could not be initialized\n");
}
if (pipe(Executed) < 0)
{
printf("\nPipe could not be initialized\n");
}
p1 = fork();
if (p1 < 0)
{
printf("\nCould not fork\n");
}
if (p1 == 0)
{
dup2(fd[1],STDOUT_FILENO);
close(fd[1]);
close(fd[0]);
int CommandState;
close(Executed[0]);
if (execvp(CommandOne[0], CommandOne) < 0)
{
printf("\nCould not execute command ..\n");
CommandState = 0;
write(Executed[1],&CommandState,sizeof(CommandState));
close(Executed[1]);
exit(1);
}
}
int ReceivedState = 1;
close(Executed[1]);
if (read(Executed[0],&ReceivedState,sizeof(ReceivedState))==-1)
{
ReceivedState=1;
}
close(Executed[0]);
if(ReceivedState==1)
{
p2=fork();
if (p2 < 0)
{
printf("\nCould not fork\n");
}
if (p2 == 0)
{
dup2(fd[0],STDIN_FILENO);
close(fd[1]);
close(fd[0]);
if (execvp(CommandTwo[0], CommandTwo) < 0)
{
printf("\nCould not execute piping command ..\n");
exit(1);
}
}
}
else
{
printf("pipe cannot be done\n");
}
close(fd[1]);
close(fd[0]);
waitpid(p1,NULL,0);
waitpid(p2,NULL,0);
}
else
{
printf("An inadequate number of arguments in the command prompt\n");
}
}