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