Mercurial > octave
annotate liboctave/wrappers/octave-popen2.c @ 30564:796f54d4ddbf stable
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2021.
In all .txi and .texi files except gpl.txi and gpl.texi in the
doc/liboctave and doc/interpreter directories, change the copyright
to "Octave Project Developers", the same as used for other source
files. Update copyright notices for 2022 (not done since 2019). For
gpl.txi and gpl.texi, change the copyright notice to be "Free Software
Foundation, Inc." and leave the date at 2007 only because this file
only contains the text of the GPL, not anything created by the Octave
Project Developers.
Add Paul Thomas to contributors.in.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 28 Dec 2021 18:22:40 -0500 |
parents | d1cb1b0e9c05 |
children | 597f3ee61a48 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29538
diff
changeset
|
3 // Copyright (C) 2016-2022 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
21911 | 25 |
26 // It makes more sense to define octave_popen2 with the wrapper | |
27 // functions than it does to try to provide wrappers for all the | |
28 // individual functions and macros that it uses and that may be provided | |
29 // by gnulib. We don't include gnulib headers directly in Octave's C++ | |
30 // source files to avoid problems that may be caused by the way that | |
31 // gnulib overrides standard library functions. | |
32 | |
33 #if defined (HAVE_CONFIG_H) | |
34 # include "config.h" | |
35 #endif | |
36 | |
37 #include <stdbool.h> | |
21933
efe9df9a1071
* octave-popen2.c: Include stdio.h for perror decl and fcnt.h for fcntl decl.
John W. Eaton <jwe@octave.org>
parents:
21911
diff
changeset
|
38 #include <stdio.h> |
21911 | 39 #include <stdlib.h> |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
40 #include <string.h> |
21933
efe9df9a1071
* octave-popen2.c: Include stdio.h for perror decl and fcnt.h for fcntl decl.
John W. Eaton <jwe@octave.org>
parents:
21911
diff
changeset
|
41 |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
42 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
43 # include <fcntl.h> |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
44 # include <io.h> |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
45 # define WIN32_LEAN_AND_MEAN 1 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
46 # include <windows.h> |
29538
d1cb1b0e9c05
Windows: Support opening pipe with non-ASCII characters in command.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29358
diff
changeset
|
47 |
d1cb1b0e9c05
Windows: Support opening pipe with non-ASCII characters in command.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29358
diff
changeset
|
48 # include "uniconv-wrappers.h" |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
49 #else |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
50 # include <errno.h> |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
51 # include <fcntl.h> |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
52 # include <sys/types.h> |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
53 # include <unistd.h> |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
54 #endif |
21911 | 55 |
56 #include "octave-popen2.h" | |
57 | |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
58 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
59 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
60 static char * |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
61 make_command_string (const char *cmd, char *const *args) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
62 { |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
63 char *const *argp; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
64 size_t cmd_len; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
65 char *command; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
66 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
67 // Count Command length, quotes, and terminating NUL character. |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
68 cmd_len = strlen (cmd) + 3; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
69 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
70 // Count argument length, space, and quotes. |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
71 // Ignore first arg as it is the command. |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
72 argp = args; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
73 while (*++argp) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
74 cmd_len += strlen (*argp) + 3; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
75 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
76 command = (char *) malloc (cmd_len); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
77 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
78 sprintf (command, "\"%s\"", cmd); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
79 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
80 argp = args; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
81 while (*++argp) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
82 sprintf (command, "%s \"%s\"", command, *argp); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
83 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
84 return command; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
85 } |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
86 |
21911 | 87 pid_t |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
88 octave_popen2 (const char *cmd, char *const *args, bool sync_mode, |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
89 int *fildes, const char **errmsg) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
90 { |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
91 pid_t pid; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
92 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
93 char *command; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
94 bool status; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
95 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
96 PROCESS_INFORMATION pi; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
97 STARTUPINFO si; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
98 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
99 HANDLE hProcess = GetCurrentProcess (); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
100 HANDLE childRead, childWrite, parentRead, parentWrite; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
101 DWORD pipeMode; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
102 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
103 ZeroMemory (&pi, sizeof (pi)); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
104 ZeroMemory (&si, sizeof (si)); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
105 si.cb = sizeof (si); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
106 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
107 if (! CreatePipe (&childRead, &parentWrite, 0, 0) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
108 || ! DuplicateHandle (hProcess, childRead, hProcess, &childRead, |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
109 0, TRUE, |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
110 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
111 { |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
112 *errmsg = "popen2: pipe creation failed"; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
113 return -1; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
114 } |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
115 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
116 if (! CreatePipe (&parentRead, &childWrite, 0, 0) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
117 || ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite, |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
118 0, TRUE, |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
119 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
120 { |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
121 *errmsg = "popen2: pipe creation failed"; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
122 return -1; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
123 } |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
124 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
125 if (! sync_mode) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
126 { |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
127 pipeMode = PIPE_NOWAIT; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
128 SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
129 } |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
130 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
131 fildes[1] = _open_osfhandle ((intptr_t) parentRead, _O_RDONLY | _O_BINARY); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
132 fildes[0] = _open_osfhandle ((intptr_t) parentWrite, _O_WRONLY | _O_BINARY); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
133 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
134 si.dwFlags |= STARTF_USESTDHANDLES; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
135 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
136 si.hStdInput = childRead; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
137 si.hStdOutput = childWrite; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
138 si.hStdError = GetStdHandle (STD_ERROR_HANDLE); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
139 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
140 command = make_command_string (cmd, args); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
141 |
29538
d1cb1b0e9c05
Windows: Support opening pipe with non-ASCII characters in command.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29358
diff
changeset
|
142 wchar_t *wcmd = u8_to_wchar (command); |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
143 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
144 free (command); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
145 |
29538
d1cb1b0e9c05
Windows: Support opening pipe with non-ASCII characters in command.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29358
diff
changeset
|
146 status = CreateProcessW (NULL, wcmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, |
d1cb1b0e9c05
Windows: Support opening pipe with non-ASCII characters in command.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29358
diff
changeset
|
147 NULL, NULL, &si, &pi); |
d1cb1b0e9c05
Windows: Support opening pipe with non-ASCII characters in command.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29358
diff
changeset
|
148 |
d1cb1b0e9c05
Windows: Support opening pipe with non-ASCII characters in command.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29358
diff
changeset
|
149 free (wcmd); |
d1cb1b0e9c05
Windows: Support opening pipe with non-ASCII characters in command.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29358
diff
changeset
|
150 |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
151 if (! status) |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
152 { |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
153 *errmsg = "popen2: process creation failed"; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
154 return -1; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
155 } |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
156 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
157 pid = pi.dwProcessId; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
158 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
159 CloseHandle (childRead); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
160 CloseHandle (childWrite); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
161 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
162 CloseHandle (pi.hProcess); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
163 CloseHandle (pi.hThread); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
164 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
165 return pid; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
166 } |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
167 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
168 #else |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
169 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
170 pid_t |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
171 octave_popen2 (const char *cmd, char *const *args, bool sync_mode, |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
172 int *fildes, const char **errmsg) |
21911 | 173 { |
174 pid_t pid; | |
175 | |
176 int child_stdin[2], child_stdout[2]; | |
177 | |
178 if (pipe (child_stdin) < 0) | |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
179 { |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
180 *errmsg = strerror (errno); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
181 return -1; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
182 } |
21911 | 183 |
184 if (pipe (child_stdout) < 0) | |
185 { | |
186 close (child_stdin[0]); | |
187 close (child_stdin[1]); | |
188 | |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
189 *errmsg = strerror (errno); |
21911 | 190 return -1; |
191 } | |
192 | |
193 pid = fork (); | |
194 | |
195 if (pid == 0) | |
196 { | |
197 // Child process | |
198 | |
199 close (child_stdin[1]); | |
200 close (child_stdout[0]); | |
201 | |
202 if (dup2 (child_stdin[0], STDIN_FILENO) >= 0) | |
203 { | |
204 close (child_stdin[0]); | |
205 | |
206 if (dup2 (child_stdout[1], STDOUT_FILENO) >= 0) | |
207 { | |
208 close (child_stdout[1]); | |
209 | |
210 if (execvp (cmd, args) < 0) | |
211 perror ("popen2 (child)"); | |
212 } | |
213 else | |
214 perror ("popen2 (child)"); | |
215 } | |
216 else | |
217 perror ("popen2 (child)"); | |
218 | |
22730
838d52a3283e
Stop segfault when popen2 called with invalid command (bug #49525).
Mike Miller <mtmiller@octave.org>
parents:
22402
diff
changeset
|
219 _exit (127); |
21911 | 220 } |
221 else if (pid > 0) | |
222 { | |
223 // Parent process | |
224 | |
225 close (child_stdin[0]); | |
226 close (child_stdout[1]); | |
227 | |
228 #if defined (F_SETFL) && defined (O_NONBLOCK) | |
229 if (! sync_mode && fcntl (child_stdout[0], F_SETFL, O_NONBLOCK) < 0) | |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
230 { |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
231 *errmsg = strerror (errno); |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
232 return -1; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
233 } |
21911 | 234 else |
235 #endif | |
236 { | |
237 fildes[0] = child_stdin[1]; | |
238 fildes[1] = child_stdout[0]; | |
239 | |
240 return pid; | |
241 } | |
242 } | |
243 | |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
244 *errmsg = "foobar!"; |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
245 *errmsg = strerror (errno); |
21911 | 246 return pid; |
247 } | |
21949
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
248 |
baeffde5c87d
fix build for Windows systems
John W. Eaton <jwe@octave.org>
parents:
21933
diff
changeset
|
249 #endif |