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 |
|
32 #include <cstdio> |
2669
|
33 #include <cstring> |
2075
|
34 |
|
35 #ifdef HAVE_UNISTD_H |
2442
|
36 #ifdef HAVE_SYS_TYPES_H |
2075
|
37 #include <sys/types.h> |
2442
|
38 #endif |
2075
|
39 #include <unistd.h> |
|
40 #endif |
|
41 |
|
42 #ifdef HAVE_FCNTL_H |
|
43 #include <fcntl.h> |
|
44 #endif |
|
45 |
2926
|
46 #include "file-ops.h" |
|
47 #include "file-stat.h" |
2937
|
48 #include "oct-syscalls.h" |
2926
|
49 |
2075
|
50 #include "defun.h" |
|
51 #include "error.h" |
2078
|
52 #include "gripes.h" |
2075
|
53 #include "lo-utils.h" |
|
54 #include "oct-map.h" |
|
55 #include "oct-obj.h" |
|
56 #include "oct-stdstrm.h" |
|
57 #include "oct-stream.h" |
|
58 #include "sysdep.h" |
|
59 #include "syswait.h" |
|
60 #include "utils.h" |
2366
|
61 #include "variables.h" |
2075
|
62 |
|
63 static Octave_map |
|
64 mk_stat_map (const file_stat& fs) |
|
65 { |
|
66 Octave_map m; |
|
67 |
4233
|
68 m["dev"](0) = static_cast<double> (fs.dev ()); |
|
69 m["ino"](0) = static_cast<double> (fs.ino ()); |
|
70 m["modestr"](0) = fs.mode_as_string (); |
|
71 m["nlink"](0) = static_cast<double> (fs.nlink ()); |
|
72 m["uid"](0) = static_cast<double> (fs.uid ()); |
|
73 m["gid"](0) = static_cast<double> (fs.gid ()); |
3887
|
74 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
4233
|
75 m["rdev"](0) = static_cast<double> (fs.rdev ()); |
2075
|
76 #endif |
4233
|
77 m["size"](0) = static_cast<double> (fs.size ()); |
|
78 m["atime"](0) = static_cast<double> (fs.atime ()); |
|
79 m["mtime"](0) = static_cast<double> (fs.mtime ()); |
|
80 m["ctime"](0) = static_cast<double> (fs.ctime ()); |
3887
|
81 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
4233
|
82 m["blksize"](0) = static_cast<double> (fs.blksize ()); |
2075
|
83 #endif |
3887
|
84 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
4233
|
85 m["blocks"](0) = static_cast<double> (fs.blocks ()); |
2075
|
86 #endif |
|
87 |
|
88 return m; |
|
89 } |
|
90 |
2457
|
91 DEFUN (dup2, args, , |
3301
|
92 "-*- texinfo -*-\n\ |
|
93 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} dup2 (@var{old}, @var{new})\n\ |
2669
|
94 Duplicate a file descriptor.\n\ |
|
95 \n\ |
3301
|
96 If successful, @var{fid} is greater than zero and contains the new file\n\ |
|
97 ID. Otherwise, @var{fid} is negative and @var{msg} contains a\n\ |
|
98 system-dependent error message.\n\ |
|
99 @end deftypefn") |
2075
|
100 { |
2669
|
101 octave_value_list retval; |
|
102 |
3523
|
103 retval(1) = std::string (); |
2669
|
104 retval(0) = -1.0; |
2075
|
105 |
|
106 int nargin = args.length (); |
|
107 |
|
108 if (nargin == 2) |
|
109 { |
3341
|
110 octave_stream old_stream |
|
111 = octave_stream_list::lookup (args(0), "dup2"); |
2075
|
112 |
3341
|
113 if (! error_state) |
2075
|
114 { |
3341
|
115 octave_stream new_stream |
|
116 = octave_stream_list::lookup (args(1), "dup2"); |
2075
|
117 |
3341
|
118 if (! error_state) |
3145
|
119 { |
3341
|
120 int i_old = old_stream.file_number (); |
|
121 int i_new = new_stream.file_number (); |
2937
|
122 |
3341
|
123 if (i_old >= 0 && i_new >= 0) |
|
124 { |
3523
|
125 std::string msg; |
2669
|
126 |
3341
|
127 int status = octave_syscalls::dup2 (i_old, i_new, msg); |
|
128 |
4233
|
129 retval(0) = status; |
3341
|
130 retval(1) = msg; |
|
131 } |
2075
|
132 } |
|
133 } |
3145
|
134 else |
|
135 error ("dup2: invalid stream"); |
2075
|
136 } |
|
137 else |
|
138 print_usage ("dup2"); |
|
139 |
|
140 return retval; |
|
141 } |
|
142 |
2457
|
143 DEFUN (exec, args, , |
3301
|
144 "-*- texinfo -*-\n\ |
|
145 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} exec (@var{file}, @var{args})\n\ |
|
146 Replace current process with a new process. Calling @code{exec} without\n\ |
|
147 first calling @code{fork} will terminate your current Octave process and\n\ |
|
148 replace it with the program named by @var{file}. For example,\n\ |
2669
|
149 \n\ |
3301
|
150 @example\n\ |
|
151 exec (\"ls\" \"-l\")\n\ |
|
152 @end example\n\ |
2669
|
153 \n\ |
3301
|
154 @noindent\n\ |
|
155 will run @code{ls} and return you to your shell prompt.\n\ |
|
156 \n\ |
|
157 If successful, @code{exec} does not return. If @code{exec} does return,\n\ |
|
158 @var{err} will be nonzero, and @var{msg} will contain a system-dependent\n\ |
|
159 error message.\n\ |
|
160 @end deftypefn") |
2075
|
161 { |
2669
|
162 octave_value_list retval; |
|
163 |
3523
|
164 retval(1) = std::string (); |
2669
|
165 retval(0) = -1.0; |
2075
|
166 |
|
167 int nargin = args.length (); |
|
168 |
|
169 if (nargin == 1 || nargin == 2) |
|
170 { |
3523
|
171 std::string exec_file = args(0).string_value (); |
2075
|
172 |
|
173 if (! error_state) |
|
174 { |
2937
|
175 string_vector exec_args; |
2075
|
176 |
|
177 if (nargin == 2) |
|
178 { |
2937
|
179 string_vector tmp = args(1).all_strings (); |
2075
|
180 |
|
181 if (! error_state) |
|
182 { |
2937
|
183 int len = tmp.length (); |
2075
|
184 |
2937
|
185 exec_args.resize (len + 1); |
2075
|
186 |
2937
|
187 exec_args[0] = exec_file; |
2075
|
188 |
2937
|
189 for (int i = 0; i < len; i++) |
|
190 exec_args[i+1] = tmp[i]; |
2075
|
191 } |
|
192 else |
|
193 error ("exec: arguments must be strings"); |
|
194 } |
|
195 else |
|
196 { |
2937
|
197 exec_args.resize (1); |
2075
|
198 |
2937
|
199 exec_args[0] = exec_file; |
2075
|
200 } |
|
201 |
|
202 if (! error_state) |
2669
|
203 { |
3523
|
204 std::string msg; |
2937
|
205 |
|
206 int status = octave_syscalls::execvp (exec_file, exec_args, msg); |
2669
|
207 |
4233
|
208 retval(0) = status; |
2937
|
209 retval(1) = msg; |
2669
|
210 } |
2075
|
211 } |
|
212 else |
|
213 error ("exec: first argument must be a string"); |
|
214 } |
|
215 else |
|
216 print_usage ("exec"); |
|
217 |
|
218 return retval; |
|
219 } |
|
220 |
2457
|
221 DEFUN (fcntl, args, , |
3301
|
222 "-*- texinfo -*-\n\ |
|
223 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} fcntl (@var{fid}, @var{request}, @var{arg})\n\ |
|
224 Change the properties of the open file @var{fid}. The following values\n\ |
|
225 may be passed as @var{request}:\n\ |
|
226 \n\ |
|
227 @vtable @code\n\ |
|
228 @item F_DUPFD\n\ |
|
229 Return a duplicate file descriptor.\n\ |
|
230 \n\ |
|
231 @item F_GETFD\n\ |
|
232 Return the file descriptor flags for @var{fid}.\n\ |
|
233 \n\ |
|
234 @item F_SETFD\n\ |
|
235 Set the file descriptor flags for @var{fid}.\n\ |
|
236 \n\ |
|
237 @item F_GETFL\n\ |
|
238 Return the file status flags for @var{fid}. The following codes may be\n\ |
|
239 returned (some of the flags may be undefined on some systems).\n\ |
|
240 \n\ |
|
241 @vtable @code\n\ |
|
242 @item O_RDONLY\n\ |
|
243 Open for reading only.\n\ |
|
244 \n\ |
|
245 @item O_WRONLY\n\ |
|
246 Open for writing only.\n\ |
2669
|
247 \n\ |
3301
|
248 @item O_RDWR\n\ |
|
249 Open for reading and writing.\n\ |
|
250 \n\ |
|
251 @item O_APPEND\n\ |
|
252 Append on each write.\n\ |
|
253 \n\ |
|
254 @item O_NONBLOCK\n\ |
|
255 Nonblocking mode.\n\ |
|
256 \n\ |
|
257 @item O_SYNC\n\ |
|
258 Wait for writes to complete.\n\ |
2669
|
259 \n\ |
3301
|
260 @item O_ASYNC\n\ |
|
261 Asynchronous I/O.\n\ |
|
262 @end vtable\n\ |
|
263 \n\ |
|
264 @item F_SETFL\n\ |
|
265 Set the file status flags for @var{fid} to the value specified by\n\ |
|
266 @var{arg}. The only flags that can be changed are @code{O_APPEND} and\n\ |
|
267 @code{O_NONBLOCK}.\n\ |
|
268 @end vtable\n\ |
|
269 \n\ |
|
270 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
271 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
272 system-dependent error message.\n\ |
|
273 @end deftypefn") |
2075
|
274 { |
2669
|
275 octave_value_list retval; |
|
276 |
3523
|
277 retval(1) = std::string (); |
2669
|
278 retval(0) = -1.0; |
2075
|
279 |
|
280 int nargin = args.length (); |
|
281 |
|
282 if (nargin == 3) |
|
283 { |
3715
|
284 octave_stream strm = octave_stream_list::lookup (args (0), "fcntl"); |
2075
|
285 |
3202
|
286 if (! error_state) |
2075
|
287 { |
3715
|
288 int fid = strm.file_number (); |
|
289 |
|
290 int req = args(1).int_value (true); |
|
291 int arg = args(2).int_value (true); |
|
292 |
|
293 if (! error_state) |
2669
|
294 { |
3715
|
295 // XXX FIXME XXX -- Need better checking here? |
|
296 if (fid < 0) |
|
297 error ("fcntl: invalid file id"); |
|
298 else |
|
299 { |
|
300 std::string msg; |
2937
|
301 |
3715
|
302 int status = octave_syscalls::fcntl (fid, req, arg, msg); |
2669
|
303 |
4233
|
304 retval(0) = status; |
3715
|
305 retval(1) = msg; |
|
306 } |
2669
|
307 } |
2075
|
308 } |
|
309 else |
3202
|
310 error ("fcntl: file id, request, and argument must be integers"); |
2075
|
311 } |
|
312 else |
|
313 print_usage ("fcntl"); |
|
314 |
|
315 return retval; |
|
316 } |
|
317 |
2457
|
318 DEFUN (fork, args, , |
3301
|
319 "-*- texinfo -*-\n\ |
|
320 @deftypefn {Built-in Function} {[@var{pid}, @var{msg}] =} fork ()\n\ |
2669
|
321 Create a copy of the current process.\n\ |
|
322 \n\ |
3301
|
323 Fork can return one of the following values:\n\ |
|
324 \n\ |
|
325 @table @asis\n\ |
|
326 @item > 0\n\ |
|
327 You are in the parent process. The value returned from @code{fork} is\n\ |
|
328 the process id of the child process. You should probably arrange to\n\ |
|
329 wait for any child processes to exit.\n\ |
|
330 \n\ |
|
331 @item 0\n\ |
|
332 You are in the child process. You can call @code{exec} to start another\n\ |
|
333 process. If that fails, you should probably call @code{exit}.\n\ |
|
334 \n\ |
|
335 @item < 0\n\ |
|
336 The call to @code{fork} failed for some reason. You must take evasive\n\ |
|
337 action. A system dependent error message will be waiting in @var{msg}.\n\ |
|
338 @end table\n\ |
|
339 @end deftypefn") |
2075
|
340 { |
2669
|
341 octave_value_list retval; |
|
342 |
3523
|
343 retval(1) = std::string (); |
2669
|
344 retval(0) = -1.0; |
2075
|
345 |
|
346 int nargin = args.length (); |
|
347 |
|
348 if (nargin == 0) |
2475
|
349 { |
3523
|
350 std::string msg; |
2937
|
351 |
|
352 pid_t pid = octave_syscalls::fork (msg); |
2669
|
353 |
4233
|
354 retval(0) = pid; |
2937
|
355 retval(1) = msg; |
2475
|
356 } |
2075
|
357 else |
|
358 print_usage ("fork"); |
|
359 |
|
360 return retval; |
|
361 } |
|
362 |
2457
|
363 DEFUN (getpgrp, args, , |
3301
|
364 "-*- texinfo -*-\n\ |
|
365 @deftypefn {Built-in Function} {pgid =} getpgrp ()\n\ |
|
366 Return the process group id of the current process.\n\ |
|
367 @end deftypefn") |
2075
|
368 { |
2937
|
369 octave_value_list retval; |
|
370 |
3523
|
371 retval(1) = std::string (); |
2937
|
372 retval(0) = -1.0; |
2075
|
373 |
|
374 int nargin = args.length (); |
|
375 |
|
376 if (nargin == 0) |
2475
|
377 { |
3523
|
378 std::string msg; |
2937
|
379 |
4233
|
380 retval(0) = octave_syscalls::getpgrp (msg); |
2937
|
381 retval(1) = msg; |
2475
|
382 } |
2075
|
383 else |
|
384 print_usage ("getpgrp"); |
|
385 |
|
386 return retval; |
|
387 } |
|
388 |
2457
|
389 DEFUN (getpid, args, , |
3301
|
390 "-*- texinfo -*-\n\ |
|
391 @deftypefn {Built-in Function} {pid =} getpid ()\n\ |
|
392 Return the process id of the current process.\n\ |
|
393 @end deftypefn") |
2075
|
394 { |
4233
|
395 octave_value retval = -1; |
2075
|
396 |
|
397 int nargin = args.length (); |
|
398 |
|
399 if (nargin == 0) |
2937
|
400 retval = octave_syscalls::getpid (); |
2075
|
401 else |
|
402 print_usage ("getpid"); |
|
403 |
|
404 return retval; |
|
405 } |
|
406 |
2457
|
407 DEFUN (getppid, args, , |
3301
|
408 "-*- texinfo -*-\n\ |
|
409 @deftypefn {Built-in Function} {pid =} getppid ()\n\ |
|
410 Return the process id of the parent process.\n\ |
|
411 @end deftypefn") |
2075
|
412 { |
4233
|
413 octave_value retval = -1; |
2075
|
414 |
2475
|
415 int nargin = args.length (); |
|
416 |
|
417 if (nargin == 0) |
2937
|
418 retval = octave_syscalls::getppid (); |
2475
|
419 else |
|
420 print_usage ("getppid"); |
|
421 |
|
422 return retval; |
|
423 } |
|
424 |
|
425 DEFUN (getegid, args, , |
3301
|
426 "-*- texinfo -*-\n\ |
|
427 @deftypefn {Built-in Function} {egid =} getegid ()\n\ |
|
428 Return the effective group id of the current process.\n\ |
|
429 @end deftypefn") |
2475
|
430 { |
4233
|
431 octave_value retval = -1; |
2475
|
432 |
2075
|
433 int nargin = args.length (); |
|
434 |
|
435 if (nargin == 0) |
4233
|
436 retval = static_cast<double> (octave_syscalls::getegid ()); |
2075
|
437 else |
2475
|
438 print_usage ("getegid"); |
|
439 |
|
440 return retval; |
|
441 } |
|
442 |
|
443 DEFUN (getgid, args, , |
3301
|
444 "-*- texinfo -*-\n\ |
|
445 @deftypefn {Built-in Function} {gid =} getgid ()\n\ |
|
446 Return the real group id of the current process.\n\ |
|
447 @end deftypefn") |
2475
|
448 { |
4233
|
449 octave_value retval = -1; |
2475
|
450 |
|
451 int nargin = args.length (); |
|
452 |
|
453 if (nargin == 0) |
4233
|
454 retval = static_cast<double> (octave_syscalls::getgid ()); |
2475
|
455 else |
|
456 print_usage ("getgid"); |
2075
|
457 |
|
458 return retval; |
|
459 } |
|
460 |
2473
|
461 DEFUN (geteuid, args, , |
3301
|
462 "-*- texinfo -*-\n\ |
|
463 @deftypefn {Built-in Function} {euid =} geteuid ()\n\ |
|
464 Return the effective user id of the current process.\n\ |
|
465 @end deftypefn") |
2472
|
466 { |
4233
|
467 octave_value retval = -1; |
2472
|
468 |
|
469 int nargin = args.length (); |
|
470 |
|
471 if (nargin == 0) |
4233
|
472 retval = static_cast<double> (octave_syscalls::geteuid ()); |
2472
|
473 else |
|
474 print_usage ("geteuid"); |
2473
|
475 |
|
476 return retval; |
2472
|
477 } |
|
478 |
2473
|
479 DEFUN (getuid, args, , |
3301
|
480 "-*- texinfo -*-\n\ |
|
481 @deftypefn {Built-in Function} {uid =} getuid ()\n\ |
|
482 Return the real user id of the current process.\n\ |
|
483 @end deftypefn") |
2472
|
484 { |
4233
|
485 octave_value retval = -1; |
2472
|
486 |
|
487 int nargin = args.length (); |
|
488 |
|
489 if (nargin == 0) |
4233
|
490 retval = static_cast<double> (octave_syscalls::getuid ()); |
2472
|
491 else |
|
492 print_usage ("getuid"); |
2473
|
493 |
|
494 return retval; |
2472
|
495 } |
|
496 |
2075
|
497 DEFUN (lstat, args, , |
3458
|
498 "-*- texinfo -*-\n\ |
|
499 @deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} lstat (@var{file})\n\ |
|
500 See stat.\n\ |
|
501 @end deftypefn") |
2075
|
502 { |
2263
|
503 octave_value_list retval; |
2075
|
504 |
|
505 if (args.length () == 1) |
|
506 { |
3523
|
507 std::string fname = file_ops::tilde_expand (args(0).string_value ()); |
2075
|
508 |
|
509 if (! error_state) |
|
510 { |
|
511 file_stat fs (fname, false); |
|
512 |
2263
|
513 if (fs) |
2262
|
514 { |
3523
|
515 retval(2) = std::string (); |
2262
|
516 retval(1) = 0.0; |
4233
|
517 retval(0) = mk_stat_map (fs); |
2262
|
518 } |
|
519 else |
|
520 { |
|
521 retval(2) = fs.error (); |
|
522 retval(1) = -1.0; |
|
523 retval(0) = Matrix (); |
|
524 } |
2075
|
525 } |
|
526 } |
|
527 else |
|
528 print_usage ("lstat"); |
|
529 |
|
530 return retval; |
|
531 } |
|
532 |
3301
|
533 |
|
534 |
2075
|
535 DEFUN (mkfifo, args, , |
3345
|
536 "-*- texinfo -*-\n\ |
|
537 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} mkfifo (@var{name})\n\ |
3301
|
538 Create a @var{fifo} special file named @var{name} with file mode @var{mode}\n\\n\ |
2075
|
539 \n\ |
3301
|
540 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
541 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
542 system-dependent error message.\n\ |
|
543 @end deftypefn") |
2075
|
544 { |
2669
|
545 octave_value_list retval; |
|
546 |
3523
|
547 retval(1) = std::string (); |
2669
|
548 retval(0) = -1.0; |
2075
|
549 |
|
550 int nargin = args.length (); |
|
551 |
|
552 if (nargin == 2) |
|
553 { |
|
554 if (args(0).is_string ()) |
|
555 { |
3523
|
556 std::string name = args(0).string_value (); |
2075
|
557 |
|
558 if (args(1).is_scalar_type ()) |
|
559 { |
2800
|
560 long mode = static_cast<long> (args(1).double_value ()); |
2075
|
561 |
3523
|
562 std::string msg; |
2669
|
563 |
2926
|
564 int status = file_ops::mkfifo (name, mode, msg); |
2669
|
565 |
4233
|
566 retval(0) = status; |
2669
|
567 |
|
568 if (status < 0) |
|
569 retval(1) = msg; |
2075
|
570 } |
|
571 else |
|
572 error ("mkfifo: MODE must be an integer"); |
|
573 } |
|
574 else |
|
575 error ("mkfifo: file name must be a string"); |
|
576 } |
|
577 else |
|
578 print_usage ("mkfifo"); |
|
579 |
|
580 return retval; |
|
581 } |
|
582 |
|
583 DEFUN (pipe, args, , |
3301
|
584 "-*- texinfo -*-\n\ |
|
585 @deftypefn {Built-in Function} {[@var{file_ids}, @var{err}, @var{msg}] =} pipe ()\n\ |
|
586 Create a pipe and return the vector @var{file_ids}, which corresponding\n\ |
|
587 to the reading and writing ends of the pipe.\n\ |
2669
|
588 \n\ |
3301
|
589 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
590 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
591 system-dependent error message.\n\ |
|
592 @end deftypefn") |
2075
|
593 { |
2669
|
594 octave_value_list retval; |
|
595 |
3523
|
596 retval(2) = std::string (); |
2669
|
597 retval(1) = -1.0; |
|
598 retval(0) = Matrix (); |
2075
|
599 |
|
600 int nargin = args.length (); |
|
601 |
|
602 if (nargin == 0) |
|
603 { |
|
604 int fid[2]; |
|
605 |
3523
|
606 std::string msg; |
2937
|
607 |
|
608 int status = octave_syscalls::pipe (fid, msg); |
2669
|
609 |
|
610 if (status < 0) |
2937
|
611 retval(2) = msg; |
2669
|
612 else |
2075
|
613 { |
3340
|
614 FILE *ifile = fdopen (fid[0], "r"); |
|
615 FILE *ofile = fdopen (fid[1], "w"); |
2075
|
616 |
3523
|
617 octave_stream is = octave_istdiostream::create (std::string (), ifile); |
|
618 octave_stream os = octave_ostdiostream::create (std::string (), ofile); |
2075
|
619 |
2902
|
620 octave_value_list file_ids; |
2075
|
621 |
2902
|
622 file_ids(1) = octave_stream_list::insert (os); |
|
623 file_ids(0) = octave_stream_list::insert (is); |
2075
|
624 |
4233
|
625 retval(1) = status; |
2902
|
626 retval(0) = octave_value (file_ids); |
2669
|
627 } |
2075
|
628 } |
|
629 else |
|
630 print_usage ("pipe"); |
|
631 |
|
632 return retval; |
|
633 } |
|
634 |
|
635 DEFUN (stat, args, , |
3301
|
636 "-*- texinfo -*-\n\ |
|
637 @deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} stat (@var{file})\n\ |
|
638 @deftypefnx {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} lstat (@var{file})\n\ |
|
639 Return a structure @var{s} containing the following information about\n\ |
|
640 @var{file}.\n\ |
|
641 \n\ |
|
642 @table @code\n\ |
|
643 @item dev\n\ |
|
644 ID of device containing a directory entry for this file.\n\ |
|
645 \n\ |
|
646 @item ino\n\ |
|
647 File number of the file.\n\ |
|
648 \n\ |
|
649 @item modestr\n\ |
|
650 File mode, as a string of ten letters or dashes as would be returned by\n\ |
|
651 @kbd{ls -l}.\n\ |
|
652 \n\ |
|
653 @item nlink\n\ |
|
654 Number of links.\n\ |
2075
|
655 \n\ |
3301
|
656 @item uid\n\ |
|
657 User ID of file's owner.\n\ |
|
658 \n\ |
|
659 @item gid\n\ |
|
660 Group ID of file's group.\n\ |
|
661 \n\ |
|
662 @item rdev\n\ |
|
663 ID of device for block or character special files.\n\ |
|
664 \n\ |
|
665 @item size\n\ |
|
666 Size in bytes.\n\ |
|
667 \n\ |
|
668 @item atime\n\ |
|
669 Time of last access in the same form as time values returned from\n\ |
|
670 @code{time}. @xref{Timing Utilities}.\n\ |
|
671 \n\ |
|
672 @item mtime\n\ |
|
673 Time of last modification in the same form as time values returned from\n\ |
|
674 @code{time}. @xref{Timing Utilities}.\n\ |
2075
|
675 \n\ |
3301
|
676 @item ctime\n\ |
|
677 Time of last file status change in the same form as time values\n\ |
|
678 returned from @code{time}. @xref{Timing Utilities}.\n\ |
|
679 \n\ |
|
680 @item blksize\n\ |
|
681 Size of blocks in the file.\n\ |
|
682 \n\ |
|
683 @item blocks\n\ |
|
684 Number of blocks allocated for file.\n\ |
|
685 @end table\n\ |
|
686 \n\ |
|
687 If the call is successful @var{err} is 0 and @var{msg} is an empty\n\ |
|
688 string. If the file does not exist, or some other error occurs, @var{s}\n\ |
|
689 is an empty matrix, @var{err} is @minus{}1, and @var{msg} contains the\n\ |
|
690 corresponding system error message.\n\ |
|
691 \n\ |
|
692 If @var{file} is a symbolic link, @code{stat} will return information\n\ |
|
693 about the actual file the is referenced by the link. Use @code{lstat}\n\ |
|
694 if you want information about the symbolic link itself.\n\ |
|
695 \n\ |
|
696 For example,\n\ |
2075
|
697 \n\ |
3301
|
698 @example\n\ |
|
699 @group\n\ |
|
700 [s, err, msg] = stat (\"/vmlinuz\")\n\ |
|
701 @result{} s =\n\ |
|
702 @{\n\ |
|
703 atime = 855399756\n\ |
|
704 rdev = 0\n\ |
|
705 ctime = 847219094\n\ |
|
706 uid = 0\n\ |
|
707 size = 389218\n\ |
|
708 blksize = 4096\n\ |
|
709 mtime = 847219094\n\ |
|
710 gid = 6\n\ |
|
711 nlink = 1\n\ |
|
712 blocks = 768\n\ |
|
713 modestr = -rw-r--r--\n\ |
|
714 ino = 9316\n\ |
|
715 dev = 2049\n\ |
|
716 @}\n\ |
|
717 @result{} err = 0\n\ |
|
718 @result{} msg = \n\ |
|
719 @end group\n\ |
|
720 @end example\n\ |
|
721 @end deftypefn") |
2075
|
722 { |
2262
|
723 octave_value_list retval; |
2075
|
724 |
|
725 if (args.length () == 1) |
|
726 { |
3523
|
727 std::string fname = file_ops::tilde_expand (args(0).string_value ()); |
2075
|
728 |
|
729 if (! error_state) |
|
730 { |
|
731 file_stat fs (fname); |
|
732 |
|
733 if (fs) |
2262
|
734 { |
3523
|
735 retval(2) = std::string (); |
2262
|
736 retval(1) = 0.0; |
|
737 retval(0) = octave_value (mk_stat_map (fs)); |
|
738 } |
|
739 else |
|
740 { |
|
741 retval(2) = fs.error (); |
|
742 retval(1) = -1.0; |
|
743 retval(0) = Matrix (); |
|
744 } |
2075
|
745 } |
|
746 } |
|
747 else |
|
748 print_usage ("stat"); |
|
749 |
|
750 return retval; |
|
751 } |
|
752 |
|
753 DEFUN (unlink, args, , |
3301
|
754 "-*- texinfo -*-\n\ |
|
755 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} unlink (@var{file})\n\ |
|
756 Delete the file named @var{file}.\n\ |
2075
|
757 \n\ |
3301
|
758 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
759 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
760 system-dependent error message.\n\ |
|
761 @end deftypefn") |
2075
|
762 { |
2669
|
763 octave_value_list retval; |
|
764 |
3523
|
765 retval(1) = std::string (); |
2669
|
766 retval(0) = -1.0; |
2075
|
767 |
|
768 int nargin = args.length (); |
|
769 |
|
770 if (nargin == 1) |
|
771 { |
|
772 if (args(0).is_string ()) |
|
773 { |
3523
|
774 std::string name = args(0).string_value (); |
2075
|
775 |
3523
|
776 std::string msg; |
2669
|
777 |
2926
|
778 int status = file_ops::unlink (name, msg); |
2669
|
779 |
4233
|
780 retval(0) = status; |
2937
|
781 retval(1) = msg; |
2075
|
782 } |
|
783 else |
|
784 error ("unlink: file name must be a string"); |
|
785 } |
|
786 else |
|
787 print_usage ("unlink"); |
|
788 |
|
789 return retval; |
|
790 } |
|
791 |
|
792 DEFUN (waitpid, args, , |
3301
|
793 "-*- texinfo -*-\n\ |
|
794 @deftypefn {Built-in Function} {[@var{pid}, @var{msg}] =} waitpid (@var{pid}, @var{options})\n\ |
|
795 Wait for process @var{pid} to terminate. The @var{pid} argument can be:\n\ |
2075
|
796 \n\ |
3301
|
797 @table @asis\n\ |
|
798 @item @minus{}1\n\ |
|
799 Wait for any child process.\n\ |
2075
|
800 \n\ |
3301
|
801 @item 0\n\ |
|
802 Wait for any child process whose process group ID is equal to that of\n\ |
|
803 the Octave interpreter process.\n\ |
2075
|
804 \n\ |
3301
|
805 @item > 0\n\ |
|
806 Wait for termination of the child process with ID @var{pid}.\n\ |
|
807 @end table\n\ |
|
808 \n\ |
|
809 The @var{options} argument can be:\n\ |
2075
|
810 \n\ |
3301
|
811 @table @asis\n\ |
|
812 @item 0\n\ |
|
813 Wait until signal is received or a child process exits (this is the\n\ |
|
814 default if the @var{options} argument is missing).\n\ |
|
815 \n\ |
|
816 @item 1\n\ |
|
817 Do not hang if status is not immediately available.\n\ |
2075
|
818 \n\ |
3301
|
819 @item 2\n\ |
|
820 Report the status of any child processes that are stopped, and whose\n\ |
|
821 status has not yet been reported since they stopped.\n\ |
|
822 \n\ |
|
823 @item 3\n\ |
|
824 Implies both 1 and 2.\n\ |
|
825 @end table\n\ |
|
826 \n\ |
|
827 If the returned value of @var{pid} is greater than 0, it is the process\n\ |
|
828 ID of the child process that exited. If an error occurs, @var{pid} will\n\ |
|
829 be less than zero and @var{msg} will contain a system-dependent error\n\ |
|
830 message.\n\ |
|
831 @end deftypefn") |
2075
|
832 { |
2669
|
833 octave_value_list retval; |
|
834 |
3523
|
835 retval(1) = std::string (); |
2669
|
836 retval(0) = -1.0; |
2075
|
837 |
|
838 int nargin = args.length (); |
|
839 |
|
840 if (nargin == 1 || nargin == 2) |
|
841 { |
3202
|
842 pid_t pid = args(0).int_value (true); |
2075
|
843 |
|
844 if (! error_state) |
|
845 { |
3202
|
846 int options = 0; |
2075
|
847 |
3202
|
848 if (args.length () == 2) |
|
849 { |
|
850 options = args(1).int_value (true); |
2075
|
851 |
|
852 if (! error_state) |
2669
|
853 { |
3202
|
854 if (options < 0 || options > 3) |
|
855 error ("waitpid: invalid OPTIONS value specified"); |
|
856 } |
|
857 else |
|
858 error ("waitpid: OPTIONS must be in integer"); |
|
859 } |
2937
|
860 |
3202
|
861 if (! error_state) |
|
862 { |
3523
|
863 std::string msg; |
2669
|
864 |
3202
|
865 pid_t status = octave_syscalls::waitpid (pid, options, msg); |
|
866 |
4233
|
867 retval(0) = status; |
3202
|
868 retval(1) = msg; |
2075
|
869 } |
|
870 } |
3202
|
871 else |
|
872 error ("waitpid: PID must be an integer value"); |
2075
|
873 } |
|
874 else |
|
875 print_usage ("waitpid"); |
|
876 |
|
877 return retval; |
|
878 } |
|
879 |
|
880 #if !defined (O_NONBLOCK) && defined (O_NDELAY) |
|
881 #define O_NONBLOCK O_NDELAY |
|
882 #endif |
|
883 |
|
884 void |
|
885 symbols_of_syscalls (void) |
|
886 { |
|
887 #if defined (F_DUPFD) |
4233
|
888 DEFCONSTX ("F_DUPFD", SBV_F_DUPFD, F_DUPFD, |
3446
|
889 "-*- texinfo -*-\n\ |
|
890 @defvr {Built-in Variable} F_DUPFD\n\ |
|
891 @end defvr"); |
2075
|
892 #endif |
|
893 |
|
894 #if defined (F_GETFD) |
4233
|
895 DEFCONSTX ("F_GETFD", SBV_F_GETFD, F_GETFD, |
3446
|
896 "-*- texinfo -*-\n\ |
|
897 @defvr {Built-in Variable} F_GETFD\n\ |
|
898 @end defvr"); |
2075
|
899 #endif |
|
900 |
|
901 #if defined (F_GETFL) |
4233
|
902 DEFCONSTX ("F_GETFL", SBV_F_GETFL, F_GETFL, |
3446
|
903 "-*- texinfo -*-\n\ |
|
904 @defvr {Built-in Variable} F_GETFL\n\ |
|
905 @end defvr"); |
2075
|
906 #endif |
|
907 |
|
908 #if defined (F_SETFD) |
4233
|
909 DEFCONSTX ("F_SETFD", SBV_F_SETFD, F_SETFD, |
3446
|
910 "-*- texinfo -*-\n\ |
|
911 @defvr {Built-in Variable} F_SETFD\n\ |
|
912 @end defvr"); |
2075
|
913 #endif |
|
914 |
|
915 #if defined (F_SETFL) |
4233
|
916 DEFCONSTX ("F_SETFL", SBV_F_SETFL, F_SETFL, |
3446
|
917 "-*- texinfo -*-\n\ |
|
918 @defvr {Built-in Variable} F_SETFL\n\ |
|
919 @end defvr"); |
2075
|
920 #endif |
|
921 |
|
922 #if defined (O_APPEND) |
4233
|
923 DEFCONSTX ("O_APPEND", SBV_O_APPEND, O_APPEND, |
3446
|
924 "-*- texinfo -*-\n\ |
|
925 @defvr {Built-in Variable} O_APPEND\n\ |
|
926 @end defvr"); |
2075
|
927 #endif |
|
928 |
2669
|
929 #if defined (O_ASYNC) |
4233
|
930 DEFCONSTX ("O_ASYNC", SBV_O_ASYNC, O_ASYNC, |
3446
|
931 "-*- texinfo -*-\n\ |
|
932 @defvr {Built-in Variable} O_ASYNC\n\ |
|
933 @end defvr"); |
2669
|
934 #endif |
|
935 |
2075
|
936 #if defined (O_CREAT) |
4233
|
937 DEFCONSTX ("O_CREAT", SBV_O_CREAT, O_CREAT, |
3446
|
938 "-*- texinfo -*-\n\ |
|
939 @defvr {Built-in Variable} O_CREAT\n\ |
|
940 @end defvr"); |
2075
|
941 #endif |
|
942 |
|
943 #if defined (O_EXCL) |
4233
|
944 DEFCONSTX ("O_EXCL", SBV_O_EXCL, O_EXCL, |
3446
|
945 "-*- texinfo -*-\n\ |
|
946 @defvr {Built-in Variable} O_EXCL\n\ |
|
947 @end defvr"); |
2075
|
948 #endif |
|
949 |
|
950 #if defined (O_NONBLOCK) |
4233
|
951 DEFCONSTX ("O_NONBLOCK", SBV_O_NONBLOCK, O_NONBLOCK, |
3446
|
952 "-*- texinfo -*-\n\ |
|
953 @defvr {Built-in Variable} O_NONBLOCK\n\ |
|
954 @end defvr"); |
2075
|
955 #endif |
|
956 |
|
957 #if defined (O_RDONLY) |
4233
|
958 DEFCONSTX ("O_RDONLY", SBV_O_RDONLY, O_RDONLY, |
3446
|
959 "-*- texinfo -*-\n\ |
|
960 @defvr {Built-in Variable} O_RDONLY\n\ |
|
961 @end defvr"); |
2075
|
962 #endif |
|
963 |
|
964 #if defined (O_RDWR) |
4233
|
965 DEFCONSTX ("O_RDWR", SBV_O_RDWR, O_RDWR, |
3446
|
966 "-*- texinfo -*-\n\ |
|
967 @defvr {Built-in Variable} O_RDWR\n\ |
|
968 @end defvr"); |
2075
|
969 #endif |
|
970 |
2669
|
971 #if defined (O_SYNC) |
4233
|
972 DEFCONSTX ("O_SYNC", SBV_O_SYNC, O_SYNC, |
3446
|
973 "-*- texinfo -*-\n\ |
|
974 @defvr {Built-in Variable} O_SYNC\n\ |
|
975 @end defvr"); |
2669
|
976 #endif |
|
977 |
2075
|
978 #if defined (O_TRUNC) |
4233
|
979 DEFCONSTX ("O_TRUNC", SBV_O_TRUNC, O_TRUNC, |
3446
|
980 "-*- texinfo -*-\n\ |
|
981 @defvr {Built-in Variable} O_TRUNC\n\ |
|
982 @end defvr"); |
2075
|
983 #endif |
|
984 |
|
985 #if defined (O_WRONLY) |
4233
|
986 DEFCONSTX ("O_WRONLY", SBV_O_WRONLY, O_WRONLY, |
3446
|
987 "-*- texinfo -*-\n\ |
|
988 @defvr {Built-in Variable} O_WRONLY\n\ |
|
989 @end defvr"); |
2075
|
990 #endif |
3446
|
991 |
2075
|
992 } |
|
993 |
|
994 /* |
|
995 ;;; Local Variables: *** |
|
996 ;;; mode: C++ *** |
|
997 ;;; End: *** |
|
998 */ |