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