33 #if defined (HAVE_CONFIG_H)
42 #if defined (__WIN32__) && ! defined (__CYGWIN__)
45 # define WIN32_LEAN_AND_MEAN 1
50 # include <sys/types.h>
56 #if defined (__WIN32__) && ! defined (__CYGWIN__)
59 make_command_string (
const char *cmd,
char *
const *args)
66 cmd_len =
strlen (cmd) + 3;
72 cmd_len +=
strlen (*argp) + 3;
74 command = (
char *)
malloc (cmd_len);
76 sprintf (command,
"\"%s\"", cmd);
80 sprintf (command,
"%s \"%s\"", command, *argp);
86 octave_popen2 (
const char *cmd,
char *
const *args,
bool sync_mode,
87 int *fildes,
const char **errmsg)
94 PROCESS_INFORMATION
pi;
97 HANDLE hProcess = GetCurrentProcess ();
98 HANDLE childRead, childWrite, parentRead, parentWrite;
101 ZeroMemory (&
pi,
sizeof (
pi));
102 ZeroMemory (&si,
sizeof (si));
105 if (! CreatePipe (&childRead, &parentWrite, 0, 0)
106 || ! DuplicateHandle (hProcess, childRead, hProcess, &childRead,
108 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
110 *errmsg =
"popen2: pipe creation failed";
114 if (! CreatePipe (&parentRead, &childWrite, 0, 0)
115 || ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite,
117 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
119 *errmsg =
"popen2: pipe creation failed";
125 pipeMode = PIPE_NOWAIT;
126 SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0);
129 fildes[1] = _open_osfhandle ((intptr_t) parentRead, _O_RDONLY | _O_BINARY);
130 fildes[0] = _open_osfhandle ((intptr_t) parentWrite, _O_WRONLY | _O_BINARY);
132 si.dwFlags |= STARTF_USESTDHANDLES;
134 si.hStdInput = childRead;
135 si.hStdOutput = childWrite;
136 si.hStdError = GetStdHandle (STD_ERROR_HANDLE);
138 command = make_command_string (cmd, args);
140 status = CreateProcess (0, command, 0, 0, TRUE, 0, 0, 0, &si, &
pi);
146 *errmsg =
"popen2: process creation failed";
150 pid =
pi.dwProcessId;
152 CloseHandle (childRead);
153 CloseHandle (childWrite);
155 CloseHandle (
pi.hProcess);
156 CloseHandle (
pi.hThread);
165 int *fildes,
const char **errmsg)
169 int child_stdin[2], child_stdout[2];
171 if (
pipe (child_stdin) < 0)
173 *errmsg = strerror (errno);
177 if (
pipe (child_stdout) < 0)
179 close (child_stdin[0]);
180 close (child_stdin[1]);
182 *errmsg = strerror (errno);
192 close (child_stdin[1]);
193 close (child_stdout[0]);
197 close (child_stdin[0]);
199 if (
dup2 (child_stdout[1], STDOUT_FILENO) >= 0)
201 close (child_stdout[1]);
203 if (
execvp (cmd, args) < 0)
204 perror (
"popen2 (child)");
207 perror (
"popen2 (child)");
210 perror (
"popen2 (child)");
218 close (child_stdin[0]);
219 close (child_stdout[1]);
221 #if defined (F_SETFL) && defined (O_NONBLOCK)
222 if (! sync_mode &&
fcntl (child_stdout[0], F_SETFL, O_NONBLOCK) < 0)
224 *errmsg = strerror (errno);
230 fildes[0] = child_stdin[1];
231 fildes[1] = child_stdout[0];
238 *errmsg = strerror (errno);
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 fork(std::string &msg)
T::size_type strlen(const typename T::value_type *str)
pid_t octave_popen2(const char *cmd, char *const *args, bool sync_mode, int *fildes, const char **errmsg)