2075
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 John W. Eaton |
|
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 |
|
32 #include <cstdio> |
|
33 |
|
34 #ifdef HAVE_UNISTD_H |
|
35 #include <sys/types.h> |
|
36 #include <unistd.h> |
|
37 #endif |
|
38 |
|
39 #ifdef HAVE_FCNTL_H |
|
40 #include <fcntl.h> |
|
41 #endif |
|
42 |
|
43 #include "defun.h" |
|
44 #include "error.h" |
|
45 #include "file-ops.h" |
2078
|
46 #include "gripes.h" |
2075
|
47 #include "help.h" |
|
48 #include "lo-utils.h" |
|
49 #include "oct-map.h" |
|
50 #include "oct-obj.h" |
|
51 #include "oct-stdstrm.h" |
|
52 #include "oct-stream.h" |
|
53 #include "sysdep.h" |
|
54 #include "syswait.h" |
|
55 #include "utils.h" |
2366
|
56 #include "variables.h" |
2075
|
57 |
|
58 static Octave_map |
|
59 mk_stat_map (const file_stat& fs) |
|
60 { |
|
61 Octave_map m; |
|
62 |
|
63 m["dev"] = (double) fs.dev (); |
|
64 m["ino"] = (double) fs.ino (); |
|
65 m["modestr"] = fs.mode_as_string (); |
|
66 m["nlink"] = (double) fs.nlink (); |
|
67 m["uid"] = (double) fs.uid (); |
|
68 m["gid"] = (double) fs.gid (); |
|
69 #if defined (HAVE_ST_RDEV) |
|
70 m["rdev"] = (double) fs.rdev (); |
|
71 #endif |
|
72 m["size"] = (double) fs.size (); |
|
73 m["atime"] = (double) fs.atime (); |
|
74 m["mtime"] = (double) fs.mtime (); |
|
75 m["ctime"] = (double) fs.ctime (); |
|
76 #if defined (HAVE_ST_BLKSIZE) |
|
77 m["blksize"] = (double) fs.blksize (); |
|
78 #endif |
|
79 #if defined (HAVE_ST_BLOCKS) |
|
80 m["blocks"] = (double) fs.blocks (); |
|
81 #endif |
|
82 |
|
83 return m; |
|
84 } |
|
85 |
|
86 DEFUN(dup2, args, , |
2080
|
87 "fid = dup2 (old, new): duplicate a file descriptor") |
2075
|
88 { |
|
89 double retval = -1.0; |
|
90 |
|
91 #if defined (HAVE_DUP2) |
|
92 int nargin = args.length (); |
|
93 |
|
94 if (nargin == 2) |
|
95 { |
|
96 double d_old = args(0).double_value (); |
|
97 double d_new = args(1).double_value (); |
|
98 |
|
99 if (! error_state) |
|
100 { |
|
101 if (D_NINT (d_old) == d_old && D_NINT (d_new) == d_new) |
|
102 { |
|
103 int i_old = NINT (d_old); |
|
104 int i_new = NINT (d_new); |
|
105 |
|
106 // XXX FIXME XXX -- are these checks sufficient? |
|
107 if (i_old >= 0 && i_new >= 0) |
|
108 retval = (double) dup2 (i_old, i_new); |
|
109 else |
|
110 error ("dup2: invalid file id"); |
|
111 } |
|
112 else |
|
113 error ("dup2: arguments must be integer values"); |
|
114 } |
|
115 } |
|
116 else |
|
117 print_usage ("dup2"); |
|
118 #else |
|
119 gripe_not_supported ("dup2"); |
|
120 #endif |
|
121 |
|
122 return retval; |
|
123 } |
|
124 |
|
125 DEFUN(exec, args, , |
2080
|
126 "exec (file, args): replace current process with a new process") |
2075
|
127 { |
|
128 double retval = -1.0; |
|
129 |
|
130 #if defined (HAVE_EXECVP) |
|
131 int nargin = args.length (); |
|
132 |
|
133 if (nargin == 1 || nargin == 2) |
|
134 { |
|
135 string exec_file = args(0).string_value (); |
|
136 |
|
137 if (! error_state) |
|
138 { |
|
139 char **exec_args = 0; |
|
140 |
|
141 if (nargin == 2) |
|
142 { |
|
143 charMatrix chm = args(1).all_strings (); |
|
144 |
|
145 if (! error_state) |
|
146 { |
|
147 int nr = chm.rows (); |
|
148 int nc = chm.cols (); |
|
149 |
|
150 exec_args = new char * [nr+2]; |
|
151 |
|
152 // XXX FIXME XXX -- potential leak? |
|
153 |
|
154 exec_args[0] = strsave (exec_file.c_str ()); |
|
155 exec_args[nr+1] = 0; |
|
156 |
|
157 for (int i = 0; i < nr; i++) |
|
158 { |
|
159 exec_args[i+1] = new char [nc+1]; |
|
160 |
|
161 for (int j = 0; j < nc; j++) |
2305
|
162 exec_args[i+1][j] = chm (i, j); |
2075
|
163 |
|
164 exec_args[i+1][nc] = '\0'; |
|
165 } |
|
166 } |
|
167 else |
|
168 error ("exec: arguments must be strings"); |
|
169 } |
|
170 else |
|
171 { |
|
172 exec_args = new char * [2]; |
|
173 |
|
174 exec_args[0] = strsave (exec_file.c_str ()); |
|
175 exec_args[1] = 0; |
|
176 } |
|
177 |
|
178 if (! error_state) |
|
179 execvp (exec_file.c_str (), exec_args); |
|
180 } |
|
181 else |
|
182 error ("exec: first argument must be a string"); |
|
183 } |
|
184 else |
|
185 print_usage ("exec"); |
|
186 #else |
|
187 gripe_not_supported ("exec"); |
|
188 #endif |
|
189 |
|
190 return retval; |
|
191 } |
|
192 |
|
193 DEFUN(fcntl, args, , |
2080
|
194 "fcntl (fid, request, argument): control open file descriptors") |
2075
|
195 { |
|
196 double retval = -1.0; |
|
197 |
|
198 #if defined (HAVE_FCNTL) |
|
199 int nargin = args.length (); |
|
200 |
|
201 if (nargin == 3) |
|
202 { |
|
203 double d_fid = args(0).double_value (); |
|
204 double d_req = args(1).double_value (); |
|
205 double d_arg = args(2).double_value (); |
|
206 |
|
207 if (! error_state |
|
208 && D_NINT (d_fid) == d_fid |
|
209 && D_NINT (d_req) == d_req |
|
210 && D_NINT (d_arg) == d_arg) |
|
211 { |
|
212 int fid = NINT (d_fid); |
|
213 int req = NINT (d_req); |
|
214 int arg = NINT (d_arg); |
|
215 |
|
216 // XXX FIXME XXX -- Need better checking here? |
|
217 if (fid < 0) |
|
218 error ("fcntl: invalid file id"); |
|
219 else |
|
220 retval = fcntl (fid, req, arg); |
|
221 } |
|
222 else |
|
223 error ("fcntl: file id must be an integer"); |
|
224 } |
|
225 else |
|
226 print_usage ("fcntl"); |
|
227 #else |
|
228 gripe_not_supported ("fcntl"); |
|
229 #endif |
|
230 |
|
231 return retval; |
|
232 } |
|
233 |
|
234 DEFUN(fork, args, , |
|
235 "fork (): create a copy of the current process") |
|
236 { |
|
237 double retval = -1.0; |
|
238 |
|
239 #if defined (HAVE_FORK) |
|
240 int nargin = args.length (); |
|
241 |
|
242 if (nargin == 0) |
|
243 retval = fork (); |
|
244 else |
|
245 print_usage ("fork"); |
|
246 #else |
|
247 gripe_not_supported ("fork"); |
|
248 #endif |
|
249 |
|
250 return retval; |
|
251 } |
|
252 |
|
253 DEFUN(getpgrp, args, , |
|
254 "pgid = getpgrp (): return the process group id of the current process") |
|
255 { |
|
256 double retval = -1.0; |
|
257 |
|
258 #if defined (HAVE_GETPGRP) |
|
259 int nargin = args.length (); |
|
260 |
|
261 if (nargin == 0) |
|
262 retval = getpgrp (); |
|
263 else |
|
264 print_usage ("getpgrp"); |
|
265 #else |
|
266 gripe_not_supported ("getpgrp"); |
|
267 #endif |
|
268 |
|
269 return retval; |
|
270 } |
|
271 |
|
272 DEFUN(getpid, args, , |
|
273 "pid = getpid (): return the process id of the current process") |
|
274 { |
|
275 double retval = -1.0; |
|
276 |
|
277 #if defined (HAVE_GETPID) |
|
278 int nargin = args.length (); |
|
279 |
|
280 if (nargin == 0) |
|
281 retval = getpid (); |
|
282 else |
|
283 print_usage ("getpid"); |
|
284 #else |
|
285 gripe_not_supported ("getpid"); |
|
286 #endif |
|
287 |
|
288 return retval; |
|
289 } |
|
290 |
|
291 DEFUN(getppid, args, , |
|
292 "pid = getppid (): return the process id of the parent process") |
|
293 { |
|
294 double retval = -1.0; |
|
295 |
|
296 #if defined (HAVE_GETPPID) |
|
297 int nargin = args.length (); |
|
298 |
|
299 if (nargin == 0) |
|
300 retval = getppid (); |
|
301 else |
|
302 print_usage ("getppid"); |
|
303 #else |
|
304 gripe_not_supported ("getppid"); |
|
305 #endif |
|
306 |
|
307 return retval; |
|
308 } |
|
309 |
|
310 DEFUN (lstat, args, , |
2262
|
311 "[S, ERR, MSG] = lstat (NAME)\n\ |
2075
|
312 \n\ |
2262
|
313 Like [S, ERR, MSG] = stat (NAME), but if NAME refers to a symbolic\n\ |
|
314 link, returns information about the link itself, not the file that it\n\ |
|
315 points to.") |
2075
|
316 { |
2263
|
317 octave_value_list retval; |
2075
|
318 |
|
319 if (args.length () == 1) |
|
320 { |
|
321 string fname = oct_tilde_expand (args(0).string_value ()); |
|
322 |
|
323 if (! error_state) |
|
324 { |
|
325 file_stat fs (fname, false); |
|
326 |
2263
|
327 if (fs) |
2262
|
328 { |
|
329 retval(2) = string (); |
|
330 retval(1) = 0.0; |
|
331 retval(0) = octave_value (mk_stat_map (fs)); |
|
332 } |
|
333 else |
|
334 { |
|
335 retval(2) = fs.error (); |
|
336 retval(1) = -1.0; |
|
337 retval(0) = Matrix (); |
|
338 } |
2075
|
339 } |
|
340 } |
|
341 else |
|
342 print_usage ("lstat"); |
|
343 |
|
344 return retval; |
|
345 } |
|
346 |
|
347 DEFUN (mkfifo, args, , |
|
348 "STATUS = mkfifo (NAME, MODE)\n\ |
|
349 \n\ |
|
350 Create a FIFO special file named NAME with file mode MODE\n\ |
|
351 \n\ |
|
352 STATUS is:\n\ |
|
353 \n\ |
|
354 != 0 : if mkfifo failed\n\ |
|
355 0 : if the FIFO special file could be created") |
|
356 { |
|
357 double retval = -1.0; |
|
358 |
|
359 int nargin = args.length (); |
|
360 |
|
361 if (nargin == 2) |
|
362 { |
|
363 if (args(0).is_string ()) |
|
364 { |
|
365 string name = args(0).string_value (); |
|
366 |
|
367 if (args(1).is_scalar_type ()) |
|
368 { |
|
369 long mode = (long) args(1).double_value (); |
|
370 |
|
371 retval = oct_mkfifo (name, mode); |
|
372 } |
|
373 else |
|
374 error ("mkfifo: MODE must be an integer"); |
|
375 } |
|
376 else |
|
377 error ("mkfifo: file name must be a string"); |
|
378 |
|
379 } |
|
380 else |
|
381 print_usage ("mkfifo"); |
|
382 |
|
383 return retval; |
|
384 } |
|
385 |
|
386 DEFUN (pipe, args, , |
2080
|
387 "[file_ids, status] = pipe (): create an interprocess channel") |
2075
|
388 { |
2086
|
389 octave_value_list retval (2, octave_value (-1.0)); |
2075
|
390 |
|
391 #if defined (HAVE_PIPE) |
|
392 int nargin = args.length (); |
|
393 |
|
394 if (nargin == 0) |
|
395 { |
|
396 int fid[2]; |
|
397 |
|
398 if (pipe (fid) >= 0) |
|
399 { |
|
400 FILE *in_file = fdopen (fid[0], "r"); |
|
401 FILE *out_file = fdopen (fid[1], "w"); |
|
402 |
|
403 octave_istdiostream *is |
|
404 = new octave_istdiostream (string (), in_file); |
|
405 |
|
406 octave_ostdiostream *os |
|
407 = new octave_ostdiostream (string (), out_file); |
|
408 |
|
409 Matrix file_ids (1, 2); |
|
410 |
2305
|
411 file_ids (0, 0) = octave_stream_list::insert (is); |
|
412 file_ids (0, 1) = octave_stream_list::insert (os); |
2075
|
413 |
|
414 retval(0) = file_ids; |
|
415 retval(1) = 0.0; |
|
416 } |
|
417 } |
|
418 else |
|
419 print_usage ("pipe"); |
|
420 #else |
|
421 gripe_not_supported ("pipe"); |
|
422 #endif |
|
423 |
|
424 return retval; |
|
425 } |
|
426 |
|
427 DEFUN (stat, args, , |
2262
|
428 "[S, ERR, MSG] = stat (NAME)\n\ |
2075
|
429 \n\ |
2262
|
430 Given the name of a file, return a structure S with the following |
2075
|
431 elements:\n\ |
|
432 \n\ |
|
433 dev : id of device containing a directory entry for this file\n\ |
|
434 ino : file number of the file\n\ |
|
435 modestr : file mode, as a string of ten letters or dashes as in ls -l\n\ |
|
436 nlink : number of links\n\ |
|
437 uid : user id of file's owner\n\ |
|
438 gid : group id of file's group \n\ |
|
439 rdev : id of device for block or character special files\n\ |
|
440 size : size in bytes\n\ |
|
441 atime : time of last access\n\ |
|
442 mtime : time of last modification\n\ |
|
443 ctime : time of last file status change\n\ |
|
444 blksize : size of blocks in the file\n\ |
|
445 blocks : number of blocks allocated for file\n\ |
|
446 \n\ |
2262
|
447 If the call is successful, ERR is 0 and MSG is an empty string.\n\ |
|
448 \n\ |
|
449 If the file does not exist, or some other error occurs, S is an\n\ |
|
450 empty matrix, ERR is -1, and MSG contains the corresponding\n\ |
|
451 system error message.") |
2075
|
452 { |
2262
|
453 octave_value_list retval; |
2075
|
454 |
|
455 if (args.length () == 1) |
|
456 { |
|
457 string fname = oct_tilde_expand (args(0).string_value ()); |
|
458 |
|
459 if (! error_state) |
|
460 { |
|
461 file_stat fs (fname); |
|
462 |
|
463 if (fs) |
2262
|
464 { |
|
465 retval(2) = string (); |
|
466 retval(1) = 0.0; |
|
467 retval(0) = octave_value (mk_stat_map (fs)); |
|
468 } |
|
469 else |
|
470 { |
|
471 retval(2) = fs.error (); |
|
472 retval(1) = -1.0; |
|
473 retval(0) = Matrix (); |
|
474 } |
2075
|
475 } |
|
476 } |
|
477 else |
|
478 print_usage ("stat"); |
|
479 |
|
480 return retval; |
|
481 } |
|
482 |
|
483 DEFUN (unlink, args, , |
|
484 "STATUS = unlink (NAME)\n\ |
|
485 \n\ |
|
486 Delete the file NAME\n\ |
|
487 \n\ |
|
488 STATUS is:\n\ |
|
489 \n\ |
|
490 != 0 : if unlink failed\n\ |
|
491 0 : if the file could be successfully deleted") |
|
492 { |
|
493 double retval = -1.0; |
|
494 |
|
495 int nargin = args.length (); |
|
496 |
|
497 if (nargin == 1) |
|
498 { |
|
499 if (args(0).is_string ()) |
|
500 { |
|
501 string name = args(0).string_value (); |
|
502 |
|
503 retval = oct_unlink (name); |
|
504 } |
|
505 else |
|
506 error ("unlink: file name must be a string"); |
|
507 } |
|
508 else |
|
509 print_usage ("unlink"); |
|
510 |
|
511 return retval; |
|
512 } |
|
513 |
|
514 DEFUN (waitpid, args, , |
|
515 "STATUS = waitpid (PID, OPTIONS)\n\ |
|
516 \n\ |
|
517 wait for process PID to terminate\n\ |
|
518 \n\ |
|
519 PID can be:\n\ |
|
520 \n\ |
|
521 -1 : wait for any child process\n\ |
|
522 0 : wait for any child process whose process group ID is equal to\n\ |
|
523 that of the Octave interpreter process.\n\ |
|
524 > 0 : wait for termination of the child process with ID PID.\n\ |
|
525 \n\ |
|
526 OPTIONS is:\n\ |
|
527 \n\ |
|
528 0 : wait until signal is received or a child process exits (this\n\ |
|
529 is the default if the OPTIONS argument is missing) \n\ |
|
530 1 : do not hang if status is not immediately available\n\ |
|
531 2 : report the status of any child processes that are\n\ |
|
532 stopped, and whose status has not yet been reported\n\ |
|
533 since they stopped\n\ |
|
534 3 : implies both 1 and 2\n\ |
|
535 \n\ |
|
536 STATUS is:\n\ |
|
537 \n\ |
|
538 -1 : if an error occured\n\ |
|
539 > 0 : the process ID of the child process that exited") |
|
540 { |
|
541 double retval = -1.0; |
|
542 |
|
543 #if defined (HAVE_WAITPID) |
|
544 int nargin = args.length (); |
|
545 |
|
546 if (nargin == 1 || nargin == 2) |
|
547 { |
|
548 double pid_num = args(0).double_value (); |
|
549 |
|
550 if (! error_state) |
|
551 { |
|
552 if (D_NINT (pid_num) != pid_num) |
|
553 error ("waitpid: PID must be an integer value"); |
|
554 else |
|
555 { |
|
556 pid_t pid = (pid_t) pid_num; |
|
557 |
|
558 int options = 0; |
|
559 |
|
560 if (args.length () == 2) |
|
561 { |
|
562 double options_num = args(1).double_value (); |
|
563 |
|
564 if (! error_state) |
|
565 { |
|
566 if (D_NINT (options_num) != options_num) |
|
567 error ("waitpid: PID must be an integer value"); |
|
568 else |
|
569 { |
|
570 options = NINT (options_num); |
|
571 if (options < 0 || options > 3) |
|
572 error ("waitpid: invalid OPTIONS value specified"); |
|
573 } |
|
574 } |
|
575 } |
|
576 |
|
577 if (! error_state) |
|
578 retval = waitpid (pid, 0, options); |
|
579 } |
|
580 } |
|
581 } |
|
582 else |
|
583 print_usage ("waitpid"); |
|
584 #else |
|
585 gripe_not_supported ("waitpid"); |
|
586 #endif |
|
587 |
|
588 return retval; |
|
589 } |
|
590 |
|
591 #if !defined (O_NONBLOCK) && defined (O_NDELAY) |
|
592 #define O_NONBLOCK O_NDELAY |
|
593 #endif |
|
594 |
|
595 void |
|
596 symbols_of_syscalls (void) |
|
597 { |
|
598 #if defined (F_DUPFD) |
|
599 DEFCONST (F_DUPFD, (double) F_DUPFD, 0, 0, |
|
600 ""); |
|
601 #endif |
|
602 |
|
603 #if defined (F_GETFD) |
|
604 DEFCONST (F_GETFD, (double) F_GETFD, 0, 0, |
|
605 ""); |
|
606 #endif |
|
607 |
|
608 #if defined (F_GETFL) |
|
609 DEFCONST (F_GETFL, (double) F_GETFL, 0, 0, |
|
610 ""); |
|
611 #endif |
|
612 |
|
613 #if defined (F_SETFD) |
|
614 DEFCONST (F_SETFD, (double) F_SETFD, 0, 0, |
|
615 ""); |
|
616 #endif |
|
617 |
|
618 #if defined (F_SETFL) |
|
619 DEFCONST (F_SETFL, (double) F_SETFL, 0, 0, |
|
620 ""); |
|
621 #endif |
|
622 |
|
623 #if defined (O_APPEND) |
|
624 DEFCONST (O_APPEND, (double) O_APPEND, 0, 0, |
|
625 ""); |
|
626 #endif |
|
627 |
|
628 #if defined (O_CREAT) |
|
629 DEFCONST (O_CREAT, (double) O_CREAT, 0, 0, |
|
630 ""); |
|
631 #endif |
|
632 |
|
633 #if defined (O_EXCL) |
|
634 DEFCONST (O_EXCL, (double) O_EXCL, 0, 0, |
|
635 ""); |
|
636 #endif |
|
637 |
|
638 #if defined (O_NONBLOCK) |
|
639 DEFCONST (O_NONBLOCK, (double) O_NONBLOCK, 0, 0, |
|
640 ""); |
|
641 #endif |
|
642 |
|
643 #if defined (O_RDONLY) |
|
644 DEFCONST (O_RDONLY, (double) O_RDONLY, 0, 0, |
|
645 ""); |
|
646 #endif |
|
647 |
|
648 #if defined (O_RDWR) |
|
649 DEFCONST (O_RDWR, (double) O_RDWR, 0, 0, |
|
650 ""); |
|
651 #endif |
|
652 |
|
653 #if defined (O_TRUNC) |
|
654 DEFCONST (O_TRUNC, (double) O_TRUNC, 0, 0, |
|
655 ""); |
|
656 #endif |
|
657 |
|
658 #if defined (O_WRONLY) |
|
659 DEFCONST (O_WRONLY, (double) O_WRONLY, 0, 0, |
|
660 ""); |
|
661 #endif |
|
662 } |
|
663 |
|
664 /* |
|
665 ;;; Local Variables: *** |
|
666 ;;; mode: C++ *** |
|
667 ;;; End: *** |
|
668 */ |