forked from hazzus/os-kidshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShellHelper.cpp
45 lines (33 loc) · 1.13 KB
/
ShellHelper.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
//
// Created by Павел Пономарев on 2019-03-31.
//
#include "ShellHelper.h"
#include <unistd.h>
std::string ShellHelper::getCommand(std::vector<std::string> const& paths, std::string const& file) {
for (auto const& path: paths) {
std::string possiblePath = path;
possiblePath.append("/");
possiblePath.append(file);
if (!access(possiblePath.c_str(), 1)) {
return possiblePath;
}
}
return file;
}
std::vector<char*> ShellHelper::getCharVector(std::vector<std::string>& cmd) {
std::vector<char*> arguments;
arguments.reserve(cmd.size());
for (auto& str: cmd) {
arguments.push_back(&str.front());
}
arguments.push_back(nullptr);
return arguments;
}
std::vector<std::string> ShellHelper::getEnvironmentVector(std::map<std::string, std::string> const& env) {
std::vector<std::string> result;
std::transform(env.begin(), env.end(), std::back_inserter(result),
[](std::pair<std::string, std::string> const& p) {
return p.first + "=" + p.second;
});
return result;
}