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