491 const char *
const environment[],
496 char *commandLineCombined;
500 unsigned long flags = 0;
501 const unsigned long startFUseStdHandles = 0x00000100;
502 const unsigned long handleFlagInherit = 0x00000001;
503 const unsigned long createNoWindow = 0x08000000;
504 struct subprocess_subprocess_information_s processInfo;
505 struct subprocess_security_attributes_s saAttr = {
sizeof(saAttr),
508 struct subprocess_startup_info_s startInfo = {0,
527 startInfo.cb =
sizeof(startInfo);
528 startInfo.dwFlags = startFUseStdHandles;
531 flags |= createNoWindow;
542 for (i = 0; environment[i]; i++) {
543 for (j = 0;
'\0' != environment[i][j]; j++) {
556 for (i = 0; environment[i]; i++) {
557 for (j = 0;
'\0' != environment[i][j]; j++) {
558 used_environment[len++] = environment[i][j];
561 used_environment[len++] =
'\0';
565 used_environment[len++] =
'\0';
566 used_environment[len++] =
'\0';
579 if (!SetHandleInformation(wr, handleFlagInherit, 0)) {
593 startInfo.hStdInput = rd;
596 if (subprocess_create_named_pipe_helper(&rd, &wr)) {
600 if (!CreatePipe(&rd, &wr,
606 if (!SetHandleInformation(rd, handleFlagInherit, 0)) {
620 startInfo.hStdOutput = wr;
625 startInfo.hStdError = startInfo.hStdOutput;
628 if (subprocess_create_named_pipe_helper(&rd, &wr)) {
632 if (!CreatePipe(&rd, &wr,
638 if (!SetHandleInformation(rd, handleFlagInherit, 0)) {
652 startInfo.hStdError = wr;
656 out_process->hEventOutput =
659 out_process->hEventError =
669 for (i = 0; commandLine[i]; i++) {
678 for (j = 0;
'\0' != commandLine[i][j]; j++) {
679 switch (commandLine[i][j]) {
683 if (commandLine[i][j + 1] ==
'"') {
698 if (!commandLineCombined) {
705 for (i = 0; commandLine[i]; i++) {
707 commandLineCombined[len++] =
' ';
713 commandLineCombined[len++] =
'"';
716 for (j = 0;
'\0' != commandLine[i][j]; j++) {
717 switch (commandLine[i][j]) {
721 if (commandLine[i][j + 1] ==
'"') {
722 commandLineCombined[len++] =
'\\';
727 commandLineCombined[len++] =
'\\';
731 commandLineCombined[len++] = commandLine[i][j];
734 commandLineCombined[len++] =
'"';
738 commandLineCombined[len] =
'\0';
755 out_process->hProcess = processInfo.hProcess;
757 out_process->hStdInput = startInfo.hStdInput;
760 CloseHandle(processInfo.hThread);
763 CloseHandle(startInfo.hStdOutput);
765 if (startInfo.hStdError != startInfo.hStdOutput) {
766 CloseHandle(startInfo.hStdError);
770 out_process->
alive = 1;
778 extern char **environ;
780 posix_spawn_file_actions_t actions;
781 char *
const *used_environment;
790 if (0 != pipe(stdinfd)) {
794 if (0 != pipe(stdoutfd)) {
800 if (0 != pipe(stderrfd)) {
807#pragma clang diagnostic push
808#pragma clang diagnostic ignored "-Wcast-qual"
809#pragma clang diagnostic ignored "-Wold-style-cast"
813#pragma clang diagnostic pop
817 used_environment = environ;
819 used_environment = empty_environment;
822 if (0 != posix_spawn_file_actions_init(&actions)) {
827 if (0 != posix_spawn_file_actions_addclose(&actions, stdinfd[1])) {
828 posix_spawn_file_actions_destroy(&actions);
834 posix_spawn_file_actions_adddup2(&actions, stdinfd[0], STDIN_FILENO)) {
835 posix_spawn_file_actions_destroy(&actions);
840 if (0 != posix_spawn_file_actions_addclose(&actions, stdoutfd[0])) {
841 posix_spawn_file_actions_destroy(&actions);
847 posix_spawn_file_actions_adddup2(&actions, stdoutfd[1], STDOUT_FILENO)) {
848 posix_spawn_file_actions_destroy(&actions);
854 if (0 != posix_spawn_file_actions_adddup2(&actions, STDOUT_FILENO,
856 posix_spawn_file_actions_destroy(&actions);
861 if (0 != posix_spawn_file_actions_addclose(&actions, stderrfd[0])) {
862 posix_spawn_file_actions_destroy(&actions);
866 if (0 != posix_spawn_file_actions_adddup2(&actions, stderrfd[1],
868 posix_spawn_file_actions_destroy(&actions);
874#pragma clang diagnostic push
875#pragma clang diagnostic ignored "-Wcast-qual"
876#pragma clang diagnostic ignored "-Wold-style-cast"
880 if (0 != posix_spawnp(&child, commandLine[0], &actions,
SUBPROCESS_NULL,
883 posix_spawn_file_actions_destroy(&actions);
887 if (0 != posix_spawn(&child, commandLine[0], &actions,
SUBPROCESS_NULL,
890 posix_spawn_file_actions_destroy(&actions);
895#pragma clang diagnostic pop
901 out_process->
stdin_file = fdopen(stdinfd[1],
"wb");
906 out_process->
stdout_file = fdopen(stdoutfd[0],
"rb");
915 out_process->
stderr_file = fdopen(stderrfd[0],
"rb");
919 out_process->
child = child;
921 out_process->
alive = 1;
923 posix_spawn_file_actions_destroy(&actions);