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