-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.cpp
145 lines (125 loc) · 3.5 KB
/
bundle.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "bundle.h"
Bundle::Bundle(){
this->name = "";
this->inFile = "";
this->outFile = "";
}
Bundle::~Bundle(){
this->name = "";
this->inFile = "";
this->outFile = "";
this->commands.clear();
}
Bundle::Bundle(std::string name, std::vector<char**> commands){
this->name = "";
this->inFile = "";
this->outFile = "";
set_commands(commands);
}
Bundle::Bundle(std::string name, std::string inFile, std::string outFile){
this->name = name;
this->inFile = inFile;
this->outFile = outFile;
}
Bundle::Bundle(std::string name, std::string inFile, std::string outFile, std::vector<char**> commands){
this->name = name;
this->inFile = inFile;
this->outFile = outFile;
set_commands(commands);
}
void Bundle::set_name(std::string name){
this->name = name;
}
std::string Bundle::get_name(){
return this->name;
}
void Bundle::set_inFile(std::string inFile){
this->inFile = inFile;
}
std::string Bundle::get_inFile(){
return this->inFile;
}
void Bundle::set_outFile(std::string outFile){
this->outFile = outFile;
}
std::string Bundle::get_outFile(){
return this->outFile;
}
void Bundle::set_commands(std::vector<char**> commands){
int length = commands.size();
for(int i = 0; i < length; i++){
this->commands.push_back(commands[i]);
}
}
void Bundle::add_command(char** cmd){
this->commands.push_back(cmd);
}
std::vector<char**> Bundle::get_commands(){
return this->commands;
}
Bundle& Bundle::operator=(const Bundle& other){
if(this != &other){
this->inFile = other.inFile;
this->outFile = other.outFile;
this->commands = other.commands;
}
return *this;
}
void Bundle::print(){
int n = this->commands.size();
std::cout << "Bundle Name: \"" << this->name << "\""<<std::endl;
for(int i = 0; i < n; i++){
std::cout << "Command " << i+1 << " : ";
for(int j = 0; this->commands[i][j] != NULL; j++){
std::cout << "\""<< this->commands[i][j]<< "\" ";
}
std::cout<<std::endl;
}
if(inFile != ""){
std::cout << "Infile: \"" << this->inFile << "\"" << std::endl;
}
if(outFile != ""){
std::cout << "Outfile: \"" << this->outFile << "\"" << std::endl;
}
}
int Bundle::execute(){
std::vector<char**> cmd = this->get_commands();
std::vector<pid_t> pids;
int parent = 1;
for(int i = 0; i < cmd.size(); i++){
char **command = cmd[i];
pid_t pid = fork();
//Child process
if(pid == 0){
parent=0;
if(inFile != ""){
int infile_desc = open(this->inFile.c_str(), O_RDONLY, 00700);
dup2(infile_desc, 0);
close(infile_desc);
}
if(outFile!=""){
int outfile_desc = open(this->outFile.c_str(), O_CREAT | O_APPEND | O_WRONLY, 00700);
dup2(outfile_desc, STDOUT_FILENO);
close(outfile_desc);
}
int status = execvp(command[0], command);
if(status == -1){
std::cout << "Error: " << strerror(errno) << std::endl;
exit(1);
}
exit(0);
}
else{
pids.push_back(pid);
}
}
//Wait for child processes to finish
if(parent){
for(int i = 0; i < pids.size(); i++){
int status;
//std::cout<< "Waiting process " << i << std::endl;
waitpid(pids[i], &status, 0);
}
}
return 0;
}