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