forked from maxLundin/os-find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
programconditions.cpp
46 lines (39 loc) · 1.12 KB
/
programconditions.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
#include <string.h>
#include "programconditions.h"
ProgramConditions::ProgramConditions(const std::vector<std::string>& args,
const std::map<std::string, std::string>& env)
{
m_argv = new char*[args.size() + 1];
m_envp = new char*[env.size() + 1];
m_argv[args.size()] = nullptr;
m_envp[env.size()] = nullptr;
for (size_t i = 0; i < args.size(); ++i) {
m_argv[i] = new char[args[i].length() + 1];
strcpy(m_argv[i], args[i].c_str());
}
auto it = env.begin();
for (size_t i = 0; i < env.size(); ++i, ++it) {
std::string assignment = it->first + "=" + it->second;
m_envp[i] = new char[assignment.length() + 1];
strcpy(m_envp[i], assignment.c_str());
}
}
ProgramConditions::~ProgramConditions()
{
for (auto argv = m_argv; *argv != nullptr; ++argv) {
delete[] *argv;
}
for (auto envp = m_envp; *envp != nullptr; ++envp) {
delete[] *envp;
}
delete[] m_argv;
delete[] m_envp;
}
char** ProgramConditions::get_argv()
{
return m_argv;
}
char** ProgramConditions::get_envp()
{
return m_envp;
}