Mercurial > hg > octave-nkf
annotate liboctave/oct-syscalls.cc @ 10396:a0b51ac0f88a
optimize accumdim with summation
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 05 Mar 2010 12:31:30 +0100 |
parents | 07ebe522dac2 |
children | 479cc8a0a846 |
rev | line source |
---|---|
2941 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2005, 2006, 2007 |
4 John W. Eaton | |
2941 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2941 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2941 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include <cerrno> | |
6482 | 29 #include <cstdlib> |
2941 | 30 |
31 #include <string.h> | |
32 | |
33 #include <sys/types.h> | |
34 #include <unistd.h> | |
35 | |
36 #include <fcntl.h> | |
37 | |
4299 | 38 // We can't use csignal as kill is not in the std namespace, and picky |
39 // compiler runtimes will also exclude it from global scope as well. | |
40 | |
41 #include <signal.h> | |
42 | |
5453 | 43 #include "lo-utils.h" |
6321 | 44 #include "lo-sysdep.h" |
2941 | 45 #include "oct-syscalls.h" |
46 #include "str-vec.h" | |
47 | |
48 #define NOT_SUPPORTED(nm) \ | |
4062 | 49 nm ": not supported on this system" |
2941 | 50 |
51 int | |
52 octave_syscalls::dup2 (int old_fd, int new_fd) | |
53 { | |
3504 | 54 std::string msg; |
2941 | 55 return dup2 (old_fd, new_fd, msg); |
56 } | |
57 | |
58 int | |
3504 | 59 octave_syscalls::dup2 (int old_fd, int new_fd, std::string& msg) |
2941 | 60 { |
3504 | 61 msg = std::string (); |
2941 | 62 |
63 int status = -1; | |
64 | |
65 #if defined (HAVE_DUP2) | |
66 status = ::dup2 (old_fd, new_fd); | |
67 | |
68 if (status < 0) | |
3504 | 69 { |
70 using namespace std; | |
71 msg = ::strerror (errno); | |
72 } | |
2941 | 73 #else |
74 msg = NOT_SUPPORTED ("dup2"); | |
75 #endif | |
76 | |
77 return status; | |
78 } | |
79 | |
80 int | |
3504 | 81 octave_syscalls::execvp (const std::string& file, const string_vector& argv) |
2941 | 82 { |
3504 | 83 std::string msg; |
2941 | 84 return execvp (file, argv, msg); |
85 } | |
86 | |
87 int | |
3504 | 88 octave_syscalls::execvp (const std::string& file, const string_vector& args, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
89 std::string& msg) |
2941 | 90 { |
3504 | 91 msg = std::string (); |
2941 | 92 |
93 int status = -1; | |
94 | |
95 #if defined (HAVE_EXECVP) | |
96 char **argv = args.c_str_vec (); | |
97 | |
98 status = ::execvp (file.c_str (), argv); | |
99 | |
100 string_vector::delete_c_str_vec (argv); | |
101 | |
102 if (status < 0) | |
3504 | 103 { |
104 using namespace std; | |
105 msg = ::strerror (errno); | |
106 } | |
2941 | 107 #else |
108 msg = NOT_SUPPORTED ("execvp"); | |
109 #endif | |
110 | |
111 return status; | |
112 } | |
113 | |
114 pid_t | |
3504 | 115 octave_syscalls::fork (std::string& msg) |
2941 | 116 { |
117 pid_t status = -1; | |
118 | |
119 #if defined (HAVE_FORK) | |
120 status = ::fork (); | |
121 | |
122 if (status < 0) | |
3504 | 123 { |
124 using namespace std; | |
125 msg = ::strerror (errno); | |
126 } | |
2941 | 127 #else |
128 msg = NOT_SUPPORTED ("fork"); | |
129 #endif | |
130 | |
131 return status; | |
132 } | |
133 | |
134 pid_t | |
3504 | 135 octave_syscalls::vfork (std::string& msg) |
3147 | 136 { |
137 pid_t status = -1; | |
138 | |
139 #if defined (HAVE_VFORK) || defined (HAVE_FORK) | |
140 #if defined (HAVE_VFORK) | |
141 status = ::vfork (); | |
142 #else | |
3148 | 143 status = ::fork (); |
3147 | 144 #endif |
145 | |
146 if (status < 0) | |
3504 | 147 { |
148 using namespace std; | |
149 msg = ::strerror (errno); | |
150 } | |
3147 | 151 #else |
152 msg = NOT_SUPPORTED ("vfork"); | |
153 #endif | |
154 | |
155 return status; | |
156 } | |
157 | |
158 pid_t | |
3504 | 159 octave_syscalls::getpgrp (std::string& msg) |
2941 | 160 { |
161 pid_t status = -1; | |
162 | |
163 #if defined (HAVE_GETPGRP) | |
164 status = ::getpgrp (); | |
165 | |
166 if (status < 0) | |
3504 | 167 { |
168 using namespace std; | |
169 msg = ::strerror (errno); | |
170 } | |
2941 | 171 #else |
172 msg = NOT_SUPPORTED ("getpgrp"); | |
173 #endif | |
174 | |
175 return status; | |
176 } | |
177 | |
178 pid_t | |
179 octave_syscalls::getpid (void) | |
180 { | |
181 #if defined (HAVE_GETPID) | |
182 return ::getpid (); | |
183 #else | |
184 return 0; | |
185 #endif | |
186 } | |
187 | |
188 pid_t | |
189 octave_syscalls::getppid (void) | |
190 { | |
191 #if defined (HAVE_GETPPID) | |
192 return ::getppid (); | |
193 #else | |
194 return 0; | |
195 #endif | |
196 } | |
197 | |
198 gid_t | |
199 octave_syscalls::getgid (void) | |
200 { | |
201 #if defined (HAVE_GETGID) | |
202 return ::getgid (); | |
203 #else | |
204 return 0; | |
205 #endif | |
206 } | |
207 | |
208 gid_t | |
209 octave_syscalls::getegid (void) | |
210 { | |
211 #if defined (HAVE_GETEGID) | |
212 return ::getegid (); | |
213 #else | |
214 return 0; | |
215 #endif | |
216 } | |
217 | |
218 uid_t | |
219 octave_syscalls::getuid (void) | |
220 { | |
221 #if defined (HAVE_GETUID) | |
222 return ::getuid (); | |
223 #else | |
224 return 0; | |
225 #endif | |
226 } | |
227 | |
228 uid_t | |
229 octave_syscalls::geteuid (void) | |
230 { | |
231 #if defined (HAVE_GETEUID) | |
232 return ::geteuid (); | |
233 #else | |
234 return 0; | |
235 #endif | |
236 } | |
237 | |
238 int | |
239 octave_syscalls::pipe (int *fildes) | |
240 { | |
3504 | 241 std::string msg; |
6123 | 242 return pipe (fildes, msg); |
2941 | 243 } |
244 | |
245 int | |
3504 | 246 octave_syscalls::pipe (int *fildes, std::string& msg) |
2941 | 247 { |
3504 | 248 msg = std::string (); |
2941 | 249 |
250 int status = -1; | |
251 | |
252 #if defined (HAVE_PIPE) | |
253 status = ::pipe (fildes); | |
254 | |
255 if (status < 0) | |
3504 | 256 { |
257 using namespace std; | |
258 msg = ::strerror (errno); | |
259 } | |
2941 | 260 #else |
261 msg = NOT_SUPPORTED ("pipe"); | |
262 #endif | |
263 | |
264 return status; | |
265 } | |
266 | |
267 pid_t | |
5453 | 268 octave_syscalls::waitpid (pid_t pid, int *status, int options) |
2941 | 269 { |
3504 | 270 std::string msg; |
5453 | 271 return waitpid (pid, status, options, msg); |
2941 | 272 } |
273 | |
274 pid_t | |
5453 | 275 octave_syscalls::waitpid (pid_t pid, int *status, int options, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
276 std::string& msg) |
2941 | 277 { |
6060 | 278 pid_t retval = -1; |
3504 | 279 msg = std::string (); |
2941 | 280 |
5453 | 281 #if defined (HAVE_WAITPID) |
6060 | 282 retval = ::octave_waitpid (pid, status, options); |
2941 | 283 |
5453 | 284 if (retval < 0) |
3504 | 285 { |
286 using namespace std; | |
287 msg = ::strerror (errno); | |
288 } | |
2941 | 289 #else |
290 msg = NOT_SUPPORTED ("waitpid"); | |
291 #endif | |
292 | |
5453 | 293 return retval; |
2941 | 294 } |
295 | |
4294 | 296 int |
297 octave_syscalls::kill (pid_t pid, int sig) | |
298 { | |
299 std::string msg; | |
300 return kill (pid, sig, msg); | |
301 } | |
302 | |
303 int | |
304 octave_syscalls::kill (pid_t pid, int sig, std::string& msg) | |
305 { | |
306 msg = std::string (); | |
307 | |
308 int status = -1; | |
309 | |
310 #if defined (HAVE_KILL) | |
311 status = ::kill (pid, sig); | |
312 | |
313 if (status < 0) | |
314 { | |
315 using namespace std; | |
316 msg = ::strerror (errno); | |
317 } | |
318 #else | |
319 msg = NOT_SUPPORTED ("kill"); | |
320 #endif | |
321 | |
322 return status; | |
323 } | |
324 | |
6321 | 325 pid_t |
326 octave_syscalls::popen2 (const std::string& cmd, const string_vector& args, | |
327 bool sync_mode, int *fildes) | |
328 { | |
329 std::string msg; | |
330 bool interactive = false; | |
331 return popen2 (cmd, args, sync_mode, fildes, msg, interactive); | |
332 } | |
333 | |
334 pid_t | |
335 octave_syscalls::popen2 (const std::string& cmd, const string_vector& args, | |
336 bool sync_mode, int *fildes, std::string& msg) | |
337 { | |
338 bool interactive = false; | |
339 return popen2 (cmd, args, sync_mode, fildes, msg, interactive); | |
340 } | |
341 | |
342 pid_t | |
343 octave_syscalls::popen2 (const std::string& cmd, const string_vector& args, | |
344 bool sync_mode, int *fildes, std::string& msg, bool &interactive) | |
345 { | |
346 #if defined (__WIN32__) && ! defined (__CYGWIN__) | |
347 return ::octave_popen2 (cmd, args, sync_mode, fildes, msg); | |
348 #else | |
349 pid_t pid; | |
350 int child_stdin[2], child_stdout[2]; | |
351 | |
352 if (pipe (child_stdin, msg) == 0) | |
353 { | |
354 if (pipe (child_stdout, msg) == 0) | |
355 { | |
356 pid = fork (msg); | |
357 if (pid < 0) | |
358 msg = "popen2: process creation failed -- " + msg; | |
359 else if (pid == 0) | |
360 { | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
361 std::string child_msg; |
6321 | 362 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
363 interactive = false; |
6321 | 364 |
365 // Child process | |
366 ::close (child_stdin[1]); | |
367 ::close (child_stdout[0]); | |
368 | |
369 if (dup2 (child_stdin[0], STDIN_FILENO) >= 0) | |
370 { | |
371 ::close (child_stdin[0]); | |
372 if (dup2 (child_stdout[1], STDOUT_FILENO) >= 0) | |
373 { | |
374 ::close (child_stdout[1]); | |
375 if (execvp (cmd, args, child_msg) < 0) | |
376 child_msg = "popen2 (child): unable to start process -- " + child_msg; | |
377 } | |
378 else | |
379 child_msg = "popen2 (child): file handle duplication failed -- " + child_msg; | |
380 } | |
381 else | |
382 child_msg = "popen2 (child): file handle duplication failed -- " + child_msg; | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
383 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
384 (*current_liboctave_error_handler)(child_msg.c_str()); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
385 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
386 exit(0); |
6321 | 387 } |
388 else | |
389 { | |
390 // Parent process | |
391 ::close (child_stdin[0]); | |
392 ::close (child_stdout[1]); | |
393 #if defined (F_SETFL) && defined (O_NONBLOCK) | |
10259 | 394 if (! sync_mode && octave_fcntl (child_stdout[0], F_SETFL, O_NONBLOCK, msg) < 0) |
6321 | 395 msg = "popen2: error setting file mode -- " + msg; |
396 else | |
397 #endif | |
398 { | |
399 fildes[0] = child_stdin [1]; | |
400 fildes[1] = child_stdout [0]; | |
401 return pid; | |
402 } | |
403 } | |
404 ::close (child_stdout[0]); | |
405 ::close (child_stdout[1]); | |
406 } | |
407 else | |
408 msg = "popen2: pipe creation failed -- " + msg; | |
409 ::close (child_stdin[0]); | |
410 ::close (child_stdin[1]); | |
411 } | |
412 else | |
413 msg = "popen2: pipe creation failed -- " + msg; | |
414 | |
415 return -1; | |
416 #endif | |
417 } | |
10259 | 418 |
419 int | |
420 octave_fcntl (int fd, int cmd, long arg) | |
421 { | |
422 std::string msg; | |
423 return octave_fcntl (fd, cmd, arg, msg); | |
424 } | |
425 | |
426 int | |
427 octave_fcntl (int fd, int cmd, long arg, std::string& msg) | |
428 { | |
429 msg = std::string (); | |
430 | |
431 int status = -1; | |
432 | |
433 status = ::fcntl (fd, cmd, arg); | |
434 | |
435 if (status < 0) | |
436 { | |
437 using namespace std; | |
438 msg = ::strerror (errno); | |
439 } | |
440 | |
441 return status; | |
442 } |