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