forked from maxLundin/os-find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.cpp
75 lines (64 loc) · 1.63 KB
/
environment.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
#include <unistd.h>
#include <sstream>
#include "environment.h"
Environment::Environment(char *envp[])
{
for (; *envp != nullptr; ++envp) {
set_variable(std::string(*envp));
}
if (m_environ.find("PATH") == m_environ.end()) {
// Assuming default value for LSB
set_variable("PATH=/bin:/sbin:/usr/bin:/usr/sbin");
}
}
void Environment::update_paths(const std::string& path_value)
{
m_paths.clear();
std::stringstream ss(path_value);
std::string segment;
while (std::getline(ss, segment, ':'))
{
m_paths.push_back(segment);
}
}
void Environment::set_variable(const std::string& assignment)
{
size_t eq = assignment.find('=');
std::string key, value;
if (eq != std::string::npos) {
key = assignment.substr(0, eq);
value = assignment.substr(eq + 1);
} else {
key = assignment;
value = "";
}
m_environ[key] = value;
if (key == "PATH") {
update_paths(value);
}
}
void Environment::unset_variable(const std::string& var)
{
m_environ.erase(var);
}
const std::map<std::string, std::string>& Environment::get_variables()
{
return m_environ;
}
std::string Environment::find_executable(const std::string& filename) const
{
if (executable_exists(filename)) {
return filename;
}
for (const auto& prefix : m_paths) {
std::string attempt = prefix + "/" + filename;
if (executable_exists(attempt)) {
return attempt;
}
}
return "";
}
bool Environment::executable_exists(const std::string &filename) const
{
return access(filename.c_str(), X_OK) != -1;
}