2075
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2075
|
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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 // Thomas Baier <baier@ci.tuwien.ac.at> added the original versions of |
|
24 // the following functions: |
|
25 // |
|
26 // mkfifo unlink waitpid |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
2669
|
32 #include <cerrno> |
2075
|
33 #include <cstdio> |
2669
|
34 #include <cstring> |
2075
|
35 |
|
36 #ifdef HAVE_UNISTD_H |
2442
|
37 #ifdef HAVE_SYS_TYPES_H |
2075
|
38 #include <sys/types.h> |
2442
|
39 #endif |
2075
|
40 #include <unistd.h> |
|
41 #endif |
|
42 |
|
43 #ifdef HAVE_FCNTL_H |
|
44 #include <fcntl.h> |
|
45 #endif |
|
46 |
|
47 #include "defun.h" |
|
48 #include "error.h" |
|
49 #include "file-ops.h" |
2078
|
50 #include "gripes.h" |
2075
|
51 #include "help.h" |
|
52 #include "lo-utils.h" |
|
53 #include "oct-map.h" |
|
54 #include "oct-obj.h" |
|
55 #include "oct-stdstrm.h" |
|
56 #include "oct-stream.h" |
|
57 #include "sysdep.h" |
|
58 #include "syswait.h" |
|
59 #include "utils.h" |
2366
|
60 #include "variables.h" |
2075
|
61 |
|
62 static Octave_map |
|
63 mk_stat_map (const file_stat& fs) |
|
64 { |
|
65 Octave_map m; |
|
66 |
2800
|
67 m["dev"] = static_cast<double> (fs.dev ()); |
|
68 m["ino"] = static_cast<double> (fs.ino ()); |
2075
|
69 m["modestr"] = fs.mode_as_string (); |
2800
|
70 m["nlink"] = static_cast<double> (fs.nlink ()); |
|
71 m["uid"] = static_cast<double> (fs.uid ()); |
|
72 m["gid"] = static_cast<double> (fs.gid ()); |
2075
|
73 #if defined (HAVE_ST_RDEV) |
2800
|
74 m["rdev"] = static_cast<double> (fs.rdev ()); |
2075
|
75 #endif |
2800
|
76 m["size"] = static_cast<double> (fs.size ()); |
|
77 m["atime"] = static_cast<double> (fs.atime ()); |
|
78 m["mtime"] = static_cast<double> (fs.mtime ()); |
|
79 m["ctime"] = static_cast<double> (fs.ctime ()); |
2075
|
80 #if defined (HAVE_ST_BLKSIZE) |
2800
|
81 m["blksize"] = static_cast<double> (fs.blksize ()); |
2075
|
82 #endif |
|
83 #if defined (HAVE_ST_BLOCKS) |
2800
|
84 m["blocks"] = static_cast<double> (fs.blocks ()); |
2075
|
85 #endif |
|
86 |
|
87 return m; |
|
88 } |
|
89 |
2457
|
90 DEFUN (dup2, args, , |
2669
|
91 "[FID, MSG] = dup2 (OLD, NEW)\n\ |
|
92 \n\ |
|
93 Duplicate a file descriptor.\n\ |
|
94 \n\ |
|
95 If successful, FID is greater than zero and contains the new file ID.\n\ |
|
96 Otherwise, FID is negative and MSG contains a system-dependent error message.") |
2075
|
97 { |
2669
|
98 octave_value_list retval; |
|
99 |
|
100 retval(1) = string (); |
|
101 retval(0) = -1.0; |
2075
|
102 |
|
103 int nargin = args.length (); |
|
104 |
|
105 if (nargin == 2) |
|
106 { |
2475
|
107 #if defined (HAVE_DUP2) |
2075
|
108 double d_old = args(0).double_value (); |
|
109 double d_new = args(1).double_value (); |
|
110 |
|
111 if (! error_state) |
|
112 { |
|
113 if (D_NINT (d_old) == d_old && D_NINT (d_new) == d_new) |
|
114 { |
|
115 int i_old = NINT (d_old); |
|
116 int i_new = NINT (d_new); |
|
117 |
|
118 // XXX FIXME XXX -- are these checks sufficient? |
|
119 if (i_old >= 0 && i_new >= 0) |
2669
|
120 { |
|
121 int status = dup2 (i_old, i_new); |
|
122 |
2800
|
123 retval(0) = static_cast<double> (status); |
2669
|
124 |
|
125 if (status < 0) |
|
126 retval(1) = strerror (errno); |
|
127 } |
2075
|
128 else |
|
129 error ("dup2: invalid file id"); |
|
130 } |
|
131 else |
|
132 error ("dup2: arguments must be integer values"); |
|
133 } |
2475
|
134 #else |
|
135 gripe_not_supported ("dup2"); |
|
136 #endif |
2075
|
137 } |
|
138 else |
|
139 print_usage ("dup2"); |
|
140 |
|
141 return retval; |
|
142 } |
|
143 |
2457
|
144 DEFUN (exec, args, , |
2669
|
145 "[STATUS, MSG] = exec (FILE, ARGS)\n\ |
|
146 \n\ |
|
147 Replace current process with a new process.\n\ |
|
148 \n\ |
2802
|
149 If successful, exec does not return. If exec does return, status will\n\ |
2669
|
150 be nonzero, and MSG will contain a system-dependent error message.") |
2075
|
151 { |
2669
|
152 octave_value_list retval; |
|
153 |
|
154 retval(1) = string (); |
|
155 retval(0) = -1.0; |
2075
|
156 |
|
157 int nargin = args.length (); |
|
158 |
|
159 if (nargin == 1 || nargin == 2) |
|
160 { |
2475
|
161 #if defined (HAVE_EXECVP) |
2075
|
162 string exec_file = args(0).string_value (); |
|
163 |
|
164 if (! error_state) |
|
165 { |
|
166 char **exec_args = 0; |
|
167 |
|
168 if (nargin == 2) |
|
169 { |
|
170 charMatrix chm = args(1).all_strings (); |
|
171 |
|
172 if (! error_state) |
|
173 { |
|
174 int nr = chm.rows (); |
|
175 int nc = chm.cols (); |
|
176 |
|
177 exec_args = new char * [nr+2]; |
|
178 |
|
179 // XXX FIXME XXX -- potential leak? |
|
180 |
|
181 exec_args[0] = strsave (exec_file.c_str ()); |
|
182 exec_args[nr+1] = 0; |
|
183 |
|
184 for (int i = 0; i < nr; i++) |
|
185 { |
|
186 exec_args[i+1] = new char [nc+1]; |
|
187 |
|
188 for (int j = 0; j < nc; j++) |
2305
|
189 exec_args[i+1][j] = chm (i, j); |
2075
|
190 |
|
191 exec_args[i+1][nc] = '\0'; |
|
192 } |
|
193 } |
|
194 else |
|
195 error ("exec: arguments must be strings"); |
|
196 } |
|
197 else |
|
198 { |
|
199 exec_args = new char * [2]; |
|
200 |
|
201 exec_args[0] = strsave (exec_file.c_str ()); |
|
202 exec_args[1] = 0; |
|
203 } |
|
204 |
|
205 if (! error_state) |
2669
|
206 { |
|
207 int status = execvp (exec_file.c_str (), exec_args); |
|
208 |
2800
|
209 retval(0) = static_cast<double> (status); |
2669
|
210 |
|
211 if (status < 0) |
|
212 retval(1) = strerror (errno); |
|
213 } |
2075
|
214 } |
|
215 else |
|
216 error ("exec: first argument must be a string"); |
2475
|
217 #else |
|
218 gripe_not_supported ("exec"); |
|
219 #endif |
2075
|
220 } |
|
221 else |
|
222 print_usage ("exec"); |
|
223 |
|
224 return retval; |
|
225 } |
|
226 |
2457
|
227 DEFUN (fcntl, args, , |
2669
|
228 "[STATUS, MSG] = fcntl (FID, REQUEST, ARGUMENT)\n\ |
|
229 \n\ |
|
230 Control open file descriptors.\n\ |
|
231 \n\ |
|
232 If successful, STATUS is 0 and MSG is an empty string. Otherwise,\n\ |
|
233 STATUS is nonzero and MSG contains a system-dependent error message.") |
2075
|
234 { |
2669
|
235 octave_value_list retval; |
|
236 |
|
237 retval(1) = string (); |
|
238 retval(0) = -1.0; |
2075
|
239 |
|
240 int nargin = args.length (); |
|
241 |
|
242 if (nargin == 3) |
|
243 { |
2475
|
244 #if defined (HAVE_FCNTL) |
2075
|
245 double d_fid = args(0).double_value (); |
|
246 double d_req = args(1).double_value (); |
|
247 double d_arg = args(2).double_value (); |
|
248 |
|
249 if (! error_state |
|
250 && D_NINT (d_fid) == d_fid |
|
251 && D_NINT (d_req) == d_req |
|
252 && D_NINT (d_arg) == d_arg) |
|
253 { |
|
254 int fid = NINT (d_fid); |
|
255 int req = NINT (d_req); |
|
256 int arg = NINT (d_arg); |
|
257 |
|
258 // XXX FIXME XXX -- Need better checking here? |
|
259 if (fid < 0) |
|
260 error ("fcntl: invalid file id"); |
|
261 else |
2669
|
262 { |
|
263 int status = fcntl (fid, req, arg); |
|
264 |
2800
|
265 retval(0) = static_cast<double> (status); |
2669
|
266 |
|
267 if (status < 0) |
|
268 retval(1) = strerror (errno); |
|
269 } |
2075
|
270 } |
|
271 else |
|
272 error ("fcntl: file id must be an integer"); |
2475
|
273 #else |
|
274 gripe_not_supported ("fcntl"); |
|
275 #endif |
2075
|
276 } |
|
277 else |
|
278 print_usage ("fcntl"); |
|
279 |
|
280 return retval; |
|
281 } |
|
282 |
2457
|
283 DEFUN (fork, args, , |
2669
|
284 "[PID, MSG] = fork ()\n\ |
|
285 \n\ |
|
286 Create a copy of the current process.\n\ |
|
287 \n\ |
|
288 If successful, PID is either the process ID and you are in the parent,\n\ |
|
289 or 0, and you are in the child. If PID is less than zero, an error\n\ |
|
290 has occured, and MSG contains a system-dependent error message.") |
2075
|
291 { |
2669
|
292 octave_value_list retval; |
|
293 |
|
294 retval(1) = string (); |
|
295 retval(0) = -1.0; |
2075
|
296 |
|
297 int nargin = args.length (); |
|
298 |
|
299 if (nargin == 0) |
2475
|
300 { |
|
301 #if defined (HAVE_FORK) |
2669
|
302 pid_t pid = fork (); |
|
303 |
2800
|
304 retval(0) = static_cast<double> (pid); |
2669
|
305 |
|
306 if (pid < 0) |
|
307 retval(1) = strerror (errno); |
2475
|
308 #else |
|
309 gripe_not_supported ("fork"); |
|
310 #endif |
|
311 } |
2075
|
312 else |
|
313 print_usage ("fork"); |
|
314 |
|
315 return retval; |
|
316 } |
|
317 |
2457
|
318 DEFUN (getpgrp, args, , |
2075
|
319 "pgid = getpgrp (): return the process group id of the current process") |
|
320 { |
|
321 double retval = -1.0; |
|
322 |
|
323 int nargin = args.length (); |
|
324 |
|
325 if (nargin == 0) |
2475
|
326 { |
|
327 #if defined (HAVE_GETPGRP) |
|
328 retval = getpgrp (); |
|
329 #else |
|
330 gripe_not_supported ("getpgrp"); |
|
331 #endif |
|
332 } |
2075
|
333 else |
|
334 print_usage ("getpgrp"); |
|
335 |
|
336 return retval; |
|
337 } |
|
338 |
2457
|
339 DEFUN (getpid, args, , |
2075
|
340 "pid = getpid (): return the process id of the current process") |
|
341 { |
|
342 double retval = -1.0; |
|
343 |
|
344 int nargin = args.length (); |
|
345 |
|
346 if (nargin == 0) |
2475
|
347 { |
|
348 #if defined (HAVE_GETPID) |
|
349 retval = getpid (); |
|
350 #else |
|
351 gripe_not_supported ("getpid"); |
|
352 #endif |
|
353 } |
2075
|
354 else |
|
355 print_usage ("getpid"); |
|
356 |
|
357 return retval; |
|
358 } |
|
359 |
2457
|
360 DEFUN (getppid, args, , |
2075
|
361 "pid = getppid (): return the process id of the parent process") |
|
362 { |
|
363 double retval = -1.0; |
|
364 |
2475
|
365 int nargin = args.length (); |
|
366 |
|
367 if (nargin == 0) |
|
368 { |
2075
|
369 #if defined (HAVE_GETPPID) |
2475
|
370 retval = getppid (); |
|
371 #else |
|
372 gripe_not_supported ("getppid"); |
|
373 #endif |
|
374 } |
|
375 else |
|
376 print_usage ("getppid"); |
|
377 |
|
378 return retval; |
|
379 } |
|
380 |
|
381 DEFUN (getegid, args, , |
|
382 "gid = getegid (): return the effective group id of the current process") |
|
383 { |
|
384 double retval = -1.0; |
|
385 |
2075
|
386 int nargin = args.length (); |
|
387 |
|
388 if (nargin == 0) |
2475
|
389 { |
|
390 #if defined (HAVE_GETEGID) |
|
391 retval = getegid (); |
|
392 #else |
|
393 gripe_not_supported ("getegid"); |
|
394 #endif |
|
395 } |
2075
|
396 else |
2475
|
397 print_usage ("getegid"); |
|
398 |
|
399 return retval; |
|
400 } |
|
401 |
|
402 DEFUN (getgid, args, , |
|
403 "gid = getgid (): return the real group id of the current process") |
|
404 { |
|
405 double retval = -1.0; |
|
406 |
|
407 int nargin = args.length (); |
|
408 |
|
409 if (nargin == 0) |
|
410 { |
|
411 #if defined (HAVE_GETGID) |
|
412 retval = getgid (); |
2075
|
413 #else |
2475
|
414 gripe_not_supported ("getgid"); |
2075
|
415 #endif |
2475
|
416 } |
|
417 else |
|
418 print_usage ("getgid"); |
2075
|
419 |
|
420 return retval; |
|
421 } |
|
422 |
2473
|
423 DEFUN (geteuid, args, , |
2472
|
424 "uid = geteuid (): return the effective user id of the current process") |
|
425 { |
|
426 double retval = -1.0; |
|
427 |
|
428 int nargin = args.length (); |
|
429 |
|
430 if (nargin == 0) |
2475
|
431 { |
|
432 #if defined (HAVE_GETEUID) |
|
433 retval = geteuid (); |
|
434 #else |
|
435 gripe_not_supported ("geteuid"); |
|
436 #endif |
|
437 } |
2472
|
438 else |
|
439 print_usage ("geteuid"); |
2473
|
440 |
|
441 return retval; |
2472
|
442 } |
|
443 |
2473
|
444 DEFUN (getuid, args, , |
2472
|
445 "uid = getuid (): return the real user id of the current process") |
|
446 { |
|
447 double retval = -1.0; |
|
448 |
|
449 int nargin = args.length (); |
|
450 |
|
451 if (nargin == 0) |
2475
|
452 { |
|
453 #if defined (HAVE_GETUID) |
|
454 retval = getuid (); |
|
455 #else |
|
456 gripe_not_supported ("getuid"); |
|
457 #endif |
|
458 } |
2472
|
459 else |
|
460 print_usage ("getuid"); |
2473
|
461 |
|
462 return retval; |
2472
|
463 } |
|
464 |
2075
|
465 DEFUN (lstat, args, , |
2262
|
466 "[S, ERR, MSG] = lstat (NAME)\n\ |
2075
|
467 \n\ |
2262
|
468 Like [S, ERR, MSG] = stat (NAME), but if NAME refers to a symbolic\n\ |
|
469 link, returns information about the link itself, not the file that it\n\ |
|
470 points to.") |
2075
|
471 { |
2263
|
472 octave_value_list retval; |
2075
|
473 |
|
474 if (args.length () == 1) |
|
475 { |
|
476 string fname = oct_tilde_expand (args(0).string_value ()); |
|
477 |
|
478 if (! error_state) |
|
479 { |
|
480 file_stat fs (fname, false); |
|
481 |
2263
|
482 if (fs) |
2262
|
483 { |
|
484 retval(2) = string (); |
|
485 retval(1) = 0.0; |
|
486 retval(0) = octave_value (mk_stat_map (fs)); |
|
487 } |
|
488 else |
|
489 { |
|
490 retval(2) = fs.error (); |
|
491 retval(1) = -1.0; |
|
492 retval(0) = Matrix (); |
|
493 } |
2075
|
494 } |
|
495 } |
|
496 else |
|
497 print_usage ("lstat"); |
|
498 |
|
499 return retval; |
|
500 } |
|
501 |
|
502 DEFUN (mkfifo, args, , |
2669
|
503 "[STATUS, MSG] = mkfifo (NAME, MODE)\n\ |
2075
|
504 \n\ |
2669
|
505 Create a FIFO special file named NAME with file mode MODE\n\ |
2075
|
506 \n\ |
2669
|
507 If successful, STATUS is 0 and MSG is an empty string. Otherwise,\n\ |
|
508 STATUS is nonzero and MSG contains a system-dependent error message.") |
2075
|
509 { |
2669
|
510 octave_value_list retval; |
|
511 |
|
512 retval(1) = string (); |
|
513 retval(0) = -1.0; |
2075
|
514 |
|
515 int nargin = args.length (); |
|
516 |
|
517 if (nargin == 2) |
|
518 { |
|
519 if (args(0).is_string ()) |
|
520 { |
|
521 string name = args(0).string_value (); |
|
522 |
|
523 if (args(1).is_scalar_type ()) |
|
524 { |
2800
|
525 long mode = static_cast<long> (args(1).double_value ()); |
2075
|
526 |
2669
|
527 string msg; |
|
528 |
|
529 int status = oct_mkfifo (name, mode, msg); |
|
530 |
2800
|
531 retval(0) = static_cast<double> (status); |
2669
|
532 |
|
533 if (status < 0) |
|
534 retval(1) = msg; |
2075
|
535 } |
|
536 else |
|
537 error ("mkfifo: MODE must be an integer"); |
|
538 } |
|
539 else |
|
540 error ("mkfifo: file name must be a string"); |
|
541 |
|
542 } |
|
543 else |
|
544 print_usage ("mkfifo"); |
|
545 |
|
546 return retval; |
|
547 } |
|
548 |
|
549 DEFUN (pipe, args, , |
2669
|
550 "[FILE_IDS, STATUS, MSG] = pipe (): create an interprocess channel.\n\ |
|
551 \n\ |
|
552 Return the FILE_IDS corresponding to the reading and writing ends of\n\ |
|
553 the pipe, as a vector.\n\ |
|
554 \n\ |
|
555 If successful, STATUS is 0 and MSG is an empty string. Otherwise,\n\ |
|
556 STATUS is nonzero and MSG contains a system-dependent error message.") |
2075
|
557 { |
2669
|
558 octave_value_list retval; |
|
559 |
|
560 retval(2) = string (); |
|
561 retval(1) = -1.0; |
|
562 retval(0) = Matrix (); |
2075
|
563 |
|
564 int nargin = args.length (); |
|
565 |
|
566 if (nargin == 0) |
|
567 { |
2475
|
568 #if defined (HAVE_PIPE) |
2075
|
569 int fid[2]; |
|
570 |
2669
|
571 int status = pipe (fid); |
|
572 |
|
573 if (status < 0) |
|
574 { |
|
575 retval(2) = strerror (errno); |
|
576 } |
|
577 else |
2075
|
578 { |
|
579 FILE *in_file = fdopen (fid[0], "r"); |
|
580 FILE *out_file = fdopen (fid[1], "w"); |
|
581 |
|
582 octave_istdiostream *is |
|
583 = new octave_istdiostream (string (), in_file); |
|
584 |
|
585 octave_ostdiostream *os |
|
586 = new octave_ostdiostream (string (), out_file); |
|
587 |
|
588 Matrix file_ids (1, 2); |
|
589 |
2305
|
590 file_ids (0, 0) = octave_stream_list::insert (is); |
|
591 file_ids (0, 1) = octave_stream_list::insert (os); |
2075
|
592 |
|
593 retval(0) = file_ids; |
2800
|
594 retval(1) = static_cast<double> (status); |
2669
|
595 } |
2475
|
596 #else |
|
597 gripe_not_supported ("pipe"); |
|
598 #endif |
2075
|
599 } |
|
600 else |
|
601 print_usage ("pipe"); |
|
602 |
|
603 return retval; |
|
604 } |
|
605 |
|
606 DEFUN (stat, args, , |
2262
|
607 "[S, ERR, MSG] = stat (NAME)\n\ |
2075
|
608 \n\ |
2802
|
609 Given the name of a file, return a structure S with the following\n\ |
2075
|
610 elements:\n\ |
|
611 \n\ |
|
612 dev : id of device containing a directory entry for this file\n\ |
|
613 ino : file number of the file\n\ |
|
614 modestr : file mode, as a string of ten letters or dashes as in ls -l\n\ |
|
615 nlink : number of links\n\ |
|
616 uid : user id of file's owner\n\ |
|
617 gid : group id of file's group \n\ |
|
618 rdev : id of device for block or character special files\n\ |
|
619 size : size in bytes\n\ |
|
620 atime : time of last access\n\ |
|
621 mtime : time of last modification\n\ |
|
622 ctime : time of last file status change\n\ |
|
623 blksize : size of blocks in the file\n\ |
|
624 blocks : number of blocks allocated for file\n\ |
|
625 \n\ |
2262
|
626 If the call is successful, ERR is 0 and MSG is an empty string.\n\ |
|
627 \n\ |
|
628 If the file does not exist, or some other error occurs, S is an\n\ |
|
629 empty matrix, ERR is -1, and MSG contains the corresponding\n\ |
|
630 system error message.") |
2075
|
631 { |
2262
|
632 octave_value_list retval; |
2075
|
633 |
|
634 if (args.length () == 1) |
|
635 { |
|
636 string fname = oct_tilde_expand (args(0).string_value ()); |
|
637 |
|
638 if (! error_state) |
|
639 { |
|
640 file_stat fs (fname); |
|
641 |
|
642 if (fs) |
2262
|
643 { |
|
644 retval(2) = string (); |
|
645 retval(1) = 0.0; |
|
646 retval(0) = octave_value (mk_stat_map (fs)); |
|
647 } |
|
648 else |
|
649 { |
|
650 retval(2) = fs.error (); |
|
651 retval(1) = -1.0; |
|
652 retval(0) = Matrix (); |
|
653 } |
2075
|
654 } |
|
655 } |
|
656 else |
|
657 print_usage ("stat"); |
|
658 |
|
659 return retval; |
|
660 } |
|
661 |
|
662 DEFUN (unlink, args, , |
2669
|
663 "[STATUS, MSG] = unlink (NAME)\n\ |
2075
|
664 \n\ |
2669
|
665 Delete the file NAME\n\ |
2075
|
666 \n\ |
2669
|
667 If successful, STATUS is 0 and MSG is an empty string. Otherwise,\n\ |
|
668 STATUS is nonzero and MSG contains a system-dependent error message.") |
2075
|
669 { |
2669
|
670 octave_value_list retval; |
|
671 |
|
672 retval(1) = string (); |
|
673 retval(0) = -1.0; |
2075
|
674 |
|
675 int nargin = args.length (); |
|
676 |
|
677 if (nargin == 1) |
|
678 { |
|
679 if (args(0).is_string ()) |
|
680 { |
|
681 string name = args(0).string_value (); |
|
682 |
2669
|
683 string msg; |
|
684 |
|
685 int status = oct_unlink (name, msg); |
|
686 |
2800
|
687 retval(0) = static_cast<double> (status); |
2669
|
688 |
|
689 if (status < 0) |
|
690 retval(1) = msg; |
2075
|
691 } |
|
692 else |
|
693 error ("unlink: file name must be a string"); |
|
694 } |
|
695 else |
|
696 print_usage ("unlink"); |
|
697 |
|
698 return retval; |
|
699 } |
|
700 |
|
701 DEFUN (waitpid, args, , |
2669
|
702 "[PID, MSG] = waitpid (PID, OPTIONS)\n\ |
2075
|
703 \n\ |
2669
|
704 Wait for process PID to terminate\n\ |
2075
|
705 \n\ |
|
706 PID can be:\n\ |
|
707 \n\ |
|
708 -1 : wait for any child process\n\ |
|
709 0 : wait for any child process whose process group ID is equal to\n\ |
|
710 that of the Octave interpreter process.\n\ |
|
711 > 0 : wait for termination of the child process with ID PID.\n\ |
|
712 \n\ |
|
713 OPTIONS is:\n\ |
|
714 \n\ |
|
715 0 : wait until signal is received or a child process exits (this\n\ |
|
716 is the default if the OPTIONS argument is missing) \n\ |
|
717 1 : do not hang if status is not immediately available\n\ |
|
718 2 : report the status of any child processes that are\n\ |
|
719 stopped, and whose status has not yet been reported\n\ |
|
720 since they stopped\n\ |
|
721 3 : implies both 1 and 2\n\ |
|
722 \n\ |
2669
|
723 If successful, PID is greater than 0 and contains the process ID of\n\ |
|
724 the child process that exited and MSG is an empty string.\n\ |
|
725 Otherwise, PID is less than zero and MSG contains a system-dependent\n\ |
|
726 error message.") |
2075
|
727 { |
2669
|
728 octave_value_list retval; |
|
729 |
|
730 retval(1) = string (); |
|
731 retval(0) = -1.0; |
2075
|
732 |
|
733 int nargin = args.length (); |
|
734 |
|
735 if (nargin == 1 || nargin == 2) |
|
736 { |
2475
|
737 #if defined (HAVE_WAITPID) |
2075
|
738 double pid_num = args(0).double_value (); |
|
739 |
|
740 if (! error_state) |
|
741 { |
|
742 if (D_NINT (pid_num) != pid_num) |
|
743 error ("waitpid: PID must be an integer value"); |
|
744 else |
|
745 { |
|
746 pid_t pid = (pid_t) pid_num; |
|
747 |
|
748 int options = 0; |
|
749 |
|
750 if (args.length () == 2) |
|
751 { |
|
752 double options_num = args(1).double_value (); |
|
753 |
|
754 if (! error_state) |
|
755 { |
|
756 if (D_NINT (options_num) != options_num) |
|
757 error ("waitpid: PID must be an integer value"); |
|
758 else |
|
759 { |
|
760 options = NINT (options_num); |
|
761 if (options < 0 || options > 3) |
|
762 error ("waitpid: invalid OPTIONS value specified"); |
|
763 } |
|
764 } |
|
765 } |
|
766 |
|
767 if (! error_state) |
2669
|
768 { |
|
769 pid_t status = waitpid (pid, 0, options); |
|
770 |
2800
|
771 retval(0) = static_cast<double> (status); |
2669
|
772 |
|
773 if (status < 0) |
|
774 retval(1) = strerror (errno); |
|
775 } |
2075
|
776 } |
|
777 } |
2475
|
778 #else |
|
779 gripe_not_supported ("waitpid"); |
|
780 #endif |
2075
|
781 } |
|
782 else |
|
783 print_usage ("waitpid"); |
|
784 |
|
785 return retval; |
|
786 } |
|
787 |
|
788 #if !defined (O_NONBLOCK) && defined (O_NDELAY) |
|
789 #define O_NONBLOCK O_NDELAY |
|
790 #endif |
|
791 |
|
792 void |
|
793 symbols_of_syscalls (void) |
|
794 { |
|
795 #if defined (F_DUPFD) |
2800
|
796 DEFCONST (F_DUPFD, static_cast<double> (F_DUPFD), 0, 0, |
2075
|
797 ""); |
|
798 #endif |
|
799 |
|
800 #if defined (F_GETFD) |
2800
|
801 DEFCONST (F_GETFD, static_cast<double> (F_GETFD), 0, 0, |
2075
|
802 ""); |
|
803 #endif |
|
804 |
|
805 #if defined (F_GETFL) |
2800
|
806 DEFCONST (F_GETFL, static_cast<double> (F_GETFL), 0, 0, |
2075
|
807 ""); |
|
808 #endif |
|
809 |
|
810 #if defined (F_SETFD) |
2800
|
811 DEFCONST (F_SETFD, static_cast<double> (F_SETFD), 0, 0, |
2075
|
812 ""); |
|
813 #endif |
|
814 |
|
815 #if defined (F_SETFL) |
2800
|
816 DEFCONST (F_SETFL, static_cast<double> (F_SETFL), 0, 0, |
2075
|
817 ""); |
|
818 #endif |
|
819 |
|
820 #if defined (O_APPEND) |
2800
|
821 DEFCONST (O_APPEND, static_cast<double> (O_APPEND), 0, 0, |
2075
|
822 ""); |
|
823 #endif |
|
824 |
2669
|
825 #if defined (O_ASYNC) |
2800
|
826 DEFCONST (O_ASYNC, static_cast<double> (O_ASYNC), 0, 0, |
2669
|
827 ""); |
|
828 #endif |
|
829 |
2075
|
830 #if defined (O_CREAT) |
2800
|
831 DEFCONST (O_CREAT, static_cast<double> (O_CREAT), 0, 0, |
2075
|
832 ""); |
|
833 #endif |
|
834 |
|
835 #if defined (O_EXCL) |
2800
|
836 DEFCONST (O_EXCL, static_cast<double> (O_EXCL), 0, 0, |
2075
|
837 ""); |
|
838 #endif |
|
839 |
|
840 #if defined (O_NONBLOCK) |
2800
|
841 DEFCONST (O_NONBLOCK, static_cast<double> (O_NONBLOCK), 0, 0, |
2075
|
842 ""); |
|
843 #endif |
|
844 |
|
845 #if defined (O_RDONLY) |
2800
|
846 DEFCONST (O_RDONLY, static_cast<double> (O_RDONLY), 0, 0, |
2075
|
847 ""); |
|
848 #endif |
|
849 |
|
850 #if defined (O_RDWR) |
2800
|
851 DEFCONST (O_RDWR, static_cast<double> (O_RDWR), 0, 0, |
2075
|
852 ""); |
|
853 #endif |
|
854 |
2669
|
855 #if defined (O_SYNC) |
2800
|
856 DEFCONST (O_SYNC, static_cast<double> (O_SYNC), 0, 0, |
2669
|
857 ""); |
|
858 #endif |
|
859 |
2075
|
860 #if defined (O_TRUNC) |
2800
|
861 DEFCONST (O_TRUNC, static_cast<double> (O_TRUNC), 0, 0, |
2075
|
862 ""); |
|
863 #endif |
|
864 |
|
865 #if defined (O_WRONLY) |
2800
|
866 DEFCONST (O_WRONLY, static_cast<double> (O_WRONLY), 0, 0, |
2075
|
867 ""); |
|
868 #endif |
|
869 } |
|
870 |
|
871 /* |
|
872 ;;; Local Variables: *** |
|
873 ;;; mode: C++ *** |
|
874 ;;; End: *** |
|
875 */ |