33 #if defined (HAVE_CONFIG_H)
42 #if defined (__WIN32__) && ! defined (__CYGWIN__)
45 # define WIN32_LEAN_AND_MEAN 1
52 # include <sys/types.h>
58 #if defined (__WIN32__) && ! defined (__CYGWIN__)
61 make_command_string (
const char *cmd,
char *
const *args)
68 cmd_len =
strlen (cmd) + 3;
74 cmd_len +=
strlen (*argp) + 3;
76 command = (
char *)
malloc (cmd_len);
78 sprintf (command,
"\"%s\"", cmd);
82 sprintf (command,
"%s \"%s\"", command, *argp);
88 octave_popen2 (
const char *cmd,
char *
const *args,
bool sync_mode,
89 int *fildes,
const char **errmsg)
96 PROCESS_INFORMATION
pi;
99 HANDLE hProcess = GetCurrentProcess ();
100 HANDLE childRead, childWrite, parentRead, parentWrite;
103 ZeroMemory (&
pi,
sizeof (
pi));
104 ZeroMemory (&si,
sizeof (si));
107 if (! CreatePipe (&childRead, &parentWrite, 0, 0)
108 || ! DuplicateHandle (hProcess, childRead, hProcess, &childRead,
110 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
112 *errmsg =
"popen2: pipe creation failed";
116 if (! CreatePipe (&parentRead, &childWrite, 0, 0)
117 || ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite,
119 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
121 *errmsg =
"popen2: pipe creation failed";
127 pipeMode = PIPE_NOWAIT;
128 SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0);
131 fildes[1] = _open_osfhandle ((intptr_t) parentRead, _O_RDONLY | _O_BINARY);
132 fildes[0] = _open_osfhandle ((intptr_t) parentWrite, _O_WRONLY | _O_BINARY);
134 si.dwFlags |= STARTF_USESTDHANDLES;
136 si.hStdInput = childRead;
137 si.hStdOutput = childWrite;
138 si.hStdError = GetStdHandle (STD_ERROR_HANDLE);
140 command = make_command_string (cmd, args);
146 status = CreateProcessW (NULL, wcmd, NULL, NULL, TRUE, CREATE_NO_WINDOW,
147 NULL, NULL, &si, &
pi);
153 *errmsg =
"popen2: process creation failed";
157 pid =
pi.dwProcessId;
159 CloseHandle (childRead);
160 CloseHandle (childWrite);
162 CloseHandle (
pi.hProcess);
163 CloseHandle (
pi.hThread);
172 int *fildes,
const char **errmsg)
176 int child_stdin[2], child_stdout[2];
178 if (
pipe (child_stdin) < 0)
180 *errmsg = strerror (errno);
184 if (
pipe (child_stdout) < 0)
186 close (child_stdin[0]);
187 close (child_stdin[1]);
189 *errmsg = strerror (errno);
199 close (child_stdin[1]);
200 close (child_stdout[0]);
204 close (child_stdin[0]);
206 if (
dup2 (child_stdout[1], STDOUT_FILENO) >= 0)
208 close (child_stdout[1]);
210 if (
execvp (cmd, args) < 0)
211 perror (
"popen2 (child)");
214 perror (
"popen2 (child)");
217 perror (
"popen2 (child)");
225 close (child_stdin[0]);
226 close (child_stdout[1]);
228 #if defined (F_SETFL) && defined (O_NONBLOCK)
229 if (! sync_mode &&
fcntl (child_stdout[0], F_SETFL, O_NONBLOCK) < 0)
231 *errmsg = strerror (errno);
237 fildes[0] = child_stdin[1];
238 fildes[1] = child_stdout[0];
245 *errmsg = strerror (errno);
T::size_type strlen(const typename T::value_type *str)
pid_t fork(std::string &msg)
int execvp(const std::string &file, const string_vector &argv)
int fcntl(int fd, int cmd, long arg)
int dup2(int old_fd, int new_fd)
pid_t octave_popen2(const char *cmd, char *const *args, bool sync_mode, int *fildes, const char **errmsg)
wchar_t * u8_to_wchar(const char *u8)