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, , |
3301
|
528 "@deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} mkfifo (@var{name})\n\ |
|
529 Create a @var{fifo} special file named @var{name} with file mode @var{mode}\n\\n\ |
2075
|
530 \n\ |
3301
|
531 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
532 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
533 system-dependent error message.\n\ |
|
534 @end deftypefn") |
2075
|
535 { |
2669
|
536 octave_value_list retval; |
|
537 |
|
538 retval(1) = string (); |
|
539 retval(0) = -1.0; |
2075
|
540 |
|
541 int nargin = args.length (); |
|
542 |
|
543 if (nargin == 2) |
|
544 { |
|
545 if (args(0).is_string ()) |
|
546 { |
|
547 string name = args(0).string_value (); |
|
548 |
|
549 if (args(1).is_scalar_type ()) |
|
550 { |
2800
|
551 long mode = static_cast<long> (args(1).double_value ()); |
2075
|
552 |
2669
|
553 string msg; |
|
554 |
2926
|
555 int status = file_ops::mkfifo (name, mode, msg); |
2669
|
556 |
2800
|
557 retval(0) = static_cast<double> (status); |
2669
|
558 |
|
559 if (status < 0) |
|
560 retval(1) = msg; |
2075
|
561 } |
|
562 else |
|
563 error ("mkfifo: MODE must be an integer"); |
|
564 } |
|
565 else |
|
566 error ("mkfifo: file name must be a string"); |
|
567 } |
|
568 else |
|
569 print_usage ("mkfifo"); |
|
570 |
|
571 return retval; |
|
572 } |
|
573 |
|
574 DEFUN (pipe, args, , |
3301
|
575 "-*- texinfo -*-\n\ |
|
576 @deftypefn {Built-in Function} {[@var{file_ids}, @var{err}, @var{msg}] =} pipe ()\n\ |
|
577 Create a pipe and return the vector @var{file_ids}, which corresponding\n\ |
|
578 to the reading and writing ends of the pipe.\n\ |
2669
|
579 \n\ |
3301
|
580 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
581 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
582 system-dependent error message.\n\ |
|
583 @end deftypefn") |
2075
|
584 { |
2669
|
585 octave_value_list retval; |
|
586 |
|
587 retval(2) = string (); |
|
588 retval(1) = -1.0; |
|
589 retval(0) = Matrix (); |
2075
|
590 |
|
591 int nargin = args.length (); |
|
592 |
|
593 if (nargin == 0) |
|
594 { |
|
595 int fid[2]; |
|
596 |
2937
|
597 string msg; |
|
598 |
|
599 int status = octave_syscalls::pipe (fid, msg); |
2669
|
600 |
|
601 if (status < 0) |
2937
|
602 retval(2) = msg; |
2669
|
603 else |
2075
|
604 { |
3340
|
605 FILE *ifile = fdopen (fid[0], "r"); |
|
606 FILE *ofile = fdopen (fid[1], "w"); |
2075
|
607 |
3340
|
608 octave_stream is = octave_istdiostream::create (string (), ifile); |
|
609 octave_stream os = octave_ostdiostream::create (string (), ofile); |
2075
|
610 |
2902
|
611 octave_value_list file_ids; |
2075
|
612 |
2902
|
613 file_ids(1) = octave_stream_list::insert (os); |
|
614 file_ids(0) = octave_stream_list::insert (is); |
2075
|
615 |
2800
|
616 retval(1) = static_cast<double> (status); |
2902
|
617 retval(0) = octave_value (file_ids); |
2669
|
618 } |
2075
|
619 } |
|
620 else |
|
621 print_usage ("pipe"); |
|
622 |
|
623 return retval; |
|
624 } |
|
625 |
|
626 DEFUN (stat, args, , |
3301
|
627 "-*- texinfo -*-\n\ |
|
628 @deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} stat (@var{file})\n\ |
|
629 @deftypefnx {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} lstat (@var{file})\n\ |
|
630 Return a structure @var{s} containing the following information about\n\ |
|
631 @var{file}.\n\ |
|
632 \n\ |
|
633 @table @code\n\ |
|
634 @item dev\n\ |
|
635 ID of device containing a directory entry for this file.\n\ |
|
636 \n\ |
|
637 @item ino\n\ |
|
638 File number of the file.\n\ |
|
639 \n\ |
|
640 @item modestr\n\ |
|
641 File mode, as a string of ten letters or dashes as would be returned by\n\ |
|
642 @kbd{ls -l}.\n\ |
|
643 \n\ |
|
644 @item nlink\n\ |
|
645 Number of links.\n\ |
2075
|
646 \n\ |
3301
|
647 @item uid\n\ |
|
648 User ID of file's owner.\n\ |
|
649 \n\ |
|
650 @item gid\n\ |
|
651 Group ID of file's group.\n\ |
|
652 \n\ |
|
653 @item rdev\n\ |
|
654 ID of device for block or character special files.\n\ |
|
655 \n\ |
|
656 @item size\n\ |
|
657 Size in bytes.\n\ |
|
658 \n\ |
|
659 @item atime\n\ |
|
660 Time of last access in the same form as time values returned from\n\ |
|
661 @code{time}. @xref{Timing Utilities}.\n\ |
|
662 \n\ |
|
663 @item mtime\n\ |
|
664 Time of last modification in the same form as time values returned from\n\ |
|
665 @code{time}. @xref{Timing Utilities}.\n\ |
2075
|
666 \n\ |
3301
|
667 @item ctime\n\ |
|
668 Time of last file status change in the same form as time values\n\ |
|
669 returned from @code{time}. @xref{Timing Utilities}.\n\ |
|
670 \n\ |
|
671 @item blksize\n\ |
|
672 Size of blocks in the file.\n\ |
|
673 \n\ |
|
674 @item blocks\n\ |
|
675 Number of blocks allocated for file.\n\ |
|
676 @end table\n\ |
|
677 \n\ |
|
678 If the call is successful @var{err} is 0 and @var{msg} is an empty\n\ |
|
679 string. If the file does not exist, or some other error occurs, @var{s}\n\ |
|
680 is an empty matrix, @var{err} is @minus{}1, and @var{msg} contains the\n\ |
|
681 corresponding system error message.\n\ |
|
682 \n\ |
|
683 If @var{file} is a symbolic link, @code{stat} will return information\n\ |
|
684 about the actual file the is referenced by the link. Use @code{lstat}\n\ |
|
685 if you want information about the symbolic link itself.\n\ |
|
686 \n\ |
|
687 For example,\n\ |
2075
|
688 \n\ |
3301
|
689 @example\n\ |
|
690 @group\n\ |
|
691 [s, err, msg] = stat (\"/vmlinuz\")\n\ |
|
692 @result{} s =\n\ |
|
693 @{\n\ |
|
694 atime = 855399756\n\ |
|
695 rdev = 0\n\ |
|
696 ctime = 847219094\n\ |
|
697 uid = 0\n\ |
|
698 size = 389218\n\ |
|
699 blksize = 4096\n\ |
|
700 mtime = 847219094\n\ |
|
701 gid = 6\n\ |
|
702 nlink = 1\n\ |
|
703 blocks = 768\n\ |
|
704 modestr = -rw-r--r--\n\ |
|
705 ino = 9316\n\ |
|
706 dev = 2049\n\ |
|
707 @}\n\ |
|
708 @result{} err = 0\n\ |
|
709 @result{} msg = \n\ |
|
710 @end group\n\ |
|
711 @end example\n\ |
|
712 @end deftypefn") |
2075
|
713 { |
2262
|
714 octave_value_list retval; |
2075
|
715 |
|
716 if (args.length () == 1) |
|
717 { |
2926
|
718 string fname = file_ops::tilde_expand (args(0).string_value ()); |
2075
|
719 |
|
720 if (! error_state) |
|
721 { |
|
722 file_stat fs (fname); |
|
723 |
|
724 if (fs) |
2262
|
725 { |
|
726 retval(2) = string (); |
|
727 retval(1) = 0.0; |
|
728 retval(0) = octave_value (mk_stat_map (fs)); |
|
729 } |
|
730 else |
|
731 { |
|
732 retval(2) = fs.error (); |
|
733 retval(1) = -1.0; |
|
734 retval(0) = Matrix (); |
|
735 } |
2075
|
736 } |
|
737 } |
|
738 else |
|
739 print_usage ("stat"); |
|
740 |
|
741 return retval; |
|
742 } |
|
743 |
|
744 DEFUN (unlink, args, , |
3301
|
745 "-*- texinfo -*-\n\ |
|
746 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} unlink (@var{file})\n\ |
|
747 Delete the file named @var{file}.\n\ |
2075
|
748 \n\ |
3301
|
749 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
750 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
751 system-dependent error message.\n\ |
|
752 @end deftypefn") |
2075
|
753 { |
2669
|
754 octave_value_list retval; |
|
755 |
|
756 retval(1) = string (); |
|
757 retval(0) = -1.0; |
2075
|
758 |
|
759 int nargin = args.length (); |
|
760 |
|
761 if (nargin == 1) |
|
762 { |
|
763 if (args(0).is_string ()) |
|
764 { |
|
765 string name = args(0).string_value (); |
|
766 |
2669
|
767 string msg; |
|
768 |
2926
|
769 int status = file_ops::unlink (name, msg); |
2669
|
770 |
2800
|
771 retval(0) = static_cast<double> (status); |
2937
|
772 retval(1) = msg; |
2075
|
773 } |
|
774 else |
|
775 error ("unlink: file name must be a string"); |
|
776 } |
|
777 else |
|
778 print_usage ("unlink"); |
|
779 |
|
780 return retval; |
|
781 } |
|
782 |
|
783 DEFUN (waitpid, args, , |
3301
|
784 "-*- texinfo -*-\n\ |
|
785 @deftypefn {Built-in Function} {[@var{pid}, @var{msg}] =} waitpid (@var{pid}, @var{options})\n\ |
|
786 Wait for process @var{pid} to terminate. The @var{pid} argument can be:\n\ |
2075
|
787 \n\ |
3301
|
788 @table @asis\n\ |
|
789 @item @minus{}1\n\ |
|
790 Wait for any child process.\n\ |
2075
|
791 \n\ |
3301
|
792 @item 0\n\ |
|
793 Wait for any child process whose process group ID is equal to that of\n\ |
|
794 the Octave interpreter process.\n\ |
2075
|
795 \n\ |
3301
|
796 @item > 0\n\ |
|
797 Wait for termination of the child process with ID @var{pid}.\n\ |
|
798 @end table\n\ |
|
799 \n\ |
|
800 The @var{options} argument can be:\n\ |
2075
|
801 \n\ |
3301
|
802 @table @asis\n\ |
|
803 @item 0\n\ |
|
804 Wait until signal is received or a child process exits (this is the\n\ |
|
805 default if the @var{options} argument is missing).\n\ |
|
806 \n\ |
|
807 @item 1\n\ |
|
808 Do not hang if status is not immediately available.\n\ |
2075
|
809 \n\ |
3301
|
810 @item 2\n\ |
|
811 Report the status of any child processes that are stopped, and whose\n\ |
|
812 status has not yet been reported since they stopped.\n\ |
|
813 \n\ |
|
814 @item 3\n\ |
|
815 Implies both 1 and 2.\n\ |
|
816 @end table\n\ |
|
817 \n\ |
|
818 If the returned value of @var{pid} is greater than 0, it is the process\n\ |
|
819 ID of the child process that exited. If an error occurs, @var{pid} will\n\ |
|
820 be less than zero and @var{msg} will contain a system-dependent error\n\ |
|
821 message.\n\ |
|
822 @end deftypefn") |
2075
|
823 { |
2669
|
824 octave_value_list retval; |
|
825 |
|
826 retval(1) = string (); |
|
827 retval(0) = -1.0; |
2075
|
828 |
|
829 int nargin = args.length (); |
|
830 |
|
831 if (nargin == 1 || nargin == 2) |
|
832 { |
3202
|
833 pid_t pid = args(0).int_value (true); |
2075
|
834 |
|
835 if (! error_state) |
|
836 { |
3202
|
837 int options = 0; |
2075
|
838 |
3202
|
839 if (args.length () == 2) |
|
840 { |
|
841 options = args(1).int_value (true); |
2075
|
842 |
|
843 if (! error_state) |
2669
|
844 { |
3202
|
845 if (options < 0 || options > 3) |
|
846 error ("waitpid: invalid OPTIONS value specified"); |
|
847 } |
|
848 else |
|
849 error ("waitpid: OPTIONS must be in integer"); |
|
850 } |
2937
|
851 |
3202
|
852 if (! error_state) |
|
853 { |
|
854 string msg; |
2669
|
855 |
3202
|
856 pid_t status = octave_syscalls::waitpid (pid, options, msg); |
|
857 |
|
858 retval(0) = static_cast<double> (status); |
|
859 retval(1) = msg; |
2075
|
860 } |
|
861 } |
3202
|
862 else |
|
863 error ("waitpid: PID must be an integer value"); |
2075
|
864 } |
|
865 else |
|
866 print_usage ("waitpid"); |
|
867 |
|
868 return retval; |
|
869 } |
|
870 |
|
871 #if !defined (O_NONBLOCK) && defined (O_NDELAY) |
|
872 #define O_NONBLOCK O_NDELAY |
|
873 #endif |
|
874 |
|
875 void |
|
876 symbols_of_syscalls (void) |
|
877 { |
|
878 #if defined (F_DUPFD) |
3141
|
879 DEFCONST (F_DUPFD, static_cast<double> (F_DUPFD), |
2075
|
880 ""); |
|
881 #endif |
|
882 |
|
883 #if defined (F_GETFD) |
3141
|
884 DEFCONST (F_GETFD, static_cast<double> (F_GETFD), |
2075
|
885 ""); |
|
886 #endif |
|
887 |
|
888 #if defined (F_GETFL) |
3141
|
889 DEFCONST (F_GETFL, static_cast<double> (F_GETFL), |
2075
|
890 ""); |
|
891 #endif |
|
892 |
|
893 #if defined (F_SETFD) |
3141
|
894 DEFCONST (F_SETFD, static_cast<double> (F_SETFD), |
2075
|
895 ""); |
|
896 #endif |
|
897 |
|
898 #if defined (F_SETFL) |
3141
|
899 DEFCONST (F_SETFL, static_cast<double> (F_SETFL), |
2075
|
900 ""); |
|
901 #endif |
|
902 |
|
903 #if defined (O_APPEND) |
3141
|
904 DEFCONST (O_APPEND, static_cast<double> (O_APPEND), |
2075
|
905 ""); |
|
906 #endif |
|
907 |
2669
|
908 #if defined (O_ASYNC) |
3141
|
909 DEFCONST (O_ASYNC, static_cast<double> (O_ASYNC), |
2669
|
910 ""); |
|
911 #endif |
|
912 |
2075
|
913 #if defined (O_CREAT) |
3141
|
914 DEFCONST (O_CREAT, static_cast<double> (O_CREAT), |
2075
|
915 ""); |
|
916 #endif |
|
917 |
|
918 #if defined (O_EXCL) |
3141
|
919 DEFCONST (O_EXCL, static_cast<double> (O_EXCL), |
2075
|
920 ""); |
|
921 #endif |
|
922 |
|
923 #if defined (O_NONBLOCK) |
3141
|
924 DEFCONST (O_NONBLOCK, static_cast<double> (O_NONBLOCK), |
2075
|
925 ""); |
|
926 #endif |
|
927 |
|
928 #if defined (O_RDONLY) |
3141
|
929 DEFCONST (O_RDONLY, static_cast<double> (O_RDONLY), |
2075
|
930 ""); |
|
931 #endif |
|
932 |
|
933 #if defined (O_RDWR) |
3141
|
934 DEFCONST (O_RDWR, static_cast<double> (O_RDWR), |
2075
|
935 ""); |
|
936 #endif |
|
937 |
2669
|
938 #if defined (O_SYNC) |
3141
|
939 DEFCONST (O_SYNC, static_cast<double> (O_SYNC), |
2669
|
940 ""); |
|
941 #endif |
|
942 |
2075
|
943 #if defined (O_TRUNC) |
3141
|
944 DEFCONST (O_TRUNC, static_cast<double> (O_TRUNC), |
2075
|
945 ""); |
|
946 #endif |
|
947 |
|
948 #if defined (O_WRONLY) |
3141
|
949 DEFCONST (O_WRONLY, static_cast<double> (O_WRONLY), |
2075
|
950 ""); |
|
951 #endif |
|
952 } |
|
953 |
|
954 /* |
|
955 ;;; Local Variables: *** |
|
956 ;;; mode: C++ *** |
|
957 ;;; End: *** |
|
958 */ |