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