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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2075
|
21 |
|
22 */ |
|
23 |
|
24 // Thomas Baier <baier@ci.tuwien.ac.at> added the original versions of |
|
25 // the following functions: |
|
26 // |
|
27 // mkfifo unlink waitpid |
|
28 |
|
29 #ifdef HAVE_CONFIG_H |
|
30 #include <config.h> |
|
31 #endif |
|
32 |
|
33 #include <cstdio> |
2669
|
34 #include <cstring> |
2075
|
35 |
|
36 #ifdef HAVE_UNISTD_H |
2442
|
37 #ifdef HAVE_SYS_TYPES_H |
2075
|
38 #include <sys/types.h> |
2442
|
39 #endif |
2075
|
40 #include <unistd.h> |
|
41 #endif |
|
42 |
|
43 #ifdef HAVE_FCNTL_H |
|
44 #include <fcntl.h> |
|
45 #endif |
|
46 |
2926
|
47 #include "file-ops.h" |
|
48 #include "file-stat.h" |
2937
|
49 #include "oct-syscalls.h" |
2926
|
50 |
2075
|
51 #include "defun.h" |
|
52 #include "error.h" |
2078
|
53 #include "gripes.h" |
2075
|
54 #include "lo-utils.h" |
|
55 #include "oct-map.h" |
|
56 #include "oct-obj.h" |
|
57 #include "oct-stdstrm.h" |
|
58 #include "oct-stream.h" |
|
59 #include "sysdep.h" |
|
60 #include "syswait.h" |
|
61 #include "utils.h" |
2366
|
62 #include "variables.h" |
2075
|
63 |
|
64 static Octave_map |
|
65 mk_stat_map (const file_stat& fs) |
|
66 { |
|
67 Octave_map m; |
|
68 |
4675
|
69 m.assign ("dev", static_cast<double> (fs.dev ())); |
|
70 m.assign ("ino", fs.ino ()); |
|
71 m.assign ("modestr", fs.mode_as_string ()); |
|
72 m.assign ("nlink", fs.nlink ()); |
|
73 m.assign ("uid", fs.uid ()); |
|
74 m.assign ("gid", fs.gid ()); |
3887
|
75 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
4675
|
76 m.assign ("rdev", static_cast<double> (fs.rdev ())); |
2075
|
77 #endif |
4675
|
78 m.assign ("size", fs.size ()); |
|
79 m.assign ("atime", fs.atime ()); |
|
80 m.assign ("mtime", fs.mtime ()); |
|
81 m.assign ("ctime", fs.ctime ()); |
3887
|
82 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
4675
|
83 m.assign ("blksize", fs.blksize ()); |
2075
|
84 #endif |
3887
|
85 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
4675
|
86 m.assign ("blocks", fs.blocks ()); |
2075
|
87 #endif |
|
88 |
|
89 return m; |
|
90 } |
|
91 |
2457
|
92 DEFUN (dup2, args, , |
3301
|
93 "-*- texinfo -*-\n\ |
|
94 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} dup2 (@var{old}, @var{new})\n\ |
2669
|
95 Duplicate a file descriptor.\n\ |
|
96 \n\ |
3301
|
97 If successful, @var{fid} is greater than zero and contains the new file\n\ |
|
98 ID. Otherwise, @var{fid} is negative and @var{msg} contains a\n\ |
|
99 system-dependent error message.\n\ |
|
100 @end deftypefn") |
2075
|
101 { |
2669
|
102 octave_value_list retval; |
|
103 |
3523
|
104 retval(1) = std::string (); |
4294
|
105 retval(0) = -1; |
2075
|
106 |
|
107 int nargin = args.length (); |
|
108 |
|
109 if (nargin == 2) |
|
110 { |
3341
|
111 octave_stream old_stream |
|
112 = octave_stream_list::lookup (args(0), "dup2"); |
2075
|
113 |
3341
|
114 if (! error_state) |
2075
|
115 { |
3341
|
116 octave_stream new_stream |
|
117 = octave_stream_list::lookup (args(1), "dup2"); |
2075
|
118 |
3341
|
119 if (! error_state) |
3145
|
120 { |
3341
|
121 int i_old = old_stream.file_number (); |
|
122 int i_new = new_stream.file_number (); |
2937
|
123 |
3341
|
124 if (i_old >= 0 && i_new >= 0) |
|
125 { |
3523
|
126 std::string msg; |
2669
|
127 |
3341
|
128 int status = octave_syscalls::dup2 (i_old, i_new, msg); |
|
129 |
4233
|
130 retval(0) = status; |
3341
|
131 retval(1) = msg; |
|
132 } |
2075
|
133 } |
|
134 } |
3145
|
135 else |
|
136 error ("dup2: invalid stream"); |
2075
|
137 } |
|
138 else |
|
139 print_usage ("dup2"); |
|
140 |
|
141 return retval; |
|
142 } |
|
143 |
2457
|
144 DEFUN (exec, args, , |
3301
|
145 "-*- texinfo -*-\n\ |
|
146 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} exec (@var{file}, @var{args})\n\ |
|
147 Replace current process with a new process. Calling @code{exec} without\n\ |
|
148 first calling @code{fork} will terminate your current Octave process and\n\ |
|
149 replace it with the program named by @var{file}. For example,\n\ |
2669
|
150 \n\ |
3301
|
151 @example\n\ |
|
152 exec (\"ls\" \"-l\")\n\ |
|
153 @end example\n\ |
2669
|
154 \n\ |
3301
|
155 @noindent\n\ |
|
156 will run @code{ls} and return you to your shell prompt.\n\ |
|
157 \n\ |
|
158 If successful, @code{exec} does not return. If @code{exec} does return,\n\ |
|
159 @var{err} will be nonzero, and @var{msg} will contain a system-dependent\n\ |
|
160 error message.\n\ |
|
161 @end deftypefn") |
2075
|
162 { |
2669
|
163 octave_value_list retval; |
|
164 |
3523
|
165 retval(1) = std::string (); |
4294
|
166 retval(0) = -1; |
2075
|
167 |
|
168 int nargin = args.length (); |
|
169 |
|
170 if (nargin == 1 || nargin == 2) |
|
171 { |
3523
|
172 std::string exec_file = args(0).string_value (); |
2075
|
173 |
|
174 if (! error_state) |
|
175 { |
2937
|
176 string_vector exec_args; |
2075
|
177 |
|
178 if (nargin == 2) |
|
179 { |
2937
|
180 string_vector tmp = args(1).all_strings (); |
2075
|
181 |
|
182 if (! error_state) |
|
183 { |
2937
|
184 int len = tmp.length (); |
2075
|
185 |
2937
|
186 exec_args.resize (len + 1); |
2075
|
187 |
2937
|
188 exec_args[0] = exec_file; |
2075
|
189 |
2937
|
190 for (int i = 0; i < len; i++) |
|
191 exec_args[i+1] = tmp[i]; |
2075
|
192 } |
|
193 else |
5138
|
194 error ("exec: arguments must be character strings"); |
2075
|
195 } |
|
196 else |
|
197 { |
2937
|
198 exec_args.resize (1); |
2075
|
199 |
2937
|
200 exec_args[0] = exec_file; |
2075
|
201 } |
|
202 |
|
203 if (! error_state) |
2669
|
204 { |
3523
|
205 std::string msg; |
2937
|
206 |
|
207 int status = octave_syscalls::execvp (exec_file, exec_args, msg); |
2669
|
208 |
4233
|
209 retval(0) = status; |
2937
|
210 retval(1) = msg; |
2669
|
211 } |
2075
|
212 } |
|
213 else |
|
214 error ("exec: first argument must be a string"); |
|
215 } |
|
216 else |
|
217 print_usage ("exec"); |
|
218 |
|
219 return retval; |
|
220 } |
|
221 |
2457
|
222 DEFUN (fcntl, args, , |
3301
|
223 "-*- texinfo -*-\n\ |
|
224 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} fcntl (@var{fid}, @var{request}, @var{arg})\n\ |
|
225 Change the properties of the open file @var{fid}. The following values\n\ |
|
226 may be passed as @var{request}:\n\ |
|
227 \n\ |
|
228 @vtable @code\n\ |
|
229 @item F_DUPFD\n\ |
|
230 Return a duplicate file descriptor.\n\ |
|
231 \n\ |
|
232 @item F_GETFD\n\ |
|
233 Return the file descriptor flags for @var{fid}.\n\ |
|
234 \n\ |
|
235 @item F_SETFD\n\ |
|
236 Set the file descriptor flags for @var{fid}.\n\ |
|
237 \n\ |
|
238 @item F_GETFL\n\ |
|
239 Return the file status flags for @var{fid}. The following codes may be\n\ |
|
240 returned (some of the flags may be undefined on some systems).\n\ |
|
241 \n\ |
|
242 @vtable @code\n\ |
|
243 @item O_RDONLY\n\ |
|
244 Open for reading only.\n\ |
|
245 \n\ |
|
246 @item O_WRONLY\n\ |
|
247 Open for writing only.\n\ |
2669
|
248 \n\ |
3301
|
249 @item O_RDWR\n\ |
|
250 Open for reading and writing.\n\ |
|
251 \n\ |
|
252 @item O_APPEND\n\ |
|
253 Append on each write.\n\ |
|
254 \n\ |
5040
|
255 @item O_CREAT\n\ |
|
256 Create the file if it does not exist.\n\ |
|
257 \n\ |
3301
|
258 @item O_NONBLOCK\n\ |
|
259 Nonblocking mode.\n\ |
|
260 \n\ |
|
261 @item O_SYNC\n\ |
|
262 Wait for writes to complete.\n\ |
2669
|
263 \n\ |
3301
|
264 @item O_ASYNC\n\ |
|
265 Asynchronous I/O.\n\ |
|
266 @end vtable\n\ |
|
267 \n\ |
|
268 @item F_SETFL\n\ |
|
269 Set the file status flags for @var{fid} to the value specified by\n\ |
|
270 @var{arg}. The only flags that can be changed are @code{O_APPEND} and\n\ |
|
271 @code{O_NONBLOCK}.\n\ |
|
272 @end vtable\n\ |
|
273 \n\ |
|
274 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
275 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
276 system-dependent error message.\n\ |
|
277 @end deftypefn") |
2075
|
278 { |
2669
|
279 octave_value_list retval; |
|
280 |
3523
|
281 retval(1) = std::string (); |
4294
|
282 retval(0) = -1; |
2075
|
283 |
|
284 int nargin = args.length (); |
|
285 |
|
286 if (nargin == 3) |
|
287 { |
3715
|
288 octave_stream strm = octave_stream_list::lookup (args (0), "fcntl"); |
2075
|
289 |
3202
|
290 if (! error_state) |
2075
|
291 { |
3715
|
292 int fid = strm.file_number (); |
|
293 |
|
294 int req = args(1).int_value (true); |
|
295 int arg = args(2).int_value (true); |
|
296 |
|
297 if (! error_state) |
2669
|
298 { |
3715
|
299 // XXX FIXME XXX -- Need better checking here? |
|
300 if (fid < 0) |
|
301 error ("fcntl: invalid file id"); |
|
302 else |
|
303 { |
|
304 std::string msg; |
2937
|
305 |
3715
|
306 int status = octave_syscalls::fcntl (fid, req, arg, msg); |
2669
|
307 |
4233
|
308 retval(0) = status; |
3715
|
309 retval(1) = msg; |
|
310 } |
2669
|
311 } |
2075
|
312 } |
|
313 else |
3202
|
314 error ("fcntl: file id, request, and argument must be integers"); |
2075
|
315 } |
|
316 else |
|
317 print_usage ("fcntl"); |
|
318 |
|
319 return retval; |
|
320 } |
|
321 |
2457
|
322 DEFUN (fork, args, , |
3301
|
323 "-*- texinfo -*-\n\ |
|
324 @deftypefn {Built-in Function} {[@var{pid}, @var{msg}] =} fork ()\n\ |
2669
|
325 Create a copy of the current process.\n\ |
|
326 \n\ |
3301
|
327 Fork can return one of the following values:\n\ |
|
328 \n\ |
|
329 @table @asis\n\ |
|
330 @item > 0\n\ |
|
331 You are in the parent process. The value returned from @code{fork} is\n\ |
|
332 the process id of the child process. You should probably arrange to\n\ |
|
333 wait for any child processes to exit.\n\ |
|
334 \n\ |
|
335 @item 0\n\ |
|
336 You are in the child process. You can call @code{exec} to start another\n\ |
|
337 process. If that fails, you should probably call @code{exit}.\n\ |
|
338 \n\ |
|
339 @item < 0\n\ |
|
340 The call to @code{fork} failed for some reason. You must take evasive\n\ |
|
341 action. A system dependent error message will be waiting in @var{msg}.\n\ |
|
342 @end table\n\ |
|
343 @end deftypefn") |
2075
|
344 { |
2669
|
345 octave_value_list retval; |
|
346 |
3523
|
347 retval(1) = std::string (); |
4294
|
348 retval(0) = -1; |
2075
|
349 |
|
350 int nargin = args.length (); |
|
351 |
|
352 if (nargin == 0) |
2475
|
353 { |
3523
|
354 std::string msg; |
2937
|
355 |
|
356 pid_t pid = octave_syscalls::fork (msg); |
2669
|
357 |
4233
|
358 retval(0) = pid; |
2937
|
359 retval(1) = msg; |
2475
|
360 } |
2075
|
361 else |
|
362 print_usage ("fork"); |
|
363 |
|
364 return retval; |
|
365 } |
|
366 |
2457
|
367 DEFUN (getpgrp, args, , |
3301
|
368 "-*- texinfo -*-\n\ |
|
369 @deftypefn {Built-in Function} {pgid =} getpgrp ()\n\ |
|
370 Return the process group id of the current process.\n\ |
|
371 @end deftypefn") |
2075
|
372 { |
2937
|
373 octave_value_list retval; |
|
374 |
3523
|
375 retval(1) = std::string (); |
4294
|
376 retval(0) = -1; |
2075
|
377 |
|
378 int nargin = args.length (); |
|
379 |
|
380 if (nargin == 0) |
2475
|
381 { |
3523
|
382 std::string msg; |
2937
|
383 |
4233
|
384 retval(0) = octave_syscalls::getpgrp (msg); |
2937
|
385 retval(1) = msg; |
2475
|
386 } |
2075
|
387 else |
|
388 print_usage ("getpgrp"); |
|
389 |
|
390 return retval; |
|
391 } |
|
392 |
2457
|
393 DEFUN (getpid, args, , |
3301
|
394 "-*- texinfo -*-\n\ |
|
395 @deftypefn {Built-in Function} {pid =} getpid ()\n\ |
|
396 Return the process id of the current process.\n\ |
|
397 @end deftypefn") |
2075
|
398 { |
4233
|
399 octave_value retval = -1; |
2075
|
400 |
|
401 int nargin = args.length (); |
|
402 |
|
403 if (nargin == 0) |
2937
|
404 retval = octave_syscalls::getpid (); |
2075
|
405 else |
|
406 print_usage ("getpid"); |
|
407 |
|
408 return retval; |
|
409 } |
|
410 |
2457
|
411 DEFUN (getppid, args, , |
3301
|
412 "-*- texinfo -*-\n\ |
|
413 @deftypefn {Built-in Function} {pid =} getppid ()\n\ |
|
414 Return the process id of the parent process.\n\ |
|
415 @end deftypefn") |
2075
|
416 { |
4233
|
417 octave_value retval = -1; |
2075
|
418 |
2475
|
419 int nargin = args.length (); |
|
420 |
|
421 if (nargin == 0) |
2937
|
422 retval = octave_syscalls::getppid (); |
2475
|
423 else |
|
424 print_usage ("getppid"); |
|
425 |
|
426 return retval; |
|
427 } |
|
428 |
|
429 DEFUN (getegid, args, , |
3301
|
430 "-*- texinfo -*-\n\ |
|
431 @deftypefn {Built-in Function} {egid =} getegid ()\n\ |
|
432 Return the effective group id of the current process.\n\ |
|
433 @end deftypefn") |
2475
|
434 { |
4233
|
435 octave_value retval = -1; |
2475
|
436 |
2075
|
437 int nargin = args.length (); |
|
438 |
|
439 if (nargin == 0) |
4254
|
440 retval = octave_syscalls::getegid (); |
2075
|
441 else |
2475
|
442 print_usage ("getegid"); |
|
443 |
|
444 return retval; |
|
445 } |
|
446 |
|
447 DEFUN (getgid, args, , |
3301
|
448 "-*- texinfo -*-\n\ |
|
449 @deftypefn {Built-in Function} {gid =} getgid ()\n\ |
|
450 Return the real group id of the current process.\n\ |
|
451 @end deftypefn") |
2475
|
452 { |
4233
|
453 octave_value retval = -1; |
2475
|
454 |
|
455 int nargin = args.length (); |
|
456 |
|
457 if (nargin == 0) |
4254
|
458 retval = octave_syscalls::getgid (); |
2475
|
459 else |
|
460 print_usage ("getgid"); |
2075
|
461 |
|
462 return retval; |
|
463 } |
|
464 |
2473
|
465 DEFUN (geteuid, args, , |
3301
|
466 "-*- texinfo -*-\n\ |
|
467 @deftypefn {Built-in Function} {euid =} geteuid ()\n\ |
|
468 Return the effective user id of the current process.\n\ |
|
469 @end deftypefn") |
2472
|
470 { |
4233
|
471 octave_value retval = -1; |
2472
|
472 |
|
473 int nargin = args.length (); |
|
474 |
|
475 if (nargin == 0) |
4254
|
476 retval = octave_syscalls::geteuid (); |
2472
|
477 else |
|
478 print_usage ("geteuid"); |
2473
|
479 |
|
480 return retval; |
2472
|
481 } |
|
482 |
2473
|
483 DEFUN (getuid, args, , |
3301
|
484 "-*- texinfo -*-\n\ |
|
485 @deftypefn {Built-in Function} {uid =} getuid ()\n\ |
|
486 Return the real user id of the current process.\n\ |
|
487 @end deftypefn") |
2472
|
488 { |
4233
|
489 octave_value retval = -1; |
2472
|
490 |
|
491 int nargin = args.length (); |
|
492 |
|
493 if (nargin == 0) |
4254
|
494 retval = octave_syscalls::getuid (); |
2472
|
495 else |
|
496 print_usage ("getuid"); |
2473
|
497 |
|
498 return retval; |
2472
|
499 } |
|
500 |
4294
|
501 DEFUN (kill, args, , |
4371
|
502 "-*- texinfo -*-\n\ |
4294
|
503 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} kill (@var{pid}, @var{sig})\n\ |
|
504 Send signal @var{sig} to process @var{pid}.\n\ |
|
505 \n\ |
|
506 If @var{pid} is positive, then signal @var{sig} is sent to @var{pid}.\n\ |
|
507 \n\ |
|
508 If @var{pid} is 0, then signal @var{sig} is sent to every process\n\ |
|
509 in the process group of the current process.\n\ |
|
510 \n\ |
|
511 If @var{pid} is -1, then signal @var{sig} is sent to every process\n\ |
|
512 except process 1.\n\ |
|
513 \n\ |
|
514 If @var{pid} is less than -1, then signal @var{sig} is sent to every\n\ |
|
515 process in the process group @var{-pid}.\n\ |
|
516 \n\ |
4371
|
517 If @var{sig} is 0, then no signal is sent, but error checking is still\n\ |
4294
|
518 performed.\n\ |
|
519 \n\ |
|
520 Return 0 if sucessful, otherwise return -1.\n\ |
|
521 @end deftypefn") |
|
522 { |
|
523 octave_value_list retval; |
|
524 |
|
525 retval(1) = std::string (); |
|
526 retval(0) = -1; |
|
527 |
|
528 if (args.length () == 2) |
|
529 { |
|
530 pid_t pid = args(0).int_value (true); |
|
531 |
|
532 if (! error_state) |
|
533 { |
|
534 int sig = args(1).int_value (true); |
|
535 |
|
536 if (! error_state) |
|
537 { |
|
538 std::string msg; |
|
539 |
|
540 int status = octave_syscalls::kill (pid, sig, msg); |
|
541 |
|
542 retval(1) = msg; |
|
543 retval(0) = status; |
|
544 } |
|
545 } |
|
546 } |
|
547 else |
|
548 print_usage ("kill"); |
|
549 |
|
550 return retval; |
|
551 } |
|
552 |
2075
|
553 DEFUN (lstat, args, , |
3458
|
554 "-*- texinfo -*-\n\ |
|
555 @deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} lstat (@var{file})\n\ |
|
556 See stat.\n\ |
|
557 @end deftypefn") |
2075
|
558 { |
2263
|
559 octave_value_list retval; |
2075
|
560 |
|
561 if (args.length () == 1) |
|
562 { |
3523
|
563 std::string fname = file_ops::tilde_expand (args(0).string_value ()); |
2075
|
564 |
|
565 if (! error_state) |
|
566 { |
|
567 file_stat fs (fname, false); |
|
568 |
2263
|
569 if (fs) |
2262
|
570 { |
3523
|
571 retval(2) = std::string (); |
4294
|
572 retval(1) = 0; |
4233
|
573 retval(0) = mk_stat_map (fs); |
2262
|
574 } |
|
575 else |
|
576 { |
|
577 retval(2) = fs.error (); |
4294
|
578 retval(1) = -1; |
2262
|
579 retval(0) = Matrix (); |
|
580 } |
2075
|
581 } |
|
582 } |
|
583 else |
|
584 print_usage ("lstat"); |
|
585 |
|
586 return retval; |
|
587 } |
|
588 |
3301
|
589 |
|
590 |
2075
|
591 DEFUN (mkfifo, args, , |
3345
|
592 "-*- texinfo -*-\n\ |
4825
|
593 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} mkfifo (@var{name}, @var{mode})\n\ |
4928
|
594 Create a @var{fifo} special file named @var{name} with file mode @var{mode}\n\ |
2075
|
595 \n\ |
3301
|
596 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
597 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
598 system-dependent error message.\n\ |
|
599 @end deftypefn") |
2075
|
600 { |
2669
|
601 octave_value_list retval; |
|
602 |
3523
|
603 retval(1) = std::string (); |
4294
|
604 retval(0) = -1; |
2075
|
605 |
|
606 int nargin = args.length (); |
|
607 |
|
608 if (nargin == 2) |
|
609 { |
|
610 if (args(0).is_string ()) |
|
611 { |
3523
|
612 std::string name = args(0).string_value (); |
2075
|
613 |
|
614 if (args(1).is_scalar_type ()) |
|
615 { |
4254
|
616 long mode = args(1).long_value (); |
2075
|
617 |
4254
|
618 if (! error_state) |
|
619 { |
|
620 std::string msg; |
|
621 |
|
622 int status = file_ops::mkfifo (name, mode, msg); |
2669
|
623 |
4254
|
624 retval(0) = status; |
2669
|
625 |
4254
|
626 if (status < 0) |
|
627 retval(1) = msg; |
|
628 } |
|
629 else |
|
630 error ("mkfifo: invalid MODE"); |
2075
|
631 } |
|
632 else |
|
633 error ("mkfifo: MODE must be an integer"); |
|
634 } |
|
635 else |
|
636 error ("mkfifo: file name must be a string"); |
|
637 } |
|
638 else |
|
639 print_usage ("mkfifo"); |
|
640 |
|
641 return retval; |
|
642 } |
|
643 |
|
644 DEFUN (pipe, args, , |
3301
|
645 "-*- texinfo -*-\n\ |
|
646 @deftypefn {Built-in Function} {[@var{file_ids}, @var{err}, @var{msg}] =} pipe ()\n\ |
|
647 Create a pipe and return the vector @var{file_ids}, which corresponding\n\ |
|
648 to the reading and writing ends of the pipe.\n\ |
2669
|
649 \n\ |
3301
|
650 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
651 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
652 system-dependent error message.\n\ |
|
653 @end deftypefn") |
2075
|
654 { |
2669
|
655 octave_value_list retval; |
|
656 |
3523
|
657 retval(2) = std::string (); |
4294
|
658 retval(1) = -1; |
2669
|
659 retval(0) = Matrix (); |
2075
|
660 |
|
661 int nargin = args.length (); |
|
662 |
|
663 if (nargin == 0) |
|
664 { |
|
665 int fid[2]; |
|
666 |
3523
|
667 std::string msg; |
2937
|
668 |
|
669 int status = octave_syscalls::pipe (fid, msg); |
2669
|
670 |
|
671 if (status < 0) |
2937
|
672 retval(2) = msg; |
2669
|
673 else |
2075
|
674 { |
3340
|
675 FILE *ifile = fdopen (fid[0], "r"); |
|
676 FILE *ofile = fdopen (fid[1], "w"); |
2075
|
677 |
4327
|
678 std::string nm; |
|
679 |
|
680 octave_stream is = octave_stdiostream::create (nm, ifile, |
|
681 std::ios::in); |
|
682 |
|
683 octave_stream os = octave_stdiostream::create (nm, ofile, |
|
684 std::ios::out); |
2075
|
685 |
2902
|
686 octave_value_list file_ids; |
2075
|
687 |
2902
|
688 file_ids(1) = octave_stream_list::insert (os); |
|
689 file_ids(0) = octave_stream_list::insert (is); |
2075
|
690 |
4233
|
691 retval(1) = status; |
2902
|
692 retval(0) = octave_value (file_ids); |
2669
|
693 } |
2075
|
694 } |
|
695 else |
|
696 print_usage ("pipe"); |
|
697 |
|
698 return retval; |
|
699 } |
|
700 |
|
701 DEFUN (stat, args, , |
3301
|
702 "-*- texinfo -*-\n\ |
|
703 @deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} stat (@var{file})\n\ |
|
704 @deftypefnx {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} lstat (@var{file})\n\ |
|
705 Return a structure @var{s} containing the following information about\n\ |
|
706 @var{file}.\n\ |
|
707 \n\ |
|
708 @table @code\n\ |
|
709 @item dev\n\ |
|
710 ID of device containing a directory entry for this file.\n\ |
|
711 \n\ |
|
712 @item ino\n\ |
|
713 File number of the file.\n\ |
|
714 \n\ |
|
715 @item modestr\n\ |
|
716 File mode, as a string of ten letters or dashes as would be returned by\n\ |
|
717 @kbd{ls -l}.\n\ |
|
718 \n\ |
|
719 @item nlink\n\ |
|
720 Number of links.\n\ |
2075
|
721 \n\ |
3301
|
722 @item uid\n\ |
|
723 User ID of file's owner.\n\ |
|
724 \n\ |
|
725 @item gid\n\ |
|
726 Group ID of file's group.\n\ |
|
727 \n\ |
|
728 @item rdev\n\ |
|
729 ID of device for block or character special files.\n\ |
|
730 \n\ |
|
731 @item size\n\ |
|
732 Size in bytes.\n\ |
|
733 \n\ |
|
734 @item atime\n\ |
|
735 Time of last access in the same form as time values returned from\n\ |
|
736 @code{time}. @xref{Timing Utilities}.\n\ |
|
737 \n\ |
|
738 @item mtime\n\ |
|
739 Time of last modification in the same form as time values returned from\n\ |
|
740 @code{time}. @xref{Timing Utilities}.\n\ |
2075
|
741 \n\ |
3301
|
742 @item ctime\n\ |
|
743 Time of last file status change in the same form as time values\n\ |
|
744 returned from @code{time}. @xref{Timing Utilities}.\n\ |
|
745 \n\ |
|
746 @item blksize\n\ |
|
747 Size of blocks in the file.\n\ |
|
748 \n\ |
|
749 @item blocks\n\ |
|
750 Number of blocks allocated for file.\n\ |
|
751 @end table\n\ |
|
752 \n\ |
|
753 If the call is successful @var{err} is 0 and @var{msg} is an empty\n\ |
|
754 string. If the file does not exist, or some other error occurs, @var{s}\n\ |
|
755 is an empty matrix, @var{err} is @minus{}1, and @var{msg} contains the\n\ |
|
756 corresponding system error message.\n\ |
|
757 \n\ |
|
758 If @var{file} is a symbolic link, @code{stat} will return information\n\ |
|
759 about the actual file the is referenced by the link. Use @code{lstat}\n\ |
|
760 if you want information about the symbolic link itself.\n\ |
|
761 \n\ |
|
762 For example,\n\ |
2075
|
763 \n\ |
3301
|
764 @example\n\ |
|
765 @group\n\ |
|
766 [s, err, msg] = stat (\"/vmlinuz\")\n\ |
|
767 @result{} s =\n\ |
|
768 @{\n\ |
|
769 atime = 855399756\n\ |
|
770 rdev = 0\n\ |
|
771 ctime = 847219094\n\ |
|
772 uid = 0\n\ |
|
773 size = 389218\n\ |
|
774 blksize = 4096\n\ |
|
775 mtime = 847219094\n\ |
|
776 gid = 6\n\ |
|
777 nlink = 1\n\ |
|
778 blocks = 768\n\ |
|
779 modestr = -rw-r--r--\n\ |
|
780 ino = 9316\n\ |
|
781 dev = 2049\n\ |
|
782 @}\n\ |
|
783 @result{} err = 0\n\ |
|
784 @result{} msg = \n\ |
|
785 @end group\n\ |
|
786 @end example\n\ |
|
787 @end deftypefn") |
2075
|
788 { |
2262
|
789 octave_value_list retval; |
2075
|
790 |
|
791 if (args.length () == 1) |
|
792 { |
3523
|
793 std::string fname = file_ops::tilde_expand (args(0).string_value ()); |
2075
|
794 |
|
795 if (! error_state) |
|
796 { |
|
797 file_stat fs (fname); |
|
798 |
|
799 if (fs) |
2262
|
800 { |
3523
|
801 retval(2) = std::string (); |
4294
|
802 retval(1) = 0; |
2262
|
803 retval(0) = octave_value (mk_stat_map (fs)); |
|
804 } |
|
805 else |
|
806 { |
|
807 retval(2) = fs.error (); |
4294
|
808 retval(1) = -1; |
2262
|
809 retval(0) = Matrix (); |
|
810 } |
2075
|
811 } |
|
812 } |
|
813 else |
|
814 print_usage ("stat"); |
|
815 |
|
816 return retval; |
|
817 } |
|
818 |
|
819 DEFUN (unlink, args, , |
3301
|
820 "-*- texinfo -*-\n\ |
|
821 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} unlink (@var{file})\n\ |
|
822 Delete the file named @var{file}.\n\ |
2075
|
823 \n\ |
3301
|
824 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
825 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
826 system-dependent error message.\n\ |
|
827 @end deftypefn") |
2075
|
828 { |
2669
|
829 octave_value_list retval; |
|
830 |
3523
|
831 retval(1) = std::string (); |
4294
|
832 retval(0) = -1; |
2075
|
833 |
|
834 int nargin = args.length (); |
|
835 |
|
836 if (nargin == 1) |
|
837 { |
|
838 if (args(0).is_string ()) |
|
839 { |
3523
|
840 std::string name = args(0).string_value (); |
2075
|
841 |
3523
|
842 std::string msg; |
2669
|
843 |
2926
|
844 int status = file_ops::unlink (name, msg); |
2669
|
845 |
4233
|
846 retval(0) = status; |
2937
|
847 retval(1) = msg; |
2075
|
848 } |
|
849 else |
|
850 error ("unlink: file name must be a string"); |
|
851 } |
|
852 else |
|
853 print_usage ("unlink"); |
|
854 |
|
855 return retval; |
|
856 } |
|
857 |
|
858 DEFUN (waitpid, args, , |
3301
|
859 "-*- texinfo -*-\n\ |
|
860 @deftypefn {Built-in Function} {[@var{pid}, @var{msg}] =} waitpid (@var{pid}, @var{options})\n\ |
|
861 Wait for process @var{pid} to terminate. The @var{pid} argument can be:\n\ |
2075
|
862 \n\ |
3301
|
863 @table @asis\n\ |
|
864 @item @minus{}1\n\ |
|
865 Wait for any child process.\n\ |
2075
|
866 \n\ |
3301
|
867 @item 0\n\ |
|
868 Wait for any child process whose process group ID is equal to that of\n\ |
|
869 the Octave interpreter process.\n\ |
2075
|
870 \n\ |
3301
|
871 @item > 0\n\ |
|
872 Wait for termination of the child process with ID @var{pid}.\n\ |
|
873 @end table\n\ |
|
874 \n\ |
|
875 The @var{options} argument can be:\n\ |
2075
|
876 \n\ |
3301
|
877 @table @asis\n\ |
|
878 @item 0\n\ |
|
879 Wait until signal is received or a child process exits (this is the\n\ |
|
880 default if the @var{options} argument is missing).\n\ |
|
881 \n\ |
|
882 @item 1\n\ |
|
883 Do not hang if status is not immediately available.\n\ |
2075
|
884 \n\ |
3301
|
885 @item 2\n\ |
|
886 Report the status of any child processes that are stopped, and whose\n\ |
|
887 status has not yet been reported since they stopped.\n\ |
|
888 \n\ |
|
889 @item 3\n\ |
|
890 Implies both 1 and 2.\n\ |
|
891 @end table\n\ |
|
892 \n\ |
|
893 If the returned value of @var{pid} is greater than 0, it is the process\n\ |
|
894 ID of the child process that exited. If an error occurs, @var{pid} will\n\ |
|
895 be less than zero and @var{msg} will contain a system-dependent error\n\ |
|
896 message.\n\ |
|
897 @end deftypefn") |
2075
|
898 { |
2669
|
899 octave_value_list retval; |
|
900 |
3523
|
901 retval(1) = std::string (); |
4294
|
902 retval(0) = -1; |
2075
|
903 |
|
904 int nargin = args.length (); |
|
905 |
|
906 if (nargin == 1 || nargin == 2) |
|
907 { |
3202
|
908 pid_t pid = args(0).int_value (true); |
2075
|
909 |
|
910 if (! error_state) |
|
911 { |
3202
|
912 int options = 0; |
2075
|
913 |
3202
|
914 if (args.length () == 2) |
|
915 { |
|
916 options = args(1).int_value (true); |
2075
|
917 |
|
918 if (! error_state) |
2669
|
919 { |
3202
|
920 if (options < 0 || options > 3) |
|
921 error ("waitpid: invalid OPTIONS value specified"); |
|
922 } |
|
923 else |
|
924 error ("waitpid: OPTIONS must be in integer"); |
|
925 } |
2937
|
926 |
3202
|
927 if (! error_state) |
|
928 { |
3523
|
929 std::string msg; |
2669
|
930 |
3202
|
931 pid_t status = octave_syscalls::waitpid (pid, options, msg); |
|
932 |
4233
|
933 retval(0) = status; |
3202
|
934 retval(1) = msg; |
2075
|
935 } |
|
936 } |
3202
|
937 else |
|
938 error ("waitpid: PID must be an integer value"); |
2075
|
939 } |
|
940 else |
|
941 print_usage ("waitpid"); |
|
942 |
|
943 return retval; |
|
944 } |
|
945 |
5138
|
946 DEFUN (canonicalize_file_name, args, , |
|
947 "-*- texinfo -*-\n\ |
|
948 @deftypefn {Built-in Function} {[@var{cname}, @var{status}, @var{msg}]} canonicalize_file_name (@var{name})\n\ |
|
949 Return the canonical name of file @var{name}.\n\ |
|
950 @end deftypefn") |
|
951 { |
|
952 octave_value_list retval; |
|
953 |
|
954 if (args.length () == 1) |
|
955 { |
|
956 std::string name = args(0).string_value (); |
|
957 |
|
958 if (! error_state) |
|
959 { |
|
960 std::string msg; |
|
961 |
|
962 std::string result = file_ops::canonicalize_file_name (name, msg); |
|
963 |
|
964 retval(2) = msg; |
|
965 retval(1) = msg.empty () ? 0 : -1; |
|
966 retval(0) = result; |
|
967 } |
|
968 else |
|
969 error ("canonicalize_file_name: argument must be a character string"); |
|
970 } |
|
971 else |
|
972 print_usage ("canonicalize_file_name"); |
|
973 |
|
974 return retval; |
|
975 } |
|
976 |
2075
|
977 #if !defined (O_NONBLOCK) && defined (O_NDELAY) |
|
978 #define O_NONBLOCK O_NDELAY |
|
979 #endif |
|
980 |
|
981 void |
|
982 symbols_of_syscalls (void) |
|
983 { |
|
984 #if defined (F_DUPFD) |
4233
|
985 DEFCONSTX ("F_DUPFD", SBV_F_DUPFD, F_DUPFD, |
3446
|
986 "-*- texinfo -*-\n\ |
5333
|
987 @defvr {Built-in Constant} F_DUPFD\n\ |
5040
|
988 Request to @code{fcntl} to return a duplicate file descriptor.\n\ |
5333
|
989 @seealso{fcntl, F_GETFD, F_GETFL, F_SETFD, F_SETFL}\n\ |
3446
|
990 @end defvr"); |
2075
|
991 #endif |
|
992 |
|
993 #if defined (F_GETFD) |
4233
|
994 DEFCONSTX ("F_GETFD", SBV_F_GETFD, F_GETFD, |
3446
|
995 "-*- texinfo -*-\n\ |
5333
|
996 @defvr {Built-in Constant} F_GETFD\n\ |
5040
|
997 Request to @code{fcntl} to return the file descriptor flags.\n\ |
5333
|
998 @seealso{fcntl, F_DUPFD, F_GETFL, F_SETFD, F_SETFL}\n\ |
3446
|
999 @end defvr"); |
2075
|
1000 #endif |
|
1001 |
|
1002 #if defined (F_GETFL) |
4233
|
1003 DEFCONSTX ("F_GETFL", SBV_F_GETFL, F_GETFL, |
3446
|
1004 "-*- texinfo -*-\n\ |
5333
|
1005 @defvr {Built-in Constant} F_GETFL\n\ |
5040
|
1006 Request to @code{fcntl} to return the file status flags.\n\ |
5333
|
1007 @seealso{fcntl, F_DUPFD, F_GETFD, F_SETFD, F_SETFL}\n\ |
3446
|
1008 @end defvr"); |
2075
|
1009 #endif |
|
1010 |
|
1011 #if defined (F_SETFD) |
4233
|
1012 DEFCONSTX ("F_SETFD", SBV_F_SETFD, F_SETFD, |
3446
|
1013 "-*- texinfo -*-\n\ |
5333
|
1014 @defvr {Built-in Constant} F_SETFD\n\ |
5040
|
1015 Request to @code{fcntl} to set the file descriptor flags.\n\ |
5333
|
1016 @seealso{fcntl, F_DUPFD, F_GETFD, F_GETFL, F_SETFL}\n\ |
3446
|
1017 @end defvr"); |
2075
|
1018 #endif |
|
1019 |
|
1020 #if defined (F_SETFL) |
4233
|
1021 DEFCONSTX ("F_SETFL", SBV_F_SETFL, F_SETFL, |
3446
|
1022 "-*- texinfo -*-\n\ |
5333
|
1023 @defvr {Built-in Constant} F_SETFL\n\ |
5040
|
1024 Request to @code{fcntl} to set the file status flags.\n\ |
5333
|
1025 @seealso{fcntl, F_DUPFD, F_GETFD, F_GETFL, F_SETFD}\n\ |
3446
|
1026 @end defvr"); |
2075
|
1027 #endif |
|
1028 |
|
1029 #if defined (O_APPEND) |
4233
|
1030 DEFCONSTX ("O_APPEND", SBV_O_APPEND, O_APPEND, |
3446
|
1031 "-*- texinfo -*-\n\ |
5333
|
1032 @defvr {Built-in Constant} O_APPEND\n\ |
5040
|
1033 File status flag, append on each write.\n\ |
5333
|
1034 @seealso{fcntl, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\ |
3446
|
1035 @end defvr"); |
2075
|
1036 #endif |
|
1037 |
2669
|
1038 #if defined (O_ASYNC) |
4233
|
1039 DEFCONSTX ("O_ASYNC", SBV_O_ASYNC, O_ASYNC, |
3446
|
1040 "-*- texinfo -*-\n\ |
5333
|
1041 @defvr {Built-in Constant} O_ASYNC\n\ |
5040
|
1042 File status flag, asynchronous I/O.\n\ |
5333
|
1043 @seealso{fcntl, O_APPEND, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\ |
3446
|
1044 @end defvr"); |
2669
|
1045 #endif |
|
1046 |
2075
|
1047 #if defined (O_CREAT) |
4233
|
1048 DEFCONSTX ("O_CREAT", SBV_O_CREAT, O_CREAT, |
3446
|
1049 "-*- texinfo -*-\n\ |
5333
|
1050 @defvr {Built-in Constant} O_CREAT\n\ |
5040
|
1051 File status flag, create file if it does not exist.\n\ |
5333
|
1052 @seealso{fcntl, O_APPEND, O_ASYNC, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\ |
3446
|
1053 @end defvr"); |
2075
|
1054 #endif |
|
1055 |
|
1056 #if defined (O_EXCL) |
4233
|
1057 DEFCONSTX ("O_EXCL", SBV_O_EXCL, O_EXCL, |
3446
|
1058 "-*- texinfo -*-\n\ |
5333
|
1059 @defvr {Built-in Constant} O_EXCL\n\ |
5040
|
1060 File status flag, file locking.\n\ |
5333
|
1061 @seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\ |
3446
|
1062 @end defvr"); |
2075
|
1063 #endif |
|
1064 |
|
1065 #if defined (O_NONBLOCK) |
4233
|
1066 DEFCONSTX ("O_NONBLOCK", SBV_O_NONBLOCK, O_NONBLOCK, |
3446
|
1067 "-*- texinfo -*-\n\ |
5333
|
1068 @defvr {Built-in Constant} O_NONBLOCK\n\ |
5040
|
1069 File status flag, non-blocking I/O.\n\ |
5333
|
1070 @seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\ |
3446
|
1071 @end defvr"); |
2075
|
1072 #endif |
|
1073 |
|
1074 #if defined (O_RDONLY) |
4233
|
1075 DEFCONSTX ("O_RDONLY", SBV_O_RDONLY, O_RDONLY, |
3446
|
1076 "-*- texinfo -*-\n\ |
5333
|
1077 @defvr {Built-in Constant} O_RDONLY\n\ |
5040
|
1078 File status flag, file opened for reading only.\n\ |
5333
|
1079 @seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\ |
3446
|
1080 @end defvr"); |
2075
|
1081 #endif |
|
1082 |
|
1083 #if defined (O_RDWR) |
4233
|
1084 DEFCONSTX ("O_RDWR", SBV_O_RDWR, O_RDWR, |
3446
|
1085 "-*- texinfo -*-\n\ |
5333
|
1086 @defvr {Built-in Constant} O_RDWR\n\ |
5040
|
1087 File status flag, file open for both reading and writing.\n\ |
5333
|
1088 @seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_SYNC, O_TRUNC, O_WRONLY}\n\ |
3446
|
1089 @end defvr"); |
2075
|
1090 #endif |
|
1091 |
2669
|
1092 #if defined (O_SYNC) |
4233
|
1093 DEFCONSTX ("O_SYNC", SBV_O_SYNC, O_SYNC, |
3446
|
1094 "-*- texinfo -*-\n\ |
5333
|
1095 @defvr {Built-in Constant} O_SYNC\n\ |
5040
|
1096 File status flag, file opened for synchronous I/O.\n\ |
5333
|
1097 @seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY}\n\ |
3446
|
1098 @end defvr"); |
2669
|
1099 #endif |
|
1100 |
2075
|
1101 #if defined (O_TRUNC) |
4233
|
1102 DEFCONSTX ("O_TRUNC", SBV_O_TRUNC, O_TRUNC, |
3446
|
1103 "-*- texinfo -*-\n\ |
|
1104 @defvr {Built-in Variable} O_TRUNC\n\ |
5040
|
1105 File status flag, if file exists, truncate it when writing.\n\ |
5333
|
1106 @seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_WRONLY}\n\ |
3446
|
1107 @end defvr"); |
2075
|
1108 #endif |
|
1109 |
|
1110 #if defined (O_WRONLY) |
4233
|
1111 DEFCONSTX ("O_WRONLY", SBV_O_WRONLY, O_WRONLY, |
3446
|
1112 "-*- texinfo -*-\n\ |
5333
|
1113 @defvr {Built-in Constant} O_WRONLY\n\ |
5040
|
1114 File status flag, file opened for writing only.\n\ |
5333
|
1115 @seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC}\n\ |
3446
|
1116 @end defvr"); |
2075
|
1117 #endif |
3446
|
1118 |
2075
|
1119 } |
|
1120 |
|
1121 /* |
|
1122 ;;; Local Variables: *** |
|
1123 ;;; mode: C++ *** |
|
1124 ;;; End: *** |
|
1125 */ |