39 const std::vector<std::string> &command,
40 const std::filesystem::path &workingDirectory,
41 const std::vector<std::pair<std::string, std::string>> &environment = {})
46 return errors::FAILED_SPAWN_PROCESS;
50 for (
const auto &variable : environment)
52 _putenv_s(variable.first.c_str(), variable.second.c_str());
54 std::string commandLine =
"cmd /c";
55 for (
const auto &argument : command)
56 commandLine +=
" " + argument;
57 STARTUPINFOA startup{};
58 startup.cb =
sizeof(startup);
59 PROCESS_INFORMATION info{};
60 const std::string directory = workingDirectory.string();
61 if (!CreateProcessA(
nullptr, commandLine.data(),
nullptr,
nullptr, TRUE, 0,
nullptr,
62 directory.c_str(), &startup, &info))
65 return errors::FAILED_SPAWN_PROCESS;
67 CloseHandle(info.hThread);
68 CloseHandle(info.hProcess);
70 setenv(variable.first.c_str(), variable.second.c_str(), 1);
72 const pid_t child = fork();
76 return errors::FAILED_SPAWN_PROCESS;
80 if (chdir(workingDirectory.c_str()) != 0)
82 std::vector<char *> arguments;
83 for (
const auto &argument : command)
84 arguments.push_back(
const_cast<char *
>(argument.c_str()));
85 arguments.push_back(
nullptr);
86 execvp(arguments[0], arguments.data());