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