diff --git a/include/hyprutils/os/Process.hpp b/include/hyprutils/os/Process.hpp index 71cc743..115fa66 100644 --- a/include/hyprutils/os/Process.hpp +++ b/include/hyprutils/os/Process.hpp @@ -2,6 +2,7 @@ #include #include +#include namespace Hyprutils { namespace OS { @@ -10,6 +11,8 @@ namespace Hyprutils { /* Creates a process object, doesn't run yet */ CProcess(const std::string& binary_, const std::vector& args_); + void addEnv(const std::string& name, const std::string& value); + /* Run the process, synchronously, get the stdout and stderr. False on fail */ bool runSync(); @@ -21,8 +24,9 @@ namespace Hyprutils { const std::string& stdErr(); private: - std::string binary, out, err; - std::vector args; + std::string binary, out, err; + std::vector args; + std::vector> env; }; } } \ No newline at end of file diff --git a/src/os/Process.cpp b/src/os/Process.cpp index e415129..2aab580 100644 --- a/src/os/Process.cpp +++ b/src/os/Process.cpp @@ -16,6 +16,10 @@ Hyprutils::OS::CProcess::CProcess(const std::string& binary_, const std::vector< ; } +void Hyprutils::OS::CProcess::addEnv(const std::string& name, const std::string& value) { + env.emplace_back(std::make_pair<>(name, value)); +} + bool Hyprutils::OS::CProcess::runSync() { int outPipe[2], errPipe[2]; if (pipe(outPipe)) @@ -53,6 +57,11 @@ bool Hyprutils::OS::CProcess::runSync() { argsC.emplace_back(nullptr); + // pass env + for (auto& [n, v] : env) { + setenv(n.c_str(), v.c_str(), 1); + } + execvp(binary.c_str(), (char* const*)argsC.data()); exit(1); } else { diff --git a/tests/os.cpp b/tests/os.cpp index 7a932ce..1e6159b 100644 --- a/tests/os.cpp +++ b/tests/os.cpp @@ -6,7 +6,8 @@ using namespace Hyprutils::OS; int main(int argc, char** argv, char** envp) { int ret = 0; - CProcess process("echo", {"Hello World!"}); + CProcess process("sh", {"-c", "echo \"Hello $WORLD!\""}); + process.addEnv("WORLD", "World"); EXPECT(process.runAsync(), true); EXPECT(process.runSync(), true);