#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include DEFUN_DLD (gpipe, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} address@hidden, @var{err}, @var{msg}] =} gpipe ()\n\ Create a pipe and return the vector @var{file_ids}, which corresponding\n\ to the reading and writing ends of the pipe.\n\ \n\ If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ system-dependent error message.\n\ @end deftypefn") { octave_value_list retval; retval(2) = std::string (); retval(1) = -1.0; retval(0) = Matrix (); int nargin = args.length (); if (nargin == 0) { int fid[2]; double ipipe,opipe; std::string msg; int status = octave_syscalls::pipe (fid, msg); if (status < 0) retval(2) = msg; else { FILE *ifile = fdopen (fid[0], "r"); FILE *ofile = fdopen (fid[1], "w"); octave_stream is = octave_istdiostream::create (std::string (), ifile); octave_stream os = octave_ostdiostream::create (std::string (), ofile); //Both of the following approaches yield the same result: //The function works fine when invoked from octave command line //but gives the wrong answer when invoked from a .m file //ipipe = fid[0]; //opipe = fid[1]; ipipe = (double) is.file_number(); opipe = (double) os.file_number(); octave_value_list file_ids; file_ids(3) = ipipe; file_ids(2) = opipe; file_ids(1) = octave_stream_list::insert (os); file_ids(0) = octave_stream_list::insert (is); retval(1) = static_cast (status); retval(0) = octave_value (file_ids); } } else print_usage ("gpipe"); return retval; }