Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Renamed exit_code to exit_status
Browse files Browse the repository at this point in the history
  • Loading branch information
eidheim committed Dec 6, 2015
1 parent 57379ee commit 7c8c905
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
28 changes: 14 additions & 14 deletions examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ int main() {
Process process1("echo Hello World", "", [](const char *bytes, size_t n) {
cout << "Output from stdout: " << std::string(bytes, n);
});
auto exit_code=process1.get_exit_code();
cout << "Example 1 process returned: " << exit_code << " (" << (exit_code==0?"success":"failure") << ")" << endl;
auto exit_status=process1.get_exit_status();
cout << "Example 1 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
std::this_thread::sleep_for(std::chrono::seconds(5));


Expand All @@ -22,16 +22,16 @@ int main() {
if(bytes[n-1]!='\n')
cout << endl;
});
exit_code=process2.get_exit_code();
cout << "Example 2 process returned: " << exit_code << " (" << (exit_code==0?"success":"failure") << ")" << endl;
exit_status=process2.get_exit_status();
cout << "Example 2 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
std::this_thread::sleep_for(std::chrono::seconds(5));


cout << endl << "Example 3 - async sleep process" << endl;
std::thread thread3([]() {
Process process3("sleep 5");
auto exit_code=process3.get_exit_code();
cout << "Example 3 process returned: " << exit_code << " (" << (exit_code==0?"success":"failure") << ")" << endl;
auto exit_status=process3.get_exit_status();
cout << "Example 3 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
});
thread3.detach();
std::this_thread::sleep_for(std::chrono::seconds(10));
Expand All @@ -40,8 +40,8 @@ int main() {
cout << endl << "Example 4 - killing async sleep process after 5 seconds" << endl;
auto process4=std::make_shared<Process>("sleep 10");
std::thread thread4([process4]() {
auto exit_code=process4->get_exit_code();
cout << "Example 4 process returned: " << exit_code << " (" << (exit_code==0?"success":"failure") << ")" << endl;
auto exit_status=process4->get_exit_status();
cout << "Example 4 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
});
thread4.detach();
std::this_thread::sleep_for(std::chrono::seconds(5));
Expand All @@ -61,8 +61,8 @@ int main() {
if(bytes[n-1]!='\n')
cout << endl;
});
exit_code=process5.get_exit_code();
cout << "Example 5 process returned: " << exit_code << " (" << (exit_code==0?"success":"failure") << ")" << endl;
exit_status=process5.get_exit_status();
cout << "Example 5 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
std::this_thread::sleep_for(std::chrono::seconds(5));


Expand All @@ -74,8 +74,8 @@ int main() {
}, nullptr, true);
process6.write("echo Hello from bash\n");
process6.write("exit\n");
exit_code=process6.get_exit_code();
cout << "Example 6 process returned: " << exit_code << " (" << (exit_code==0?"success":"failure") << ")" << endl;
exit_status=process6.get_exit_status();
cout << "Example 6 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
std::this_thread::sleep_for(std::chrono::seconds(5));


Expand All @@ -87,8 +87,8 @@ int main() {
}, nullptr, true);
process7.write("Hello cat\n");
process7.close_stdin();
exit_code=process7.get_exit_code();
cout << "Example 7 process returned: " << exit_code << " (" << (exit_code==0?"success":"failure") << ")" << endl;
exit_status=process7.get_exit_status();
cout << "Example 7 process returned: " << exit_status << " (" << (exit_status==0?"success":"failure") << ")" << endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
#endif
return 0;
Expand Down
4 changes: 2 additions & 2 deletions process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class Process {

///Get the process id of the started process.
id_type get_id() {return id;}
///Wait until process is finished, and return exit_code.
int get_exit_code();
///Wait until process is finished, and return exit status.
int get_exit_status();
///Write to stdin.
bool write(const char *bytes, size_t n);
///Write to stdin. Convenience function using write(const char *, size_t).
Expand Down
8 changes: 4 additions & 4 deletions process_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ void Process::async_read() {
}
}

int Process::get_exit_code() {
int Process::get_exit_status() {
if(id<=0)
return -1;
int exit_code;
waitpid(id, &exit_code, 0);
int exit_status;
waitpid(id, &exit_status, 0);

close_all();

return exit_code;
return exit_status;
}

void Process::close_all() {
Expand Down
10 changes: 5 additions & 5 deletions process_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,22 @@ void Process::async_read() {
}
}

int Process::get_exit_code() {
int Process::get_exit_status() {
if(id==0)
return -1;
DWORD exit_code;
DWORD exit_status;
HANDLE process_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, id);
if(process_handle) {
WaitForSingleObject(process_handle, INFINITE);
GetExitCodeProcess(process_handle, &exit_code);
GetExitCodeProcess(process_handle, &exit_status);
CloseHandle(process_handle);
}
else
exit_code=-1;
exit_status=-1;

close_all();

return static_cast<int>(exit_code);
return static_cast<int>(exit_status);
}

void Process::close_all() {
Expand Down
4 changes: 2 additions & 2 deletions test/open_close_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ int main() {
if(std::string(bytes, n)!="Hello World\n")
stdout_error=true;
});
auto exit_code=process.get_exit_code();
if(exit_code!=0) {
auto exit_status=process.get_exit_status();
if(exit_status!=0) {
cerr << "Process returned failure." << endl;
return 1;
}
Expand Down

0 comments on commit 7c8c905

Please sign in to comment.