31 #include <sys/types.h>
36 #if defined (__WIN32__) && ! defined (__CYGWIN__)
37 #define WIN32_LEAN_AND_MEAN
57 char *tmp = gnulib::getcwd (0, 0);
75 #if defined (__WIN32__) && ! defined (__CYGWIN__)
76 if (path.length () == 2 && path[1] ==
':')
80 return gnulib::chdir (path.c_str ());
83 #if defined (__WIN32__) && ! defined (__CYGWIN__)
86 octave_popen2 (
const std::string& cmd,
const string_vector& args,
88 int *fildes, std::string& msg)
91 PROCESS_INFORMATION pi;
93 std::string command =
"\"" + cmd +
"\"";
94 HANDLE hProcess = GetCurrentProcess ();
95 HANDLE childRead, childWrite, parentRead, parentWrite;
98 ZeroMemory (&pi,
sizeof (pi));
99 ZeroMemory (&si,
sizeof (si));
102 if (! CreatePipe (&childRead, &parentWrite, 0, 0) ||
103 ! DuplicateHandle (hProcess, childRead, hProcess, &childRead, 0, TRUE,
104 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
106 msg =
"popen2: pipe creation failed";
109 if (! CreatePipe (&parentRead, &childWrite, 0, 0) ||
110 ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite, 0, TRUE,
111 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
113 msg =
"popen2: pipe creation failed";
118 pipeMode = PIPE_NOWAIT;
119 SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0);
121 fildes[1] = _open_osfhandle (reinterpret_cast<intptr_t> (parentRead),
122 _O_RDONLY | _O_BINARY);
123 fildes[0] = _open_osfhandle (reinterpret_cast<intptr_t> (parentWrite),
124 _O_WRONLY | _O_BINARY);
125 si.dwFlags |= STARTF_USESTDHANDLES;
126 si.hStdInput = childRead;
127 si.hStdOutput = childWrite;
130 for (
int k=1; k<args.
length (); k++)
131 command +=
" \"" + args[k] +
"\"";
133 strcpy (c_command, command.c_str ());
134 if (! CreateProcess (0, c_command, 0, 0, TRUE, 0, 0, 0, &si, &pi))
136 msg =
"popen2: process creation failed";
139 pid = pi.dwProcessId;
141 CloseHandle (childRead);
142 CloseHandle (childWrite);
143 CloseHandle (pi.hProcess);
144 CloseHandle (pi.hThread);