-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForeJob.cpp
executable file
·186 lines (138 loc) · 5.06 KB
/
ForeJob.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* file: ForeJob.cpp
Foreground Job Class Implementation File
==================================================
Wimpy Shell Project - Com Sci 342
Shea Daniels
This class is based around the code needed to redirect,
execute, and wait for a foreground job.
*/
#include "ForeJob.h"
using namespace std;
/******************************************************
This is the basic constructor for the class.
PRE: new_command must a parsed Command object
POST: my_command is set.
*/
ForeJob::ForeJob(Command new_command) {
my_command = new_command;
}
/******************************************************
Tries to execute the job and then wait for it
to finish running.
POST: Returns true if the job was successfully started
and finished running. Returns false if there
was an error.
*/
bool ForeJob::execute() {
// linux system call
int pid = fork();
// error
if (pid < 0) {
cout << "Execution error: " << endl;
cout << " Could not create process." << endl;
return false;
}
// child process
if (pid == 0) {
// check for input redirection
if (my_command.isInputRedirected()) {
redirectInput();
}
// check for output redirection
if (my_command.isOutputRedirected()) {
redirectOutput();
}
// linux system call to replace process with another process
execvp(my_command.getCommandName().c_str(), my_command.getArgsArray());
// still here, must be an error
cout << "Execution error:" << endl;
if (errno == 2)
cout << " Command not found." << endl;
else
cout << " " << strerror(errno) << "." << endl;
exit(-1);
}
// still here, must be the parent
int pid_success = waitpid(pid, NULL, 0);
// check if something nasty happend
if (pid_success == -1) {
cout << "Execution error:" << endl;
cout << " " << strerror(errno) << "." << endl;
return false;
}
return true;
}
/******************************************************
Tries to redirect the output of the job to the file
specified in the my_command object.
PRE: my_command has been parsed.
POST: Returns after a successful redirect attempt.
Otherwise, an error message is printed and
the process terminates.
*/
void ForeJob::redirectOutput() {
// convert filename string into char array
char *file_ptr = NULL;
file_ptr = new char [my_command.getOutputFileName().size() + 1];
strcpy(file_ptr, my_command.getOutputFileName().c_str());
// linux system call get file descriptor
int file_descriptor = open(file_ptr, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
// check if something nasty happened
if (file_descriptor == -1) {
cout << "Output file error:" << endl;
cout << " " << strerror(errno) << "." << endl;
exit(-1);
}
// linux system call to redirect output
int redirect_success = dup2(file_descriptor, 1);
// check if something nasty happened
if (redirect_success == -1) {
cout << "Output file redirect error:" << endl;
cout << " " << strerror(errno) << "." << endl;
exit(-1);
}
close(file_descriptor);
// check if something nasty happened
if (redirect_success == -1) {
cout << "Output file error:" << endl;
cout << " " << strerror(errno) << "." << endl;
exit(-1);
}
}
/******************************************************
Tries to redirect the input of the job to the file
specified in the my_command object.
PRE: my_command has been parsed.
POST: Returns after a successful redirect attempt.
Otherwise, an error message is printed and
the process terminates.
*/
void ForeJob::redirectInput() {
// convert filename string into char array
char *file_ptr = NULL;
file_ptr = new char [my_command.getInputFileName().size() + 1];
strcpy(file_ptr, my_command.getInputFileName().c_str());
// linux system call get file descriptor
int file_descriptor = open(file_ptr, O_RDONLY);
// check if something nasty happened
if (file_descriptor == -1) {
cout << "Input file error:" << endl;
cout << " " << strerror(errno) << "." << endl;
exit(-1);
}
// linux system call to redirect input
int redirect_success = dup2(file_descriptor, 0);
// check if something nasty happened
if (redirect_success == -1) {
cout << "Input file redirect error:" << endl;
cout << " " << strerror(errno) << "." << endl;
exit(-1);
}
close(file_descriptor);
// check if something nasty happened
if (redirect_success == -1) {
cout << "Input file error:" << endl;
cout << " " << strerror(errno) << "." << endl;
exit(-1);
}
}