-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog.cpp
80 lines (78 loc) · 1.27 KB
/
prog.cpp
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
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
int main()
{
string str, command;
vector<string> commands;
getline(cin,str);
command = "";
for(int i = 0;;i++)
{
if(str[i] == '\0')
{
commands.push_back(command);
command = "";
break;
}
if(str[i] != '|')
command += str[i];
else
{
commands.push_back(command);
command = "";
i++;
}
}
int fd[2];
for(int i = 0; i < commands.size(); i++)
{
if(i == 0)
{
pipe(fd);
close(STDOUT_FILENO);
dup2(fd[1],STDOUT_FILENO);
if(!fork())
{
system(commands[i].c_str());
return 0;
}
else
continue;
}
else if(i+1 != commands.size())
{
close(STDIN_FILENO);
dup2(fd[0],STDIN_FILENO);
close(fd[0]);
close(fd[1]);
pipe(fd);
close(STDOUT_FILENO);
dup2(fd[1],STDOUT_FILENO);
if(!fork())
{
system(commands[i].c_str());
return 0;
}
else
continue;
}
else
{
close(STDIN_FILENO);
dup2(fd[0],STDIN_FILENO);
close(fd[0]);
close(fd[1]);
close(STDOUT_FILENO);
int out = open("/home/box/result.out", O_RDWR | O_CREAT, 0666);
dup2(out,STDOUT_FILENO);
system(commands[i].c_str());
}
}
}