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