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 |
2800
|
68 m["dev"] = static_cast<double> (fs.dev ()); |
|
69 m["ino"] = static_cast<double> (fs.ino ()); |
2075
|
70 m["modestr"] = fs.mode_as_string (); |
2800
|
71 m["nlink"] = static_cast<double> (fs.nlink ()); |
|
72 m["uid"] = static_cast<double> (fs.uid ()); |
|
73 m["gid"] = static_cast<double> (fs.gid ()); |
2075
|
74 #if defined (HAVE_ST_RDEV) |
2800
|
75 m["rdev"] = static_cast<double> (fs.rdev ()); |
2075
|
76 #endif |
2800
|
77 m["size"] = static_cast<double> (fs.size ()); |
|
78 m["atime"] = static_cast<double> (fs.atime ()); |
|
79 m["mtime"] = static_cast<double> (fs.mtime ()); |
|
80 m["ctime"] = static_cast<double> (fs.ctime ()); |
2075
|
81 #if defined (HAVE_ST_BLKSIZE) |
2800
|
82 m["blksize"] = static_cast<double> (fs.blksize ()); |
2075
|
83 #endif |
|
84 #if defined (HAVE_ST_BLOCKS) |
2800
|
85 m["blocks"] = 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 |
|
129 retval(0) = static_cast<double> (status); |
|
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 |
2800
|
208 retval(0) = static_cast<double> (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 { |
3202
|
284 int fid = args(0).int_value (true); |
|
285 int req = args(1).int_value (true); |
|
286 int arg = args(2).int_value (true); |
2075
|
287 |
3202
|
288 if (! error_state) |
2075
|
289 { |
|
290 // XXX FIXME XXX -- Need better checking here? |
|
291 if (fid < 0) |
|
292 error ("fcntl: invalid file id"); |
|
293 else |
2669
|
294 { |
3523
|
295 std::string msg; |
2937
|
296 |
|
297 int status = octave_syscalls::fcntl (fid, req, arg, msg); |
2669
|
298 |
2800
|
299 retval(0) = static_cast<double> (status); |
2937
|
300 retval(1) = msg; |
2669
|
301 } |
2075
|
302 } |
|
303 else |
3202
|
304 error ("fcntl: file id, request, and argument must be integers"); |
2075
|
305 } |
|
306 else |
|
307 print_usage ("fcntl"); |
|
308 |
|
309 return retval; |
|
310 } |
|
311 |
2457
|
312 DEFUN (fork, args, , |
3301
|
313 "-*- texinfo -*-\n\ |
|
314 @deftypefn {Built-in Function} {[@var{pid}, @var{msg}] =} fork ()\n\ |
2669
|
315 Create a copy of the current process.\n\ |
|
316 \n\ |
3301
|
317 Fork can return one of the following values:\n\ |
|
318 \n\ |
|
319 @table @asis\n\ |
|
320 @item > 0\n\ |
|
321 You are in the parent process. The value returned from @code{fork} is\n\ |
|
322 the process id of the child process. You should probably arrange to\n\ |
|
323 wait for any child processes to exit.\n\ |
|
324 \n\ |
|
325 @item 0\n\ |
|
326 You are in the child process. You can call @code{exec} to start another\n\ |
|
327 process. If that fails, you should probably call @code{exit}.\n\ |
|
328 \n\ |
|
329 @item < 0\n\ |
|
330 The call to @code{fork} failed for some reason. You must take evasive\n\ |
|
331 action. A system dependent error message will be waiting in @var{msg}.\n\ |
|
332 @end table\n\ |
|
333 @end deftypefn") |
2075
|
334 { |
2669
|
335 octave_value_list retval; |
|
336 |
3523
|
337 retval(1) = std::string (); |
2669
|
338 retval(0) = -1.0; |
2075
|
339 |
|
340 int nargin = args.length (); |
|
341 |
|
342 if (nargin == 0) |
2475
|
343 { |
3523
|
344 std::string msg; |
2937
|
345 |
|
346 pid_t pid = octave_syscalls::fork (msg); |
2669
|
347 |
2800
|
348 retval(0) = static_cast<double> (pid); |
2937
|
349 retval(1) = msg; |
2475
|
350 } |
2075
|
351 else |
|
352 print_usage ("fork"); |
|
353 |
|
354 return retval; |
|
355 } |
|
356 |
2457
|
357 DEFUN (getpgrp, args, , |
3301
|
358 "-*- texinfo -*-\n\ |
|
359 @deftypefn {Built-in Function} {pgid =} getpgrp ()\n\ |
|
360 Return the process group id of the current process.\n\ |
|
361 @end deftypefn") |
2075
|
362 { |
2937
|
363 octave_value_list retval; |
|
364 |
3523
|
365 retval(1) = std::string (); |
2937
|
366 retval(0) = -1.0; |
2075
|
367 |
|
368 int nargin = args.length (); |
|
369 |
|
370 if (nargin == 0) |
2475
|
371 { |
3523
|
372 std::string msg; |
2937
|
373 |
|
374 retval(0) = static_cast<double> (octave_syscalls::getpgrp (msg)); |
|
375 retval(1) = msg; |
2475
|
376 } |
2075
|
377 else |
|
378 print_usage ("getpgrp"); |
|
379 |
|
380 return retval; |
|
381 } |
|
382 |
2457
|
383 DEFUN (getpid, args, , |
3301
|
384 "-*- texinfo -*-\n\ |
|
385 @deftypefn {Built-in Function} {pid =} getpid ()\n\ |
|
386 Return the process id of the current process.\n\ |
|
387 @end deftypefn") |
2075
|
388 { |
|
389 double retval = -1.0; |
|
390 |
|
391 int nargin = args.length (); |
|
392 |
|
393 if (nargin == 0) |
2937
|
394 retval = octave_syscalls::getpid (); |
2075
|
395 else |
|
396 print_usage ("getpid"); |
|
397 |
|
398 return retval; |
|
399 } |
|
400 |
2457
|
401 DEFUN (getppid, args, , |
3301
|
402 "-*- texinfo -*-\n\ |
|
403 @deftypefn {Built-in Function} {pid =} getppid ()\n\ |
|
404 Return the process id of the parent process.\n\ |
|
405 @end deftypefn") |
2075
|
406 { |
|
407 double retval = -1.0; |
|
408 |
2475
|
409 int nargin = args.length (); |
|
410 |
|
411 if (nargin == 0) |
2937
|
412 retval = octave_syscalls::getppid (); |
2475
|
413 else |
|
414 print_usage ("getppid"); |
|
415 |
|
416 return retval; |
|
417 } |
|
418 |
|
419 DEFUN (getegid, args, , |
3301
|
420 "-*- texinfo -*-\n\ |
|
421 @deftypefn {Built-in Function} {egid =} getegid ()\n\ |
|
422 Return the effective group id of the current process.\n\ |
|
423 @end deftypefn") |
2475
|
424 { |
|
425 double retval = -1.0; |
|
426 |
2075
|
427 int nargin = args.length (); |
|
428 |
|
429 if (nargin == 0) |
2937
|
430 retval = octave_syscalls::getegid (); |
2075
|
431 else |
2475
|
432 print_usage ("getegid"); |
|
433 |
|
434 return retval; |
|
435 } |
|
436 |
|
437 DEFUN (getgid, args, , |
3301
|
438 "-*- texinfo -*-\n\ |
|
439 @deftypefn {Built-in Function} {gid =} getgid ()\n\ |
|
440 Return the real group id of the current process.\n\ |
|
441 @end deftypefn") |
2475
|
442 { |
|
443 double retval = -1.0; |
|
444 |
|
445 int nargin = args.length (); |
|
446 |
|
447 if (nargin == 0) |
2937
|
448 retval = octave_syscalls::getgid (); |
2475
|
449 else |
|
450 print_usage ("getgid"); |
2075
|
451 |
|
452 return retval; |
|
453 } |
|
454 |
2473
|
455 DEFUN (geteuid, args, , |
3301
|
456 "-*- texinfo -*-\n\ |
|
457 @deftypefn {Built-in Function} {euid =} geteuid ()\n\ |
|
458 Return the effective user id of the current process.\n\ |
|
459 @end deftypefn") |
2472
|
460 { |
|
461 double retval = -1.0; |
|
462 |
|
463 int nargin = args.length (); |
|
464 |
|
465 if (nargin == 0) |
2937
|
466 retval = octave_syscalls::geteuid (); |
2472
|
467 else |
|
468 print_usage ("geteuid"); |
2473
|
469 |
|
470 return retval; |
2472
|
471 } |
|
472 |
2473
|
473 DEFUN (getuid, args, , |
3301
|
474 "-*- texinfo -*-\n\ |
|
475 @deftypefn {Built-in Function} {uid =} getuid ()\n\ |
|
476 Return the real user id of the current process.\n\ |
|
477 @end deftypefn") |
2472
|
478 { |
|
479 double retval = -1.0; |
|
480 |
|
481 int nargin = args.length (); |
|
482 |
|
483 if (nargin == 0) |
2937
|
484 retval = octave_syscalls::getuid (); |
2472
|
485 else |
|
486 print_usage ("getuid"); |
2473
|
487 |
|
488 return retval; |
2472
|
489 } |
|
490 |
2075
|
491 DEFUN (lstat, args, , |
3458
|
492 "-*- texinfo -*-\n\ |
|
493 @deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} lstat (@var{file})\n\ |
|
494 See stat.\n\ |
|
495 @end deftypefn") |
2075
|
496 { |
2263
|
497 octave_value_list retval; |
2075
|
498 |
|
499 if (args.length () == 1) |
|
500 { |
3523
|
501 std::string fname = file_ops::tilde_expand (args(0).string_value ()); |
2075
|
502 |
|
503 if (! error_state) |
|
504 { |
|
505 file_stat fs (fname, false); |
|
506 |
2263
|
507 if (fs) |
2262
|
508 { |
3523
|
509 retval(2) = std::string (); |
2262
|
510 retval(1) = 0.0; |
|
511 retval(0) = octave_value (mk_stat_map (fs)); |
|
512 } |
|
513 else |
|
514 { |
|
515 retval(2) = fs.error (); |
|
516 retval(1) = -1.0; |
|
517 retval(0) = Matrix (); |
|
518 } |
2075
|
519 } |
|
520 } |
|
521 else |
|
522 print_usage ("lstat"); |
|
523 |
|
524 return retval; |
|
525 } |
|
526 |
3301
|
527 |
|
528 |
2075
|
529 DEFUN (mkfifo, args, , |
3345
|
530 "-*- texinfo -*-\n\ |
|
531 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} mkfifo (@var{name})\n\ |
3301
|
532 Create a @var{fifo} special file named @var{name} with file mode @var{mode}\n\\n\ |
2075
|
533 \n\ |
3301
|
534 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
535 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
536 system-dependent error message.\n\ |
|
537 @end deftypefn") |
2075
|
538 { |
2669
|
539 octave_value_list retval; |
|
540 |
3523
|
541 retval(1) = std::string (); |
2669
|
542 retval(0) = -1.0; |
2075
|
543 |
|
544 int nargin = args.length (); |
|
545 |
|
546 if (nargin == 2) |
|
547 { |
|
548 if (args(0).is_string ()) |
|
549 { |
3523
|
550 std::string name = args(0).string_value (); |
2075
|
551 |
|
552 if (args(1).is_scalar_type ()) |
|
553 { |
2800
|
554 long mode = static_cast<long> (args(1).double_value ()); |
2075
|
555 |
3523
|
556 std::string msg; |
2669
|
557 |
2926
|
558 int status = file_ops::mkfifo (name, mode, msg); |
2669
|
559 |
2800
|
560 retval(0) = static_cast<double> (status); |
2669
|
561 |
|
562 if (status < 0) |
|
563 retval(1) = msg; |
2075
|
564 } |
|
565 else |
|
566 error ("mkfifo: MODE must be an integer"); |
|
567 } |
|
568 else |
|
569 error ("mkfifo: file name must be a string"); |
|
570 } |
|
571 else |
|
572 print_usage ("mkfifo"); |
|
573 |
|
574 return retval; |
|
575 } |
|
576 |
|
577 DEFUN (pipe, args, , |
3301
|
578 "-*- texinfo -*-\n\ |
|
579 @deftypefn {Built-in Function} {[@var{file_ids}, @var{err}, @var{msg}] =} pipe ()\n\ |
|
580 Create a pipe and return the vector @var{file_ids}, which corresponding\n\ |
|
581 to the reading and writing ends of the pipe.\n\ |
2669
|
582 \n\ |
3301
|
583 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
584 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
585 system-dependent error message.\n\ |
|
586 @end deftypefn") |
2075
|
587 { |
2669
|
588 octave_value_list retval; |
|
589 |
3523
|
590 retval(2) = std::string (); |
2669
|
591 retval(1) = -1.0; |
|
592 retval(0) = Matrix (); |
2075
|
593 |
|
594 int nargin = args.length (); |
|
595 |
|
596 if (nargin == 0) |
|
597 { |
|
598 int fid[2]; |
|
599 |
3523
|
600 std::string msg; |
2937
|
601 |
|
602 int status = octave_syscalls::pipe (fid, msg); |
2669
|
603 |
|
604 if (status < 0) |
2937
|
605 retval(2) = msg; |
2669
|
606 else |
2075
|
607 { |
3340
|
608 FILE *ifile = fdopen (fid[0], "r"); |
|
609 FILE *ofile = fdopen (fid[1], "w"); |
2075
|
610 |
3523
|
611 octave_stream is = octave_istdiostream::create (std::string (), ifile); |
|
612 octave_stream os = octave_ostdiostream::create (std::string (), ofile); |
2075
|
613 |
2902
|
614 octave_value_list file_ids; |
2075
|
615 |
2902
|
616 file_ids(1) = octave_stream_list::insert (os); |
|
617 file_ids(0) = octave_stream_list::insert (is); |
2075
|
618 |
2800
|
619 retval(1) = static_cast<double> (status); |
2902
|
620 retval(0) = octave_value (file_ids); |
2669
|
621 } |
2075
|
622 } |
|
623 else |
|
624 print_usage ("pipe"); |
|
625 |
|
626 return retval; |
|
627 } |
|
628 |
|
629 DEFUN (stat, args, , |
3301
|
630 "-*- texinfo -*-\n\ |
|
631 @deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} stat (@var{file})\n\ |
|
632 @deftypefnx {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} lstat (@var{file})\n\ |
|
633 Return a structure @var{s} containing the following information about\n\ |
|
634 @var{file}.\n\ |
|
635 \n\ |
|
636 @table @code\n\ |
|
637 @item dev\n\ |
|
638 ID of device containing a directory entry for this file.\n\ |
|
639 \n\ |
|
640 @item ino\n\ |
|
641 File number of the file.\n\ |
|
642 \n\ |
|
643 @item modestr\n\ |
|
644 File mode, as a string of ten letters or dashes as would be returned by\n\ |
|
645 @kbd{ls -l}.\n\ |
|
646 \n\ |
|
647 @item nlink\n\ |
|
648 Number of links.\n\ |
2075
|
649 \n\ |
3301
|
650 @item uid\n\ |
|
651 User ID of file's owner.\n\ |
|
652 \n\ |
|
653 @item gid\n\ |
|
654 Group ID of file's group.\n\ |
|
655 \n\ |
|
656 @item rdev\n\ |
|
657 ID of device for block or character special files.\n\ |
|
658 \n\ |
|
659 @item size\n\ |
|
660 Size in bytes.\n\ |
|
661 \n\ |
|
662 @item atime\n\ |
|
663 Time of last access in the same form as time values returned from\n\ |
|
664 @code{time}. @xref{Timing Utilities}.\n\ |
|
665 \n\ |
|
666 @item mtime\n\ |
|
667 Time of last modification in the same form as time values returned from\n\ |
|
668 @code{time}. @xref{Timing Utilities}.\n\ |
2075
|
669 \n\ |
3301
|
670 @item ctime\n\ |
|
671 Time of last file status change in the same form as time values\n\ |
|
672 returned from @code{time}. @xref{Timing Utilities}.\n\ |
|
673 \n\ |
|
674 @item blksize\n\ |
|
675 Size of blocks in the file.\n\ |
|
676 \n\ |
|
677 @item blocks\n\ |
|
678 Number of blocks allocated for file.\n\ |
|
679 @end table\n\ |
|
680 \n\ |
|
681 If the call is successful @var{err} is 0 and @var{msg} is an empty\n\ |
|
682 string. If the file does not exist, or some other error occurs, @var{s}\n\ |
|
683 is an empty matrix, @var{err} is @minus{}1, and @var{msg} contains the\n\ |
|
684 corresponding system error message.\n\ |
|
685 \n\ |
|
686 If @var{file} is a symbolic link, @code{stat} will return information\n\ |
|
687 about the actual file the is referenced by the link. Use @code{lstat}\n\ |
|
688 if you want information about the symbolic link itself.\n\ |
|
689 \n\ |
|
690 For example,\n\ |
2075
|
691 \n\ |
3301
|
692 @example\n\ |
|
693 @group\n\ |
|
694 [s, err, msg] = stat (\"/vmlinuz\")\n\ |
|
695 @result{} s =\n\ |
|
696 @{\n\ |
|
697 atime = 855399756\n\ |
|
698 rdev = 0\n\ |
|
699 ctime = 847219094\n\ |
|
700 uid = 0\n\ |
|
701 size = 389218\n\ |
|
702 blksize = 4096\n\ |
|
703 mtime = 847219094\n\ |
|
704 gid = 6\n\ |
|
705 nlink = 1\n\ |
|
706 blocks = 768\n\ |
|
707 modestr = -rw-r--r--\n\ |
|
708 ino = 9316\n\ |
|
709 dev = 2049\n\ |
|
710 @}\n\ |
|
711 @result{} err = 0\n\ |
|
712 @result{} msg = \n\ |
|
713 @end group\n\ |
|
714 @end example\n\ |
|
715 @end deftypefn") |
2075
|
716 { |
2262
|
717 octave_value_list retval; |
2075
|
718 |
|
719 if (args.length () == 1) |
|
720 { |
3523
|
721 std::string fname = file_ops::tilde_expand (args(0).string_value ()); |
2075
|
722 |
|
723 if (! error_state) |
|
724 { |
|
725 file_stat fs (fname); |
|
726 |
|
727 if (fs) |
2262
|
728 { |
3523
|
729 retval(2) = std::string (); |
2262
|
730 retval(1) = 0.0; |
|
731 retval(0) = octave_value (mk_stat_map (fs)); |
|
732 } |
|
733 else |
|
734 { |
|
735 retval(2) = fs.error (); |
|
736 retval(1) = -1.0; |
|
737 retval(0) = Matrix (); |
|
738 } |
2075
|
739 } |
|
740 } |
|
741 else |
|
742 print_usage ("stat"); |
|
743 |
|
744 return retval; |
|
745 } |
|
746 |
|
747 DEFUN (unlink, args, , |
3301
|
748 "-*- texinfo -*-\n\ |
|
749 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} unlink (@var{file})\n\ |
|
750 Delete the file named @var{file}.\n\ |
2075
|
751 \n\ |
3301
|
752 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
753 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
754 system-dependent error message.\n\ |
|
755 @end deftypefn") |
2075
|
756 { |
2669
|
757 octave_value_list retval; |
|
758 |
3523
|
759 retval(1) = std::string (); |
2669
|
760 retval(0) = -1.0; |
2075
|
761 |
|
762 int nargin = args.length (); |
|
763 |
|
764 if (nargin == 1) |
|
765 { |
|
766 if (args(0).is_string ()) |
|
767 { |
3523
|
768 std::string name = args(0).string_value (); |
2075
|
769 |
3523
|
770 std::string msg; |
2669
|
771 |
2926
|
772 int status = file_ops::unlink (name, msg); |
2669
|
773 |
2800
|
774 retval(0) = static_cast<double> (status); |
2937
|
775 retval(1) = msg; |
2075
|
776 } |
|
777 else |
|
778 error ("unlink: file name must be a string"); |
|
779 } |
|
780 else |
|
781 print_usage ("unlink"); |
|
782 |
|
783 return retval; |
|
784 } |
|
785 |
|
786 DEFUN (waitpid, args, , |
3301
|
787 "-*- texinfo -*-\n\ |
|
788 @deftypefn {Built-in Function} {[@var{pid}, @var{msg}] =} waitpid (@var{pid}, @var{options})\n\ |
|
789 Wait for process @var{pid} to terminate. The @var{pid} argument can be:\n\ |
2075
|
790 \n\ |
3301
|
791 @table @asis\n\ |
|
792 @item @minus{}1\n\ |
|
793 Wait for any child process.\n\ |
2075
|
794 \n\ |
3301
|
795 @item 0\n\ |
|
796 Wait for any child process whose process group ID is equal to that of\n\ |
|
797 the Octave interpreter process.\n\ |
2075
|
798 \n\ |
3301
|
799 @item > 0\n\ |
|
800 Wait for termination of the child process with ID @var{pid}.\n\ |
|
801 @end table\n\ |
|
802 \n\ |
|
803 The @var{options} argument can be:\n\ |
2075
|
804 \n\ |
3301
|
805 @table @asis\n\ |
|
806 @item 0\n\ |
|
807 Wait until signal is received or a child process exits (this is the\n\ |
|
808 default if the @var{options} argument is missing).\n\ |
|
809 \n\ |
|
810 @item 1\n\ |
|
811 Do not hang if status is not immediately available.\n\ |
2075
|
812 \n\ |
3301
|
813 @item 2\n\ |
|
814 Report the status of any child processes that are stopped, and whose\n\ |
|
815 status has not yet been reported since they stopped.\n\ |
|
816 \n\ |
|
817 @item 3\n\ |
|
818 Implies both 1 and 2.\n\ |
|
819 @end table\n\ |
|
820 \n\ |
|
821 If the returned value of @var{pid} is greater than 0, it is the process\n\ |
|
822 ID of the child process that exited. If an error occurs, @var{pid} will\n\ |
|
823 be less than zero and @var{msg} will contain a system-dependent error\n\ |
|
824 message.\n\ |
|
825 @end deftypefn") |
2075
|
826 { |
2669
|
827 octave_value_list retval; |
|
828 |
3523
|
829 retval(1) = std::string (); |
2669
|
830 retval(0) = -1.0; |
2075
|
831 |
|
832 int nargin = args.length (); |
|
833 |
|
834 if (nargin == 1 || nargin == 2) |
|
835 { |
3202
|
836 pid_t pid = args(0).int_value (true); |
2075
|
837 |
|
838 if (! error_state) |
|
839 { |
3202
|
840 int options = 0; |
2075
|
841 |
3202
|
842 if (args.length () == 2) |
|
843 { |
|
844 options = args(1).int_value (true); |
2075
|
845 |
|
846 if (! error_state) |
2669
|
847 { |
3202
|
848 if (options < 0 || options > 3) |
|
849 error ("waitpid: invalid OPTIONS value specified"); |
|
850 } |
|
851 else |
|
852 error ("waitpid: OPTIONS must be in integer"); |
|
853 } |
2937
|
854 |
3202
|
855 if (! error_state) |
|
856 { |
3523
|
857 std::string msg; |
2669
|
858 |
3202
|
859 pid_t status = octave_syscalls::waitpid (pid, options, msg); |
|
860 |
|
861 retval(0) = static_cast<double> (status); |
|
862 retval(1) = msg; |
2075
|
863 } |
|
864 } |
3202
|
865 else |
|
866 error ("waitpid: PID must be an integer value"); |
2075
|
867 } |
|
868 else |
|
869 print_usage ("waitpid"); |
|
870 |
|
871 return retval; |
|
872 } |
|
873 |
|
874 #if !defined (O_NONBLOCK) && defined (O_NDELAY) |
|
875 #define O_NONBLOCK O_NDELAY |
|
876 #endif |
|
877 |
|
878 void |
|
879 symbols_of_syscalls (void) |
|
880 { |
|
881 #if defined (F_DUPFD) |
3360
|
882 DEFCONSTX ("F_DUPFD", SBV_F_DUPFD, static_cast<double> (F_DUPFD), |
3446
|
883 "-*- texinfo -*-\n\ |
|
884 @defvr {Built-in Variable} F_DUPFD\n\ |
|
885 @end defvr"); |
2075
|
886 #endif |
|
887 |
|
888 #if defined (F_GETFD) |
3360
|
889 DEFCONSTX ("F_GETFD", SBV_F_GETFD, static_cast<double> (F_GETFD), |
3446
|
890 "-*- texinfo -*-\n\ |
|
891 @defvr {Built-in Variable} F_GETFD\n\ |
|
892 @end defvr"); |
2075
|
893 #endif |
|
894 |
|
895 #if defined (F_GETFL) |
3360
|
896 DEFCONSTX ("F_GETFL", SBV_F_GETFL, static_cast<double> (F_GETFL), |
3446
|
897 "-*- texinfo -*-\n\ |
|
898 @defvr {Built-in Variable} F_GETFL\n\ |
|
899 @end defvr"); |
2075
|
900 #endif |
|
901 |
|
902 #if defined (F_SETFD) |
3360
|
903 DEFCONSTX ("F_SETFD", SBV_F_SETFD, static_cast<double> (F_SETFD), |
3446
|
904 "-*- texinfo -*-\n\ |
|
905 @defvr {Built-in Variable} F_SETFD\n\ |
|
906 @end defvr"); |
2075
|
907 #endif |
|
908 |
|
909 #if defined (F_SETFL) |
3360
|
910 DEFCONSTX ("F_SETFL", SBV_F_SETFL, static_cast<double> (F_SETFL), |
3446
|
911 "-*- texinfo -*-\n\ |
|
912 @defvr {Built-in Variable} F_SETFL\n\ |
|
913 @end defvr"); |
2075
|
914 #endif |
|
915 |
|
916 #if defined (O_APPEND) |
3360
|
917 DEFCONSTX ("O_APPEND", SBV_O_APPEND, static_cast<double> (O_APPEND), |
3446
|
918 "-*- texinfo -*-\n\ |
|
919 @defvr {Built-in Variable} O_APPEND\n\ |
|
920 @end defvr"); |
2075
|
921 #endif |
|
922 |
2669
|
923 #if defined (O_ASYNC) |
3360
|
924 DEFCONSTX ("O_ASYNC", SBV_O_ASYNC, static_cast<double> (O_ASYNC), |
3446
|
925 "-*- texinfo -*-\n\ |
|
926 @defvr {Built-in Variable} O_ASYNC\n\ |
|
927 @end defvr"); |
2669
|
928 #endif |
|
929 |
2075
|
930 #if defined (O_CREAT) |
3360
|
931 DEFCONSTX ("O_CREAT", SBV_O_CREAT, static_cast<double> (O_CREAT), |
3446
|
932 "-*- texinfo -*-\n\ |
|
933 @defvr {Built-in Variable} O_CREAT\n\ |
|
934 @end defvr"); |
2075
|
935 #endif |
|
936 |
|
937 #if defined (O_EXCL) |
3360
|
938 DEFCONSTX ("O_EXCL", SBV_O_EXCL, static_cast<double> (O_EXCL), |
3446
|
939 "-*- texinfo -*-\n\ |
|
940 @defvr {Built-in Variable} O_EXCL\n\ |
|
941 @end defvr"); |
2075
|
942 #endif |
|
943 |
|
944 #if defined (O_NONBLOCK) |
3360
|
945 DEFCONSTX ("O_NONBLOCK", SBV_O_NONBLOCK, static_cast<double> (O_NONBLOCK), |
3446
|
946 "-*- texinfo -*-\n\ |
|
947 @defvr {Built-in Variable} O_NONBLOCK\n\ |
|
948 @end defvr"); |
2075
|
949 #endif |
|
950 |
|
951 #if defined (O_RDONLY) |
3360
|
952 DEFCONSTX ("O_RDONLY", SBV_O_RDONLY, static_cast<double> (O_RDONLY), |
3446
|
953 "-*- texinfo -*-\n\ |
|
954 @defvr {Built-in Variable} O_RDONLY\n\ |
|
955 @end defvr"); |
2075
|
956 #endif |
|
957 |
|
958 #if defined (O_RDWR) |
3360
|
959 DEFCONSTX ("O_RDWR", SBV_O_RDWR, static_cast<double> (O_RDWR), |
3446
|
960 "-*- texinfo -*-\n\ |
|
961 @defvr {Built-in Variable} O_RDWR\n\ |
|
962 @end defvr"); |
2075
|
963 #endif |
|
964 |
2669
|
965 #if defined (O_SYNC) |
3360
|
966 DEFCONSTX ("O_SYNC", SBV_O_SYNC, static_cast<double> (O_SYNC), |
3446
|
967 "-*- texinfo -*-\n\ |
|
968 @defvr {Built-in Variable} O_SYNC\n\ |
|
969 @end defvr"); |
2669
|
970 #endif |
|
971 |
2075
|
972 #if defined (O_TRUNC) |
3360
|
973 DEFCONSTX ("O_TRUNC", SBV_O_TRUNC, static_cast<double> (O_TRUNC), |
3446
|
974 "-*- texinfo -*-\n\ |
|
975 @defvr {Built-in Variable} O_TRUNC\n\ |
|
976 @end defvr"); |
2075
|
977 #endif |
|
978 |
|
979 #if defined (O_WRONLY) |
3360
|
980 DEFCONSTX ("O_WRONLY", SBV_O_WRONLY, static_cast<double> (O_WRONLY), |
3446
|
981 "-*- texinfo -*-\n\ |
|
982 @defvr {Built-in Variable} O_WRONLY\n\ |
|
983 @end defvr"); |
2075
|
984 #endif |
3446
|
985 |
2075
|
986 } |
|
987 |
|
988 /* |
|
989 ;;; Local Variables: *** |
|
990 ;;; mode: C++ *** |
|
991 ;;; End: *** |
|
992 */ |