23 std::int64_t
run_process(
const std::string& program,
const std::vector<std::string>& args,
bool discardOutput )
28 std::vector<char*> argv;
29 argv.reserve(args.size() + 2);
30 argv.push_back(
const_cast<char*
>(program.c_str()));
31 for (
const auto& arg : args)
33 argv.push_back(
const_cast<char*
>(arg.c_str()));
35 argv.push_back(
nullptr);
37 posix_spawn_file_actions_t actions;
38 posix_spawn_file_actions_init(&actions);
42 posix_spawn_file_actions_addopen(&actions, STDIN_FILENO,
"/dev/null", O_RDONLY, 0);
43 posix_spawn_file_actions_addopen(&actions, STDOUT_FILENO,
"/dev/null", O_WRONLY, 0);
44 posix_spawn_file_actions_addopen(&actions, STDERR_FILENO,
"/dev/null", O_WRONLY, 0);
48 const int spawnResult = posix_spawnp(&pid, program.c_str(), &actions,
nullptr, argv.data(),
environ);
49 posix_spawn_file_actions_destroy(&actions);
63 waitResult = waitpid(pid, &status, 0);
64 }
while (waitResult == -1 && errno == EINTR);
70 if (WIFEXITED(status))
72 return WEXITSTATUS(status);
74 if (WIFSIGNALED(status))
79 return 128 + WTERMSIG(status);
94 std::wstring quote_windows_argument(
const std::wstring& argument)
96 if (!argument.empty() && argument.find_first_of(L
" \t\"") == std::wstring::npos)
102 quoted.reserve(argument.size() + 2);
103 quoted.push_back(L
'"');
104 std::size_t backslashRun = 0;
105 for (
const auto c : argument)
116 quoted.append(backslashRun * 2, L
'\\');
121 quoted.append(backslashRun, L
'\\');
126 quoted.append(backslashRun * 2, L
'\\');
127 quoted.push_back(L
'"');
132 std::int64_t
run_process(
const std::string& program,
const std::vector<std::string>& args,
bool discardOutput )
142 std::wstring commandLine = quote_windows_argument(std::filesystem::path(program).wstring());
143 for (
const auto& arg : args)
146 commandLine += quote_windows_argument(std::filesystem::path(arg).wstring());
150 STARTUPINFOW startupInfo{};
151 startupInfo.cb =
sizeof(startupInfo);
152 SECURITY_ATTRIBUTES inheritable{
sizeof(SECURITY_ATTRIBUTES),
nullptr, TRUE };
153 HANDLE nullOutput = INVALID_HANDLE_VALUE;
154 HANDLE nullInput = INVALID_HANDLE_VALUE;
157 startupInfo.dwFlags = STARTF_USESTDHANDLES;
158 nullInput = CreateFileW(L
"NUL", GENERIC_READ, 0, &inheritable, OPEN_EXISTING, 0,
nullptr);
159 nullOutput = CreateFileW(L
"NUL", GENERIC_WRITE, 0, &inheritable, OPEN_EXISTING, 0,
nullptr);
160 startupInfo.hStdInput = nullInput;
161 startupInfo.hStdOutput = nullOutput;
162 startupInfo.hStdError = nullOutput;
165 PROCESS_INFORMATION processInfo{};
166 const BOOL created = CreateProcessW(
171 discardOutput ? TRUE : FALSE,
178 if (nullInput != INVALID_HANDLE_VALUE) CloseHandle(nullInput);
179 if (nullOutput != INVALID_HANDLE_VALUE) CloseHandle(nullOutput);
187 WaitForSingleObject(processInfo.hProcess, INFINITE);
188 const BOOL gotExitCode = GetExitCodeProcess(processInfo.hProcess, &exitCode);
189 CloseHandle(processInfo.hThread);
190 CloseHandle(processInfo.hProcess);
200 return static_cast<std::int64_t
>(exitCode);
std::int64_t run_process(const std::string &program, const std::vector< std::string > &args, bool discardOutput)