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