forked from JonathanMeans/EvilC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform_macos.cpp
62 lines (58 loc) · 1.64 KB
/
platform_macos.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
#if defined(__APPLE__)
#include <unistd.h>
#include <vector>
#include "platform.h"
PlatformMacOS::PlatformMacOS() : PlatformBase("a.out")
{
}
void PlatformMacOS::runProgram(const std::string& programName,
const std::vector<std::string>& arguments) const
{
int fds[2]; // an array that will hold two file descriptors
pipe(fds); // populates fds with two file descriptors
pid_t child_process = fork();
if (child_process == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if (child_process > 0)
{
// parent process
std::cout << "spawn child with pid - " << child_process << std::endl;
close(fds[1]);
int status;
if (wait(&status) == -1)
{
perror("wait");
}
char buff[1000] = {};
ssize_t returnCode = read(fds[0], buff, 1000);
if (returnCode == -1)
{
perror("read");
}
std::cout << "buffer - " << buff << std::endl;
}
else
{
// child process
dup2(fds[1], STDERR_FILENO);
close(fds[0]); // file descriptor no longer needed in child since stdin
// is a copy
close(fds[1]); // file descriptor unused in child
int n = arguments.size();
// TODO: Free arg_list
char** arg_list = new char*[n + 1];
for (int i = 0; i < n; i++)
{
char* arg = strdup(arguments[i].c_str());
arg_list[i] = arg;
}
arg_list[n] = nullptr;
execvp(programName.c_str(), arg_list);
perror("execvp");
exit(EXIT_FAILURE);
}
}
#endif