-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.cpp
More file actions
87 lines (74 loc) · 2.53 KB
/
env.cpp
File metadata and controls
87 lines (74 loc) · 2.53 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <unistd.h>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/error.hpp>
extern char** environ;
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "env",
.version = CFBOX_VERSION_STRING,
.one_line = "run a program in a modified environment",
.usage = "env [-i] [NAME=VALUE]... [COMMAND [ARGS]...]",
.options = " -i start with an empty environment",
.extra = "",
};
} // namespace
auto env_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'i', false},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
bool clear_env = parsed.has('i');
// Collect NAME=VALUE assignments from positional args until we hit a non-assignment
std::vector<std::pair<std::string, std::string>> assignments;
const auto& pos = parsed.positional();
std::size_t cmd_start = pos.size();
for (std::size_t i = 0; i < pos.size(); ++i) {
auto arg = std::string{pos[i]};
auto eq = arg.find('=');
if (eq == std::string::npos || eq == 0) {
cmd_start = i;
break;
}
assignments.emplace_back(arg.substr(0, eq), arg.substr(eq + 1));
}
if (clear_env) {
// Clear environment, then set assigned vars
for (char** env = environ; *env != nullptr; ++env) {
auto eq = std::string_view{*env}.find('=');
if (eq != std::string_view::npos) {
std::string name{*env, eq};
unsetenv(name.c_str());
}
}
}
for (const auto& [name, value] : assignments) {
setenv(name.c_str(), value.c_str(), 1);
}
// No command: print environment
if (cmd_start >= pos.size()) {
for (char** env = environ; *env != nullptr; ++env) {
std::puts(*env);
}
return 0;
}
// Execute command
std::string cmd{pos[cmd_start]};
std::vector<std::string> arg_storage;
for (std::size_t i = cmd_start; i < pos.size(); ++i) {
arg_storage.emplace_back(pos[i]);
}
std::vector<char*> cmd_args;
for (auto& s : arg_storage) {
cmd_args.push_back(s.data());
}
cmd_args.push_back(nullptr);
execvp(cmd.c_str(), cmd_args.data());
CFBOX_ERR("env", "failed to execute '%s': %s", cmd.c_str(), std::strerror(errno));
return 127;
}