changelog shortlog tags changeset files revisions annotate raw

src/syscalls.cc

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