changelog shortlog tags changeset files revisions annotate raw

src/syscalls.cc

changeset 10289: 4b124317dc38
parent:2fcc927a8757
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (48 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1/*
2
3Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008, 2009 John W. Eaton
5
6This file is part of Octave.
7
8Octave is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13Octave is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with Octave; see the file COPYING. If not, see
20<http://www.gnu.org/licenses/>.
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>
34#include <cstring>
35
36#include <sys/types.h>
37#include <unistd.h>
38
39#include <fcntl.h>
40
41#include "file-ops.h"
42#include "file-stat.h"
43#include "oct-syscalls.h"
44#include "oct-uname.h"
45
46#include "defun.h"
47#include "error.h"
48#include "gripes.h"
49#include "lo-utils.h"
50#include "oct-map.h"
51#include "oct-obj.h"
52#include "oct-stdstrm.h"
53#include "oct-stream.h"
54#include "sysdep.h"
55#include "utils.h"
56#include "variables.h"
57#include "input.h"
58
59static Octave_map
60mk_stat_map (const base_file_stat& fs)
61{
62 Octave_map m;
63
64 m.assign ("dev", static_cast<double> (fs.dev ()));
65 m.assign ("ino", fs.ino ());
66 m.assign ("mode", fs.mode ());
67 m.assign ("modestr", fs.mode_as_string ());
68 m.assign ("nlink", fs.nlink ());
69 m.assign ("uid", fs.uid ());
70 m.assign ("gid", fs.gid ());
71#if defined (HAVE_STRUCT_STAT_ST_RDEV)
72 m.assign ("rdev", static_cast<double> (fs.rdev ()));
73#endif
74 m.assign ("size", fs.size ());
75 m.assign ("atime", fs.atime ());
76 m.assign ("mtime", fs.mtime ());
77 m.assign ("ctime", fs.ctime ());
78#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
79 m.assign ("blksize", fs.blksize ());
80#endif
81#if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
82 m.assign ("blocks", fs.blocks ());
83#endif
84
85 return m;
86}
87
88DEFUN (dup2, args, ,
89 "-*- texinfo -*-\n\
90@deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} dup2 (@var{old}, @var{new})\n\
91Duplicate a file descriptor.\n\
92\n\
93If successful, @var{fid} is greater than zero and contains the new file\n\
94ID. Otherwise, @var{fid} is negative and @var{msg} contains a\n\
95system-dependent error message.\n\
96@end deftypefn")
97{
98 octave_value_list retval;
99
100 retval(1) = std::string ();
101 retval(0) = -1;
102
103 int nargin = args.length ();
104
105 if (nargin == 2)
106 {
107 octave_stream old_stream
108 = octave_stream_list::lookup (args(0), "dup2");
109
110 if (! error_state)
111 {
112 octave_stream new_stream
113 = octave_stream_list::lookup (args(1), "dup2");
114
115 if (! error_state)
116 {
117 int i_old = old_stream.file_number ();
118 int i_new = new_stream.file_number ();
119
120 if (i_old >= 0 && i_new >= 0)
121 {
122 std::string msg;
123
124 int status = octave_syscalls::dup2 (i_old, i_new, msg);
125
126 retval(0) = status;
127 retval(1) = msg;
128 }
129 }
130 }
131 else
132 error ("dup2: invalid stream");
133 }
134 else
135 print_usage ();
136
137 return retval;
138}
139
140DEFUN (exec, args, ,
141 "-*- texinfo -*-\n\
142@deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} exec (@var{file}, @var{args})\n\
143Replace current process with a new process. Calling @code{exec} without\n\
144first calling @code{fork} will terminate your current Octave process and\n\
145replace it with the program named by @var{file}. For example,\n\
146\n\
147@example\n\
148exec (\"ls\" \"-l\")\n\
149@end example\n\
150\n\
151@noindent\n\
152will run @code{ls} and return you to your shell prompt.\n\
153\n\
154If successful, @code{exec} does not return. If @code{exec} does return,\n\
155@var{err} will be nonzero, and @var{msg} will contain a system-dependent\n\
156error message.\n\
157@end deftypefn")
158{
159 octave_value_list retval;
160
161 retval(1) = std::string ();
162 retval(0) = -1;
163
164 int nargin = args.length ();
165
166 if (nargin == 1 || nargin == 2)
167 {
168 std::string exec_file = args(0).string_value ();
169
170 if (! error_state)
171 {
172 string_vector exec_args;
173
174 if (nargin == 2)
175 {
176 string_vector tmp = args(1).all_strings ();
177
178 if (! error_state)
179 {
180 int len = tmp.length ();
181
182 exec_args.resize (len + 1);
183
184 exec_args[0] = exec_file;
185
186 for (int i = 0; i < len; i++)
187 exec_args[i+1] = tmp[i];
188 }
189 else
190 error ("exec: arguments must be character strings");
191 }
192 else
193 {
194 exec_args.resize (1);
195
196 exec_args[0] = exec_file;
197 }
198
199 if (! error_state)
200 {
201 std::string msg;
202
203 int status = octave_syscalls::execvp (exec_file, exec_args, msg);
204
205 retval(0) = status;
206 retval(1) = msg;
207 }
208 }
209 else
210 error ("exec: first argument must be a string");
211 }
212 else
213 print_usage ();
214
215 return retval;
216}
217
218DEFUN (popen2, args, ,
219 "-*- texinfo -*-\n\
220@deftypefn {Built-in Function} {[@var{in}, @var{out}, @var{pid}] =} popen2 (@var{command}, @var{args})\n\
221Start a subprocess with two-way communication. The name of the process\n\
222is given by @var{command}, and @var{args} is an array of strings\n\
223containing options for the command. The file identifiers for the input\n\
224and output streams of the subprocess are returned in @var{in} and\n\
225@var{out}. If execution of the command is successful, @var{pid}\n\
226contains the process ID of the subprocess. Otherwise, @var{pid} is\n\
227@minus{}1.\n\
228\n\
229For example,\n\
230\n\
231@example\n\
232[in, out, pid] = popen2 (\"sort\", \"-r\");\n\
233fputs (in, \"these\\nare\\nsome\\nstrings\\n\");\n\
234fclose (in);\n\
235EAGAIN = errno (\"EAGAIN\");\n\
236done = false;\n\
237do\n\
238 s = fgets (out);\n\
239 if (ischar (s))\n\
240 fputs (stdout, s);\n\
241 elseif (errno () == EAGAIN)\n\
242 sleep (0.1);\n\
243 fclear (out);\n\
244 else\n\
245 done = true;\n\
246 endif\n\
247until (done)\n\
248fclose (out);\n\
249waitpid (pid);\n\
250 @print{} these\n\
251 @print{} strings\n\
252 @print{} some\n\
253 @print{} are\n\
254@end example\n\
255\n\
256Note that @code{popen2}, unlike @code{popen}, will not \"reap\" the\n\
257child process. If you don't use @code{waitpid} to check the child's\n\
258exit status, it will linger until Octave exits.\n\
259@end deftypefn")
260{
261 octave_value_list retval;
262
263 retval(2) = -1;
264 retval(1) = Matrix ();
265 retval(0) = Matrix ();
266
267 int nargin = args.length ();
268
269 if (nargin >= 1 && nargin <= 3)
270 {
271 std::string exec_file = args(0).string_value();
272
273 if (! error_state)
274 {
275 string_vector arg_list;
276
277 if (nargin >= 2)
278 {
279 string_vector tmp = args(1).all_strings ();
280
281 if (! error_state)
282 {
283 int len = tmp.length ();
284
285 arg_list.resize (len + 1);
286
287 arg_list[0] = exec_file;
288
289 for (int i = 0; i < len; i++)
290 arg_list[i+1] = tmp[i];
291 }
292 else
293 error ("popen2: arguments must be character strings");
294 }
295 else
296 {
297 arg_list.resize (1);
298
299 arg_list[0] = exec_file;
300 }
301
302 if (! error_state)
303 {
304 bool sync_mode = (nargin == 3 ? args(2).bool_value() : false);
305
306 if (! error_state)
307 {
308 int fildes[2];
309 std::string msg;
310 pid_t pid;
311
312 pid = octave_syscalls::popen2 (exec_file, arg_list, sync_mode, fildes, msg, interactive);
313 if (pid >= 0)
314 {
315 FILE *ifile = fdopen (fildes[1], "r");
316 FILE *ofile = fdopen (fildes[0], "w");
317
318 std::string nm;
319
320 octave_stream is = octave_stdiostream::create (nm, ifile,
321 std::ios::in);
322
323 octave_stream os = octave_stdiostream::create (nm, ofile,
324 std::ios::out);
325
326 Cell file_ids (1, 2);
327
328 retval(0) = octave_stream_list::insert (os);
329 retval(1) = octave_stream_list::insert (is);
330 retval(2) = pid;
331 }
332 else
333 error (msg.c_str ());
334 }
335 }
336 else
337 error ("popen2: arguments must be character strings");
338 }
339 else
340 error ("popen2: first argument must be a string");
341 }
342 else
343 print_usage ();
344
345 return retval;
346}
347
348/*
349
350%!test
351%! if (isunix())
352%! [in, out, pid] = popen2 ("sort", "-r");
353%! EAGAIN = errno ("EAGAIN");
354%! else
355%! [in, out, pid] = popen2 ("sort", "/R");
356%! EAGAIN = errno ("EINVAL");
357%! endif
358%! fputs (in, "these\nare\nsome\nstrings\n");
359%! fclose (in);
360%! done = false;
361%! str = {};
362%! idx = 0;
363%! errs = 0;
364%! do
365%! if (!isunix())
366%! errno (0);
367%! endif
368%! s = fgets (out);
369%! if (ischar (s))
370%! idx++;
371%! str{idx} = s;
372%! elseif (errno () == EAGAIN)
373%! fclear (out);
374%! sleep (0.1);
375%! if (++errs == 100)
376%! done = true;
377%! endif
378%! else
379%! done = true;
380%! endif
381%! until (done)
382%! fclose (out);
383%! if (isunix())
384%! assert(str,{"these\n","strings\n","some\n","are\n"})
385%! else
386%! assert(str,{"these\r\n","strings\r\n","some\r\n","are\r\n"})
387%! end
388
389*/
390
391DEFUNX ("fcntl", Ffcntl, args, ,
392 "-*- texinfo -*-\n\
393@deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} fcntl (@var{fid}, @var{request}, @var{arg})\n\
394Change the properties of the open file @var{fid}. The following values\n\
395may be passed as @var{request}:\n\
396\n\
397@vtable @code\n\
398@item F_DUPFD\n\
399Return a duplicate file descriptor.\n\
400\n\
401@item F_GETFD\n\
402Return the file descriptor flags for @var{fid}.\n\
403\n\
404@item F_SETFD\n\
405Set the file descriptor flags for @var{fid}.\n\
406\n\
407@item F_GETFL\n\
408Return the file status flags for @var{fid}. The following codes may be\n\
409returned (some of the flags may be undefined on some systems).\n\
410\n\
411@vtable @code\n\
412@item O_RDONLY\n\
413Open for reading only.\n\
414\n\
415@item O_WRONLY\n\
416Open for writing only.\n\
417\n\
418@item O_RDWR\n\
419Open for reading and writing.\n\
420\n\
421@item O_APPEND\n\
422Append on each write.\n\
423\n\
424@item O_CREAT\n\
425Create the file if it does not exist.\n\
426\n\
427@item O_NONBLOCK\n\
428Nonblocking mode.\n\
429\n\
430@item O_SYNC\n\
431Wait for writes to complete.\n\
432\n\
433@item O_ASYNC\n\
434Asynchronous I/O.\n\
435@end vtable\n\
436\n\
437@item F_SETFL\n\
438Set the file status flags for @var{fid} to the value specified by\n\
439@var{arg}. The only flags that can be changed are @w{@code{O_APPEND}} and\n\
440@w{@code{O_NONBLOCK}}.\n\
441@end vtable\n\
442\n\
443If successful, @var{err} is 0 and @var{msg} is an empty string.\n\
444Otherwise, @var{err} is nonzero and @var{msg} contains a\n\
445system-dependent error message.\n\
446@end deftypefn")
447{
448 octave_value_list retval;
449
450 retval(1) = std::string ();
451 retval(0) = -1;
452
453 int nargin = args.length ();
454
455 if (nargin == 3)
456 {
457 octave_stream strm = octave_stream_list::lookup (args (0), "fcntl");
458
459 if (! error_state)
460 {
461 int fid = strm.file_number ();
462
463 int req = args(1).int_value (true);
464 int arg = args(2).int_value (true);
465
466 if (! error_state)
467 {
468 // FIXME -- Need better checking here?
469 if (fid < 0)
470 error ("fcntl: invalid file id");
471 else
472 {
473 std::string msg;
474
475 int status = octave_fcntl (fid, req, arg, msg);
476
477 retval(0) = status;
478 retval(1) = msg;
479 }
480 }
481 }
482 else
483 error ("fcntl: file id, request, and argument must be integers");
484 }
485 else
486 print_usage ();
487
488 return retval;
489}
490
491DEFUN (fork, args, ,
492 "-*- texinfo -*-\n\
493@deftypefn {Built-in Function} {[@var{pid}, @var{msg}] =} fork ()\n\
494Create a copy of the current process.\n\
495\n\
496Fork can return one of the following values:\n\
497\n\
498@table @asis\n\
499@item > 0\n\
500You are in the parent process. The value returned from @code{fork} is\n\
501the process id of the child process. You should probably arrange to\n\
502wait for any child processes to exit.\n\
503\n\
504@item 0\n\
505You are in the child process. You can call @code{exec} to start another\n\
506process. If that fails, you should probably call @code{exit}.\n\
507\n\
508@item < 0\n\
509The call to @code{fork} failed for some reason. You must take evasive\n\
510action. A system dependent error message will be waiting in @var{msg}.\n\
511@end table\n\
512@end deftypefn")
513{
514 octave_value_list retval;
515
516 retval(1) = std::string ();
517 retval(0) = -1;
518
519 int nargin = args.length ();
520
521 if (nargin == 0)
522 {
523 std::string msg;
524
525 pid_t pid = octave_syscalls::fork (msg);
526
527 retval(0) = pid;
528 retval(1) = msg;
529 }
530 else
531 print_usage ();
532
533 return retval;
534}
535
536DEFUN (getpgrp, args, ,
537 "-*- texinfo -*-\n\
538@deftypefn {Built-in Function} {pgid =} getpgrp ()\n\
539Return the process group id of the current process.\n\
540@end deftypefn")
541{
542 octave_value_list retval;
543
544 retval(1) = std::string ();
545 retval(0) = -1;
546
547 int nargin = args.length ();
548
549 if (nargin == 0)
550 {
551 std::string msg;
552
553 retval(0) = octave_syscalls::getpgrp (msg);
554 retval(1) = msg;
555 }
556 else
557 print_usage ();
558
559 return retval;
560}
561
562DEFUN (getpid, args, ,
563 "-*- texinfo -*-\n\
564@deftypefn {Built-in Function} {pid =} getpid ()\n\
565Return the process id of the current process.\n\
566@end deftypefn")
567{
568 octave_value retval = -1;
569
570 int nargin = args.length ();
571
572 if (nargin == 0)
573 retval = octave_syscalls::getpid ();
574 else
575 print_usage ();
576
577 return retval;
578}
579
580DEFUN (getppid, args, ,
581 "-*- texinfo -*-\n\
582@deftypefn {Built-in Function} {pid =} getppid ()\n\
583Return the process id of the parent process.\n\
584@end deftypefn")
585{
586 octave_value retval = -1;
587
588 int nargin = args.length ();
589
590 if (nargin == 0)
591 retval = octave_syscalls::getppid ();
592 else
593 print_usage ();
594
595 return retval;
596}
597
598DEFUN (getegid, args, ,
599 "-*- texinfo -*-\n\
600@deftypefn {Built-in Function} {egid =} getegid ()\n\
601Return the effective group id of the current process.\n\
602@end deftypefn")
603{
604 octave_value retval = -1;
605
606 int nargin = args.length ();
607
608 if (nargin == 0)
609 retval = octave_syscalls::getegid ();
610 else
611 print_usage ();
612
613 return retval;
614}
615
616DEFUN (getgid, args, ,
617 "-*- texinfo -*-\n\
618@deftypefn {Built-in Function} {gid =} getgid ()\n\
619Return the real group id of the current process.\n\
620@end deftypefn")
621{
622 octave_value retval = -1;
623
624 int nargin = args.length ();
625
626 if (nargin == 0)
627 retval = octave_syscalls::getgid ();
628 else
629 print_usage ();
630
631 return retval;
632}
633
634DEFUN (geteuid, args, ,
635 "-*- texinfo -*-\n\
636@deftypefn {Built-in Function} {euid =} geteuid ()\n\
637Return the effective user id of the current process.\n\
638@end deftypefn")
639{
640 octave_value retval = -1;
641
642 int nargin = args.length ();
643
644 if (nargin == 0)
645 retval = octave_syscalls::geteuid ();
646 else
647 print_usage ();
648
649 return retval;
650}
651
652DEFUN (getuid, args, ,
653 "-*- texinfo -*-\n\
654@deftypefn {Built-in Function} {uid =} getuid ()\n\
655Return the real user id of the current process.\n\
656@end deftypefn")
657{
658 octave_value retval = -1;
659
660 int nargin = args.length ();
661
662 if (nargin == 0)
663 retval = octave_syscalls::getuid ();
664 else
665 print_usage ();
666
667 return retval;
668}
669
670DEFUN (kill, args, ,
671 "-*- texinfo -*-\n\
672@deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} kill (@var{pid}, @var{sig})\n\
673Send signal @var{sig} to process @var{pid}.\n\
674\n\
675If @var{pid} is positive, then signal @var{sig} is sent to @var{pid}.\n\
676\n\
677If @var{pid} is 0, then signal @var{sig} is sent to every process\n\
678in the process group of the current process.\n\
679\n\
680If @var{pid} is -1, then signal @var{sig} is sent to every process\n\
681except process 1.\n\
682\n\
683If @var{pid} is less than -1, then signal @var{sig} is sent to every\n\
684process in the process group @var{-pid}.\n\
685\n\
686If @var{sig} is 0, then no signal is sent, but error checking is still\n\
687performed.\n\
688\n\
689Return 0 if successful, otherwise return -1.\n\
690@end deftypefn")
691{
692 octave_value_list retval;
693
694 retval(1) = std::string ();
695 retval(0) = -1;
696
697 if (args.length () == 2)
698 {
699 pid_t pid = args(0).int_value (true);
700
701 if (! error_state)
702 {
703 int sig = args(1).int_value (true);
704
705 if (! error_state)
706 {
707 std::string msg;
708
709 int status = octave_syscalls::kill (pid, sig, msg);
710
711 retval(1) = msg;
712 retval(0) = status;
713 }
714 }
715 }
716 else
717 print_usage ();
718
719 return retval;
720}
721
722DEFUN (fstat, args, ,
723 "-*- texinfo -*-\n\
724@deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} fstat (@var{fid})\n\
725Return information about the open file @var{fid}. See @code{stat}\n\
726for a description of the contents of @var{info}.\n\
727@end deftypefn")
728{
729 octave_value_list retval;
730
731 if (args.length () == 1)
732 {
733 int fid = octave_stream_list::get_file_number (args(0));
734
735 if (! error_state)
736 {
737 file_fstat fs (fid);
738
739 if (fs)
740 {
741 retval(2) = std::string ();
742 retval(1) = 0;
743 retval(0) = octave_value (mk_stat_map (fs));
744 }
745 else
746 {
747 retval(2) = fs.error ();
748 retval(1) = -1;
749 retval(0) = Matrix ();
750 }
751 }
752 }
753 else
754 print_usage ();
755
756 return retval;
757}
758
759DEFUN (lstat, args, ,
760 "-*- texinfo -*-\n\
761@deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} lstat (@var{file})\n\
762See stat.\n\
763@end deftypefn")
764{
765 octave_value_list retval;
766
767 if (args.length () == 1)
768 {
769 std::string fname = args(0).string_value ();
770
771 if (! error_state)
772 {
773 file_stat fs (fname, false);
774
775 if (fs)
776 {
777 retval(2) = std::string ();
778 retval(1) = 0;
779 retval(0) = mk_stat_map (fs);
780 }
781 else
782 {
783 retval(2) = fs.error ();
784 retval(1) = -1;
785 retval(0) = Matrix ();
786 }
787 }
788 }
789 else
790 print_usage ();
791
792 return retval;
793}
794
795
796
797DEFUNX ("mkfifo", Fmkfifo, args, ,
798 "-*- texinfo -*-\n\
799@deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} mkfifo (@var{name}, @var{mode})\n\
800Create a @var{fifo} special file named @var{name} with file mode @var{mode}\n\
801\n\
802If successful, @var{err} is 0 and @var{msg} is an empty string.\n\
803Otherwise, @var{err} is nonzero and @var{msg} contains a\n\
804system-dependent error message.\n\
805@end deftypefn")
806{
807 octave_value_list retval;
808
809 retval(1) = std::string ();
810 retval(0) = -1;
811
812 int nargin = args.length ();
813
814 if (nargin == 2)
815 {
816 if (args(0).is_string ())
817 {
818 std::string name = args(0).string_value ();
819
820 if (args(1).is_scalar_type ())
821 {
822 long mode = args(1).long_value ();
823
824 if (! error_state)
825 {
826 std::string msg;
827
828 int status = octave_mkfifo (name, mode, msg);
829
830 retval(0) = status;
831
832 if (status < 0)
833 retval(1) = msg;
834 }
835 else
836 error ("mkfifo: invalid MODE");
837 }
838 else
839 error ("mkfifo: MODE must be an integer");
840 }
841 else
842 error ("mkfifo: file name must be a string");
843 }
844 else
845 print_usage ();
846
847 return retval;
848}
849
850DEFUN (pipe, args, ,
851 "-*- texinfo -*-\n\
852@deftypefn {Built-in Function} {[@var{read_fd}, @var{write_fd}, @var{err}, @var{msg}] =} pipe ()\n\
853Create a pipe and return the reading and writing ends of the pipe\n\
854into @var{read_fd} and @var{write_fd} respectively.\n\
855\n\
856If successful, @var{err} is 0 and @var{msg} is an empty string.\n\
857Otherwise, @var{err} is nonzero and @var{msg} contains a\n\
858system-dependent error message.\n\
859@end deftypefn")
860{
861 octave_value_list retval;
862
863 retval(3) = std::string ();
864 retval(2) = -1;
865 retval(1) = -1;
866 retval(0) = -1;
867
868 int nargin = args.length ();
869
870 if (nargin == 0)
871 {
872 int fid[2];
873
874 std::string msg;
875
876 int status = octave_syscalls::pipe (fid, msg);
877
878 if (status < 0)
879 retval(3) = msg;
880 else
881 {
882 FILE *ifile = fdopen (fid[0], "r");
883 FILE *ofile = fdopen (fid[1], "w");
884
885 std::string nm;
886
887 octave_stream is = octave_stdiostream::create (nm, ifile,
888 std::ios::in);
889
890 octave_stream os = octave_stdiostream::create (nm, ofile,
891 std::ios::out);
892
893 retval(1) = octave_stream_list::insert (os);
894 retval(0) = octave_stream_list::insert (is);
895
896 retval(2) = status;
897 }
898 }
899 else
900 print_usage ();
901
902 return retval;
903}
904
905DEFUN (stat, args, ,
906 "-*- texinfo -*-\n\
907@deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} stat (@var{file})\n\
908@deftypefnx {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} lstat (@var{file})\n\
909Return a structure @var{s} containing the following information about\n\
910@var{file}.\n\
911\n\
912@table @code\n\
913@item dev\n\
914ID of device containing a directory entry for this file.\n\
915\n\
916@item ino\n\
917File number of the file.\n\
918\n\
919@item mode\n\
920File mode, as an integer. Use the functions @w{@code{S_ISREG}},\n\
921@w{@code{S_ISDIR}}, @w{@code{S_ISCHR}}, @w{@code{S_ISBLK}}, @w{@code{S_ISFIFO}},\n\
922@w{@code{S_ISLNK}}, or @w{@code{S_ISSOCK}} to extract information from this\n\
923value.\n\
924\n\
925@item modestr\n\
926File mode, as a string of ten letters or dashes as would be returned by\n\
927@kbd{ls -l}.\n\
928\n\
929@item nlink\n\
930Number of links.\n\
931\n\
932@item uid\n\
933User ID of file's owner.\n\
934\n\
935@item gid\n\
936Group ID of file's group.\n\
937\n\
938@item rdev\n\
939ID of device for block or character special files.\n\
940\n\
941@item size\n\
942Size in bytes.\n\
943\n\
944@item atime\n\
945Time of last access in the same form as time values returned from\n\
946@code{time}. @xref{Timing Utilities}.\n\
947\n\
948@item mtime\n\
949Time of last modification in the same form as time values returned from\n\
950@code{time}. @xref{Timing Utilities}.\n\
951\n\
952@item ctime\n\
953Time of last file status change in the same form as time values\n\
954returned from @code{time}. @xref{Timing Utilities}.\n\
955\n\
956@item blksize\n\
957Size of blocks in the file.\n\
958\n\
959@item blocks\n\
960Number of blocks allocated for file.\n\
961@end table\n\
962\n\
963If the call is successful @var{err} is 0 and @var{msg} is an empty\n\
964string. If the file does not exist, or some other error occurs, @var{s}\n\
965is an empty matrix, @var{err} is @minus{}1, and @var{msg} contains the\n\
966corresponding system error message.\n\
967\n\
968If @var{file} is a symbolic link, @code{stat} will return information\n\
969about the actual file that is referenced by the link. Use @code{lstat}\n\
970if you want information about the symbolic link itself.\n\
971\n\
972For example,\n\
973\n\
974@example\n\
975[s, err, msg] = stat (\"/vmlinuz\")\n\
976 @result{} s =\n\
977 @{\n\
978 atime = 855399756\n\
979 rdev = 0\n\
980 ctime = 847219094\n\
981 uid = 0\n\
982 size = 389218\n\
983 blksize = 4096\n\
984 mtime = 847219094\n\
985 gid = 6\n\
986 nlink = 1\n\
987 blocks = 768\n\
988 mode = -rw-r--r--\n\
989 modestr = -rw-r--r--\n\
990 ino = 9316\n\
991 dev = 2049\n\
992 @}\n\
993 @result{} err = 0\n\
994 @result{} msg = \n\
995@end example\n\
996@end deftypefn")
997{
998 octave_value_list retval;
999
1000 if (args.length () == 1)
1001 {
1002 std::string fname = args(0).string_value ();
1003
1004 if (! error_state)
1005 {
1006 file_stat fs (fname);
1007
1008 if (fs)
1009 {
1010 retval(2) = std::string ();
1011 retval(1) = 0;
1012 retval(0) = octave_value (mk_stat_map (fs));
1013 }
1014 else
1015 {
1016 retval(2) = fs.error ();
1017 retval(1) = -1;
1018 retval(0) = Matrix ();
1019 }
1020 }
1021 }
1022 else
1023 print_usage ();
1024
1025 return retval;
1026}
1027
1028DEFUNX ("S_ISREG", FS_ISREG, args, ,
1029 "-*- texinfo -*-\n\
1030@deftypefn {Built-in Function} {} S_ISREG (@var{mode})\n\
1031Return true if @var{mode} corresponds to a regular file. The value\n\
1032of @var{mode} is assumed to be returned from a call to @code{stat}.\n\
1033@seealso{stat, lstat}\n\
1034@end deftypefn")
1035{
1036 octave_value retval = false;
1037
1038 if (args.length () == 1)
1039 {
1040 double mode = args(0).double_value ();
1041
1042 if (! error_state)
1043 retval = file_stat::is_reg (static_cast<mode_t> (mode));
1044 else
1045 error ("S_ISREG: invalid mode value");
1046 }
1047 else
1048 print_usage ();
1049
1050 return retval;
1051}
1052
1053DEFUNX ("S_ISDIR", FS_ISDIR, args, ,
1054 "-*- texinfo -*-\n\
1055@deftypefn {Built-in Function} {} S_ISDIR (@var{mode})\n\
1056Return true if @var{mode} corresponds to a directory. The value\n\
1057of @var{mode} is assumed to be returned from a call to @code{stat}.\n\
1058@seealso{stat, lstat}\n\
1059@end deftypefn")
1060{
1061 octave_value retval = false;
1062
1063 if (args.length () == 1)
1064 {
1065 double mode = args(0).double_value ();
1066
1067 if (! error_state)
1068 retval = file_stat::is_dir (static_cast<mode_t> (mode));
1069 else
1070 error ("S_ISDIR: invalid mode value");
1071 }
1072 else
1073 print_usage ();
1074
1075 return retval;
1076}
1077
1078DEFUNX ("S_ISCHR", FS_ISCHR, args, ,
1079 "-*- texinfo -*-\n\
1080@deftypefn {Built-in Function} {} S_ISCHR (@var{mode})\n\
1081Return true if @var{mode} corresponds to a character devicey. The value\n\
1082of @var{mode} is assumed to be returned from a call to @code{stat}.\n\
1083@seealso{stat, lstat}\n\
1084@end deftypefn")
1085{
1086 octave_value retval = false;
1087
1088 if (args.length () == 1)
1089 {
1090 double mode = args(0).double_value ();
1091
1092 if (! error_state)
1093 retval = file_stat::is_chr (static_cast<mode_t> (mode));
1094 else
1095 error ("S_ISCHR: invalid mode value");
1096 }
1097 else
1098 print_usage ();
1099
1100 return retval;
1101}
1102
1103DEFUNX ("S_ISBLK", FS_ISBLK, args, ,
1104 "-*- texinfo -*-\n\
1105@deftypefn {Built-in Function} {} S_ISBLK (@var{mode})\n\
1106Return true if @var{mode} corresponds to a block device. The value\n\
1107of @var{mode} is assumed to be returned from a call to @code{stat}.\n\
1108@seealso{stat, lstat}\n\
1109@end deftypefn")
1110{
1111 octave_value retval = false;
1112
1113 if (args.length () == 1)
1114 {
1115 double mode = args(0).double_value ();
1116
1117 if (! error_state)
1118 retval = file_stat::is_blk (static_cast<mode_t> (mode));
1119 else
1120 error ("S_ISBLK: invalid mode value");
1121 }
1122 else
1123 print_usage ();
1124
1125 return retval;
1126}
1127
1128DEFUNX ("S_ISFIFO", FS_ISFIFO, args, ,
1129 "-*- texinfo -*-\n\
1130@deftypefn {Built-in Function} {} S_ISFIFO (@var{mode})\n\
1131Return true if @var{mode} corresponds to a fifo. The value\n\
1132of @var{mode} is assumed to be returned from a call to @code{stat}.\n\
1133@seealso{stat, lstat}\n\
1134@end deftypefn")
1135{
1136 octave_value retval = false;
1137
1138 if (args.length () == 1)
1139 {
1140 double mode = args(0).double_value ();
1141
1142 if (! error_state)
1143 retval = file_stat::is_fifo (static_cast<mode_t> (mode));
1144 else
1145 error ("S_ISFIFO: invalid mode value");
1146 }
1147 else
1148 print_usage ();
1149
1150 return retval;
1151}
1152
1153DEFUNX ("S_ISLNK", FS_ISLNK, args, ,
1154 "-*- texinfo -*-\n\
1155@deftypefn {Built-in Function} {} S_ISLNK (@var{mode})\n\
1156Return true if @var{mode} corresponds to a symbolic link. The value\n\
1157of @var{mode} is assumed to be returned from a call to @code{stat}.\n\
1158@seealso{stat, lstat}\n\
1159@end deftypefn")
1160{
1161 octave_value retval = false;
1162
1163 if (args.length () == 1)
1164 {
1165 double mode = args(0).double_value ();
1166
1167 if (! error_state)
1168 retval = file_stat::is_lnk (static_cast<mode_t> (mode));
1169 else
1170 error ("S_ISLNK: invalid mode value");
1171 }
1172 else
1173 print_usage ();
1174
1175 return retval;
1176}
1177
1178DEFUNX ("S_ISSOCK", FS_ISSOCK, args, ,
1179 "-*- texinfo -*-\n\
1180@deftypefn {Built-in Function} {} S_ISSOCK (@var{mode})\n\
1181@seealso{stat, lstat}\n\
1182@end deftypefn")
1183{
1184 octave_value retval = false;
1185
1186 if (args.length () == 1)
1187 {
1188 double mode = args(0).double_value ();
1189
1190 if (! error_state)
1191 retval = file_stat::is_sock (static_cast<mode_t> (mode));
1192 else
1193 error ("S_ISSOCK: invalid mode value");
1194 }
1195 else
1196 print_usage ();
1197
1198 return retval;
1199}
1200
1201DEFUN (uname, args, ,
1202 "-*- texinfo -*-\n\
1203@deftypefn {Built-in Function} {[@var{uts}, @var{err}, @var{msg}] =} uname ()\n\
1204Return system information in the structure. For example,\n\
1205\n\
1206@example\n\
1207@group\n\
1208uname ()\n\
1209 @result{} @{\n\
1210 sysname = x86_64\n\
1211 nodename = segfault\n\
1212 release = 2.6.15-1-amd64-k8-smp\n\
1213 version = Linux\n\
1214 machine = #2 SMP Thu Feb 23 04:57:49 UTC 2006\n\
1215 @}\n\
1216@end group\n\
1217@end example\n\
1218\n\
1219If successful, @var{err} is 0 and @var{msg} is an empty string.\n\
1220Otherwise, @var{err} is nonzero and @var{msg} contains a\n\
1221system-dependent error message.\n\
1222@end deftypefn")
1223{
1224 octave_value_list retval;
1225
1226 if (args.length () == 0)
1227 {
1228 octave_uname sysinfo;
1229
1230 Octave_map m;
1231
1232 m.assign ("sysname", sysinfo.sysname ());
1233 m.assign ("nodename", sysinfo.nodename ());
1234 m.assign ("release", sysinfo.release ());
1235 m.assign ("version", sysinfo.version ());
1236 m.assign ("machine", sysinfo.machine ());
1237
1238 retval(2) = sysinfo.message ();
1239 retval(1) = sysinfo.error ();
1240 retval(0) = m;
1241 }
1242 else
1243 print_usage ();
1244
1245 return retval;
1246}
1247
1248DEFUNX ("unlink", Funlink, args, ,
1249 "-*- texinfo -*-\n\
1250@deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} unlink (@var{file})\n\
1251Delete the file named @var{file}.\n\
1252\n\
1253If successful, @var{err} is 0 and @var{msg} is an empty string.\n\
1254Otherwise, @var{err} is nonzero and @var{msg} contains a\n\
1255system-dependent error message.\n\
1256@end deftypefn")
1257{
1258 octave_value_list retval;
1259
1260 retval(1) = std::string ();
1261 retval(0) = -1;
1262
1263 int nargin = args.length ();
1264
1265 if (nargin == 1)
1266 {
1267 if (args(0).is_string ())
1268 {
1269 std::string name = args(0).string_value ();
1270
1271 std::string msg;
1272
1273 int status = octave_unlink (name, msg);
1274
1275 retval(0) = status;
1276 retval(1) = msg;
1277 }
1278 else
1279 error ("unlink: file name must be a string");
1280 }
1281 else
1282 print_usage ();
1283
1284 return retval;
1285}
1286
1287DEFUN (waitpid, args, ,
1288 "-*- texinfo -*-\n\
1289@deftypefn {Built-in Function} {[@var{pid}, @var{status}, @var{msg}] =} waitpid (@var{pid}, @var{options})\n\
1290Wait for process @var{pid} to terminate. The @var{pid} argument can be:\n\
1291\n\
1292@table @asis\n\
1293@item @minus{}1\n\
1294Wait for any child process.\n\
1295\n\
1296@item 0\n\
1297Wait for any child process whose process group ID is equal to that of\n\
1298the Octave interpreter process.\n\
1299\n\
1300@item > 0\n\
1301Wait for termination of the child process with ID @var{pid}.\n\
1302@end table\n\
1303\n\
1304The @var{options} argument can be a bitwise OR of zero or more of\n\
1305the following constants:\n\
1306\n\
1307@table @code\n\
1308@item 0\n\
1309Wait until signal is received or a child process exits (this is the\n\
1310default if the @var{options} argument is missing).\n\
1311\n\
1312@item WNOHANG\n\
1313Do not hang if status is not immediately available.\n\
1314\n\
1315@item WUNTRACED\n\
1316Report the status of any child processes that are stopped, and whose\n\
1317status has not yet been reported since they stopped.\n\
1318\n\
1319@item WCONTINUE\n\
1320Return if a stopped child has been resumed by delivery of @code{SIGCONT}.\n\
1321This value may not be meaningful on all systems.\n\
1322@end table\n\
1323\n\
1324If the returned value of @var{pid} is greater than 0, it is the process\n\
1325ID of the child process that exited. If an error occurs, @var{pid} will\n\
1326be less than zero and @var{msg} will contain a system-dependent error\n\
1327message. The value of @var{status} contains additional system-dependent\n\
1328information about the subprocess that exited.\n\
1329@seealso{WCONTINUE, WCOREDUMP, WEXITSTATUS, WIFCONTINUED, WIFSIGNALED, WIFSTOPPED, WNOHANG, WSTOPSIG, WTERMSIG, WUNTRACED}\n\
1330@end deftypefn")
1331{
1332 octave_value_list retval;
1333
1334 retval(2) = std::string ();
1335 retval(1) = 0;
1336 retval(0) = -1;
1337
1338 int nargin = args.length ();
1339
1340 if (nargin == 1 || nargin == 2)
1341 {
1342 pid_t pid = args(0).int_value (true);
1343
1344 if (! error_state)
1345 {
1346 int options = 0;
1347
1348 if (args.length () == 2)
1349 options = args(1).int_value (true);
1350
1351 if (! error_state)
1352 {
1353 std::string msg;
1354
1355 int status = 0;
1356
1357 pid_t result = octave_syscalls::waitpid (pid, &status, options, msg);
1358
1359 retval(0) = result;
1360 retval(1) = status;
1361 retval(2) = msg;
1362 }
1363 else
1364 error ("waitpid: OPTIONS must be an integer");
1365 }
1366 else
1367 error ("waitpid: PID must be an integer value");
1368 }
1369 else
1370 print_usage ();
1371
1372 return retval;
1373}
1374
1375DEFUNX ("WIFEXITED", FWIFEXITED, args, ,
1376 "-*- texinfo -*-\n\
1377@deftypefn {Built-in Function} {} WIFEXITED (@var{status})\n\
1378Given @var{status} from a call to @code{waitpid}, return true if the\n\
1379child terminated normally.\n\
1380@seealso{waitpid, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WCOREDUMP, WIFSTOPPED, WSTOPSIG, WIFCONTINUED}\n\
1381@end deftypefn")
1382{
1383 octave_value retval = 0.0;
1384
1385#if defined (WIFEXITED)
1386 if (args.length () == 1)
1387 {
1388 int status = args(0).int_value ();
1389
1390 if (! error_state)
1391 retval = WIFEXITED (status);
1392 else
1393 error ("WIFEXITED: expecting integer argument");
1394 }
1395#else
1396 warning ("WIFEXITED always returns false in this version of Octave")
1397#endif
1398
1399 return retval;
1400}
1401
1402DEFUNX ("WEXITSTATUS", FWEXITSTATUS, args, ,
1403 "-*- texinfo -*-\n\
1404@deftypefn {Built-in Function} {} WEXITSTATUS (@var{status})\n\
1405Given @var{status} from a call to @code{waitpid}, return the exit\n\
1406status of the child. This function should only be employed if\n\
1407@code{WIFEXITED} returned true.\n\
1408@seealso{waitpid, WIFEXITED, WIFSIGNALED, WTERMSIG, WCOREDUMP, WIFSTOPPED, WSTOPSIG, WIFCONTINUED}\n\
1409@end deftypefn")
1410{
1411 octave_value retval = 0.0;
1412
1413#if defined (WEXITSTATUS)
1414 if (args.length () == 1)
1415 {
1416 int status = args(0).int_value ();
1417
1418 if (! error_state)
1419 retval = WEXITSTATUS (status);
1420 else
1421 error ("WEXITSTATUS: expecting integer argument");
1422 }
1423#else
1424 warning ("WEXITSTATUS always returns false in this version of Octave")
1425#endif
1426
1427 return retval;
1428}
1429
1430DEFUNX ("WIFSIGNALED", FWIFSIGNALED, args, ,
1431 "-*- texinfo -*-\n\
1432@deftypefn {Built-in Function} {} WIFSIGNALED (@var{status})\n\
1433Given @var{status} from a call to @code{waitpid}, return true if the\n\
1434child process was terminated by a signal.\n\
1435@seealso{waitpid, WIFEXITED, WEXITSTATUS, WTERMSIG, WCOREDUMP, WIFSTOPPED, WSTOPSIG, WIFCONTINUED}\n\
1436@end deftypefn")
1437{
1438 octave_value retval = 0.0;
1439
1440#if defined (WIFSIGNALED)
1441 if (args.length () == 1)
1442 {
1443 int status = args(0).int_value ();
1444
1445 if (! error_state)
1446 retval = WIFSIGNALED (status);
1447 else
1448 error ("WIFSIGNALED: expecting integer argument");
1449 }
1450#else
1451 warning ("WIFSIGNALED always returns false in this version of Octave");
1452#endif
1453
1454 return retval;
1455}
1456
1457DEFUNX ("WTERMSIG", FWTERMSIG, args, ,
1458 "-*- texinfo -*-\n\
1459@deftypefn {Built-in Function} {} WTERMSIG (@var{status})\n\
1460Given @var{status} from a call to @code{waitpid}, return the number of\n\
1461the signal that caused the child process to terminate. This function\n\
1462should only be employed if @code{WIFSIGNALED} returned true.\n\
1463@seealso{waitpid, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WCOREDUMP, WIFSTOPPED, WSTOPSIG, WIFCONTINUED}\n\
1464@end deftypefn")
1465{
1466 octave_value retval = 0.0;
1467
1468#if defined (WTERMSIG)
1469 if (args.length () == 1)
1470 {
1471 int status = args(0).int_value ();
1472
1473 if (! error_state)
1474 retval = WTERMSIG (status);
1475 else
1476 error ("WTERMSIG: expecting integer argument");
1477 }
1478#else
1479 warning ("WTERMSIG always returns false in this version of Octave");
1480#endif
1481
1482 return retval;
1483}
1484
1485DEFUNX ("WCOREDUMP", FWCOREDUMP, args, ,
1486 "-*- texinfo -*-\n\
1487@deftypefn {Built-in Function} {} WCOREDUMP (@var{status})\n\
1488Given @var{status} from a call to @code{waitpid}, return true if the\n\
1489child produced a core dump. This function should only be employed if\n\
1490@code{WIFSIGNALED} returned true. The macro used to implement this\n\
1491function is not specified in POSIX.1-2001 and is not available on some\n\
1492Unix implementations (e.g., AIX, SunOS).\n\
1493@seealso{waitpid, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WIFSTOPPED, WSTOPSIG, WIFCONTINUED}\n\
1494@end deftypefn")
1495{
1496 octave_value retval = 0.0;
1497
1498#if defined (WCOREDUMP)
1499 if (args.length () == 1)
1500 {
1501 int status = args(0).int_value ();
1502
1503 if (! error_state)
1504 retval = WCOREDUMP (status);
1505 else
1506 error ("WCOREDUMP: expecting integer argument");
1507 }
1508#else
1509 warning ("WCOREDUMP always returns false in this version of Octave");
1510#endif
1511
1512 return retval;
1513}
1514
1515DEFUNX ("WIFSTOPPED", FWIFSTOPPED, args, ,
1516 "-*- texinfo -*-\n\
1517@deftypefn {Built-in Function} {} WIFSTOPPED (@var{status})\n\
1518Given @var{status} from a call to @code{waitpid}, return true if the\n\
1519child process was stopped by delivery of a signal; this is only\n\
1520possible if the call was done using @code{WUNTRACED} or when the child\n\
1521is being traced (see ptrace(2)).\n\
1522@seealso{waitpid, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WCOREDUMP, WSTOPSIG, WIFCONTINUED}\n\
1523@end deftypefn")
1524{
1525 octave_value retval = 0.0;
1526
1527#if defined (WIFSTOPPED)
1528 if (args.length () == 1)
1529 {
1530 int status = args(0).int_value ();
1531
1532 if (! error_state)
1533 retval = WIFSTOPPED (status);
1534 else
1535 error ("WIFSTOPPED: expecting integer argument");
1536 }
1537#else
1538 warning ("WIFSTOPPED always returns false in this version of Octave");
1539#endif
1540
1541 return retval;
1542}
1543
1544DEFUNX ("WSTOPSIG", FWSTOPSIG, args, ,
1545 "-*- texinfo -*-\n\
1546@deftypefn {Built-in Function} {} WSTOPSIG (@var{status})\n\
1547Given @var{status} from a call to @code{waitpid}, return the number of\n\
1548the signal which caused the child to stop. This function should only\n\
1549be employed if @code{WIFSTOPPED} returned true.\n\
1550@seealso{waitpid, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WCOREDUMP, WIFSTOPPED, WIFCONTINUED}\n\
1551@end deftypefn")
1552{
1553 octave_value retval = 0.0;
1554
1555#if defined (WSTOPSIG)
1556 if (args.length () == 1)
1557 {
1558 int status = args(0).int_value ();
1559
1560 if (! error_state)
1561 retval = WSTOPSIG (status);
1562 else
1563 error ("WSTOPSIG: expecting integer argument");
1564 }
1565#else
1566 warning ("WSTOPSIG always returns false in this version of Octave");
1567#endif
1568
1569 return retval;
1570}
1571
1572DEFUNX ("WIFCONTINUED", FWIFCONTINUED, args, ,
1573 "-*- texinfo -*-\n\
1574@deftypefn {Built-in Function} {} WIFCONTINUED (@var{status})\n\
1575Given @var{status} from a call to @code{waitpid}, return true if the\n\
1576child process was resumed by delivery of @code{SIGCONT}.\n\
1577@seealso{waitpid, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WCOREDUMP, WIFSTOPPED, WSTOPSIG}\n\
1578@end deftypefn")
1579{
1580 octave_value retval = 0.0;
1581
1582#if defined (WIFCONTINUED)
1583 if (args.length () == 1)
1584 {
1585 int status = args(0).int_value ();
1586
1587 if (! error_state)
1588 retval = WIFCONTINUED (status);
1589 else
1590 error ("WIFCONTINUED: expecting integer argument");
1591 }
1592#else
1593 warning ("WIFCONTINUED always returns false in this version of Octave");
1594#endif
1595
1596 return retval;
1597}
1598
1599DEFUNX ("canonicalize_file_name", Fcanonicalize_file_name, args, ,
1600 "-*- texinfo -*-\n\
1601@deftypefn {Built-in Function} {[@var{cname}, @var{status}, @var{msg}]} canonicalize_file_name (@var{name})\n\
1602Return the canonical name of file @var{name}.\n\
1603@end deftypefn")
1604{
1605 octave_value_list retval;
1606
1607 if (args.length () == 1)
1608 {
1609 std::string name = args(0).string_value ();
1610
1611 if (! error_state)
1612 {
1613 std::string msg;
1614
1615 std::string result = octave_canonicalize_file_name (name, msg);
1616
1617 retval(2) = msg;
1618 retval(1) = msg.empty () ? 0 : -1;
1619 retval(0) = result;
1620 }
1621 else
1622 error ("canonicalize_file_name: argument must be a character string");
1623 }
1624 else
1625 print_usage ();
1626
1627 return retval;
1628}
1629
1630static octave_value
1631const_value (const octave_value_list& args, int val)
1632{
1633 octave_value retval;
1634
1635 int nargin = args.length ();
1636
1637 if (nargin == 0)
1638 retval = val;
1639 else
1640 print_usage ();
1641
1642 return retval;
1643}
1644
1645#if !defined (O_NONBLOCK) && defined (O_NDELAY)
1646#define O_NONBLOCK O_NDELAY
1647#endif
1648
1649#if defined (F_DUPFD)
1650DEFUNX ("F_DUPFD", FF_DUPFD, args, ,
1651 "-*- texinfo -*-\n\
1652@deftypefn {Built-in Function} {} F_DUPFD ()\n\
1653Return the value required to request that @code{fcntl} return a\n\
1654duplicate file descriptor.\n\
1655@seealso{fcntl, F_GETFD, F_GETFL, F_SETFD, F_SETFL}\n\
1656@end deftypefn")
1657{
1658 return const_value (args, F_DUPFD);
1659}
1660#endif
1661
1662#if defined (F_GETFD)
1663DEFUNX ("F_GETFD", FF_GETFD, args, ,
1664 "-*- texinfo -*-\n\
1665@deftypefn {Built-in Function} {} F_GETFD ()\n\
1666Return the value required to request that @code{fcntl} to return the\n\
1667file descriptor flags.\n\
1668@seealso{fcntl, F_DUPFD, F_GETFL, F_SETFD, F_SETFL}\n\
1669@end deftypefn")
1670{
1671 return const_value (args, F_GETFD);
1672}
1673#endif
1674
1675#if defined (F_GETFL)
1676DEFUNX ("F_GETFL", FF_GETFL, args, ,
1677 "-*- texinfo -*-\n\
1678@deftypefn {Built-in Function} {} F_GETFL ()\n\
1679Return the value required to request that @code{fcntl} to return the\n\
1680file status flags.\n\
1681@seealso{fcntl, F_DUPFD, F_GETFD, F_SETFD, F_SETFL}\n\
1682@end deftypefn")
1683{
1684 return const_value (args, F_GETFL);
1685}
1686#endif
1687
1688#if defined (F_SETFD)
1689DEFUNX ("F_SETFD", FF_SETFD, args, ,
1690 "-*- texinfo -*-\n\
1691@deftypefn {Built-in Function} {} F_SETFD ()\n\
1692Return the value required to request that @code{fcntl} to set the file\n\
1693descriptor flags.\n\
1694@seealso{fcntl, F_DUPFD, F_GETFD, F_GETFL, F_SETFL}\n\
1695@end deftypefn")
1696{
1697 return const_value (args, F_SETFD);
1698}
1699#endif
1700
1701#if defined (F_SETFL)
1702DEFUNX ("F_SETFL", FF_SETFL, args, ,
1703 "-*- texinfo -*-\n\
1704@deftypefn {Built-in Function} {} F_SETFL ()\n\
1705Return the value required to request that @code{fcntl} to set the file\n\
1706status flags.\n\
1707@seealso{fcntl, F_DUPFD, F_GETFD, F_GETFL, F_SETFD}\n\
1708@end deftypefn")
1709{
1710 return const_value (args, F_SETFL);
1711}
1712#endif
1713
1714#if defined (O_APPEND)
1715DEFUNX ("O_APPEND", FO_APPEND, args, ,
1716 "-*- texinfo -*-\n\
1717@deftypefn {Built-in Function} {} O_APPEND ()\n\
1718Return the numerical value of the file status flag that may be\n\
1719returned by @code{fcntl} to indicate each write operation appends,\n\
1720or that may be passed to @code{fcntl} to set the write mode to append.\\n\
1721@seealso{fcntl, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\
1722@end deftypefn")
1723{
1724 return const_value (args, O_APPEND);
1725}
1726#endif
1727
1728#if defined (O_ASYNC)
1729DEFUNX ("O_ASYNC", FO_ASYNC, args, ,
1730 "-*- texinfo -*-\n\
1731@deftypefn {Built-in Function} {} O_ASYNC ()\n\
1732Return the numerical value of the file status flag that may be\n\
1733returned by @code{fcntl} to indicate asynchronous I/O.\n\
1734@seealso{fcntl, O_APPEND, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\
1735@end deftypefn")
1736{
1737 return const_value (args, O_ASYNC);
1738}
1739#endif
1740
1741#if defined (O_CREAT)
1742DEFUNX ("O_CREAT", FO_CREAT, args, ,
1743 "-*- texinfo -*-\n\
1744@deftypefn {Built-in Function} {} O_CREAT ()\n\
1745Return the numerical value of the file status flag that may be\n\
1746returned by @code{fcntl} to indicate that a file should be\n\
1747created if it does not exist.\n\
1748@seealso{fcntl, O_APPEND, O_ASYNC, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\
1749@end deftypefn")
1750{
1751 return const_value (args, O_CREAT);
1752}
1753#endif
1754
1755#if defined (O_EXCL)
1756DEFUNX ("O_EXCL", FO_EXCL, args, ,
1757 "-*- texinfo -*-\n\
1758@deftypefn {Built-in Function} {} O_EXCL ()\n\
1759Return the numerical value of the file status flag that may be\n\
1760returned by @code{fcntl} to indicate that file locking is used.\n\
1761@seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\
1762@end deftypefn")
1763{
1764 return const_value (args, O_EXCL);
1765}
1766#endif
1767
1768#if defined (O_NONBLOCK)
1769DEFUNX ("O_NONBLOCK", FO_NONBLOCK, args, ,
1770 "-*- texinfo -*-\n\
1771@deftypefn {Built-in Function} {} O_NONBLOCK ()\n\
1772Return the numerical value of the file status flag that may be\n\
1773returned by @code{fcntl} to indicate that non-blocking I/O is in use,\n\
1774or that may be passsed to @code{fcntl} to set non-blocking I/O.\n\
1775@seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\
1776@end deftypefn")
1777{
1778 return const_value (args, O_NONBLOCK);
1779}
1780#endif
1781
1782#if defined (O_RDONLY)
1783DEFUNX ("O_RDONLY", FO_RDONLY, args, ,
1784 "-*- texinfo -*-\n\
1785@deftypefn {Built-in Function} {} O_RDONLY ()\n\
1786Return the numerical value of the file status flag that may be\n\
1787returned by @code{fcntl} to indicate that a file is open for\n\
1788reading only.\n\
1789@seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY}\n\
1790@end deftypefn")
1791{
1792 return const_value (args, O_RDONLY);
1793}
1794#endif
1795
1796#if defined (O_RDWR)
1797DEFUNX ("O_RDWR", FO_RDWR, args, ,
1798 "-*- texinfo -*-\n\
1799@deftypefn {Built-in Function} {} O_RDWR ()\n\
1800Return the numerical value of the file status flag that may be\n\
1801returned by @code{fcntl} to indicate that a file is open for both\n\
1802reading and writing.\n\
1803@seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_SYNC, O_TRUNC, O_WRONLY}\n\
1804@end deftypefn")
1805{
1806 return const_value (args, O_RDWR);
1807}
1808#endif
1809
1810#if defined (O_SYNC)
1811DEFUNX ("O_SYNC", FO_SYNC, args, ,
1812 "-*- texinfo -*-\n\
1813@deftypefn {Built-in Function} {} O_SYNC ()\n\
1814Return the numerical value of the file status flag that may be\n\
1815returned by @code{fcntl} to indicate that a file is open for\n\
1816synchronous I/O.\n\
1817@seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY}\n\
1818@end deftypefn")
1819{
1820 return const_value (args, O_SYNC);
1821}
1822#endif
1823
1824#if defined (O_TRUNC)
1825DEFUNX ("O_TRUNC", FO_TRUNC, args, ,
1826 "-*- texinfo -*-\n\
1827@deftypefn {Built-in Variable} O_TRUNC ()\n\
1828Return the numerical value of the file status flag that may be\n\
1829returned by @code{fcntl} to indicate that if file exists, it should\n\
1830be truncated when writing.\n\
1831@seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_WRONLY}\n\
1832@end deftypefn")
1833{
1834 return const_value (args, O_TRUNC);
1835}
1836#endif
1837
1838#if defined (O_WRONLY)
1839DEFUNX ("O_WRONLY", FO_WRONLY, args, ,
1840 "-*- texinfo -*-\n\
1841@deftypefn {Built-in Function} {} O_WRONLY ()\n\
1842Return the numerical value of the file status flag that may be\n\
1843returned by @code{fcntl} to indicate that a file is open for\n\
1844writing only.\n\
1845@seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC}\n\
1846@end deftypefn")
1847{
1848 return const_value (args, O_WRONLY);
1849}
1850#endif
1851
1852#if !defined (WNOHANG)
1853#define WNOHANG 0
1854#endif
1855
1856DEFUNX ("WNOHANG", FWNOHANG, args, ,
1857 "-*- texinfo -*-\n\
1858@deftypefn {Built-in Function} {} WNOHANG ()\n\
1859Return the numerical value of the option argument that may be\n\
1860passed to @code{waitpid} to indicate that it should return its\n\
1861status immediately instead of waiting for a process to exit.\n\
1862@seealso{waitpid, WUNTRACED, WCONTINUE}\n\
1863@end deftypefn")
1864{
1865 return const_value (args, WNOHANG);
1866}
1867
1868#if !defined (WUNTRACED)
1869#define WUNTRACED 0
1870#endif
1871
1872DEFUNX ("WUNTRACED", FWUNTRACED, args, ,
1873 "-*- texinfo -*-\n\
1874@deftypefn {Built-in Function} {} WUNTRACED ()\n\
1875Return the numerical value of the option argument that may be\n\
1876passed to @code{waitpid} to indicate that it should also return\n\
1877if the child process has stopped but is not traced via the\n\
1878@code{ptrace} system call\n\
1879@seealso{waitpid, WNOHANG, WCONTINUE}\n\
1880@end deftypefn")
1881{
1882 return const_value (args, WUNTRACED);
1883}
1884
1885#if !defined (WCONTINUE)
1886#define WCONTINUE 0
1887#endif
1888
1889DEFUNX ("WCONTINUE", FWCONTINUE, args, ,
1890 "-*- texinfo -*-\n\
1891@deftypefn {Built-in Function} {} WCONTINUE ()\n\
1892Return the numerical value of the option argument that may be\n\
1893passed to @code{waitpid} to indicate that it should also return\n\
1894if a stopped child has been resumed by delivery of a @code{SIGCONT}\n\
1895signal.\n\
1896@seealso{waitpid, WNOHANG, WUNTRACED}\n\
1897@end deftypefn")
1898{
1899 return const_value (args, WCONTINUE);
1900}