-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pipe.h
87 lines (78 loc) · 1.33 KB
/
Pipe.h
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
#include <bits/stdc++.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<iostream>
#include<cstdio>
#include<fcntl.h>
#include<sys/stat.h>
#include<pwd.h>
//#include"checkCommand.cpp"
#include"Redirect.h"
extern int status;
using namespace std;
vector<string> parsePipe(string x)
{
vector<string> tokens;
stringstream s(x);
string temp;
int i=0;
while(getline(s,temp,'|'))
{
tokens.push_back(temp);
}
return tokens;
}
//--------------------------------------
void evaluatePipe(string ip)
{
vector<string> tokens = parsePipe(ip);
int RD = 0;
int fd[2];
for(auto i=tokens.begin();i!=tokens.end();i++)
{
char **temp2 = tokenize(*i);
temp2 = checkinMap(temp2);
pipe(fd);
int pid = fork();
if(pid == 0) //child process
{
if(i == tokens.begin())
{
dup2(fd[1],1);
close(fd[0]);
close(fd[1]);
execvp(temp2[0],temp2);
}
else if(i == tokens.end()-1)
{
string p = checkTypeOfCommand(*i);
if(p == "redirect")
{
evaluateRedirect(*i,1,&RD,fd);
}
else
{
dup2(RD,0);
close(fd[0]);
close(fd[1]);
execvp(temp2[0],temp2);
}
}
else
{
dup2(RD,0);
dup2(fd[1],1);
close(fd[0]);
close(fd[1]);
execvp(temp2[0],temp2);
}
}
else
{
wait(&status);
close(fd[1]);
RD = fd[0];
}
}
}