changeset 2669:c821858188b6

[project @ 1997-02-13 18:25:39 by jwe]
author jwe
date Thu, 13 Feb 1997 18:25:40 +0000
parents 0d865ef7478f
children 18192eea4973
files src/ChangeLog src/dirfns.cc src/syscalls.cc
diffstat 3 files changed, 227 insertions(+), 82 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,20 @@
+Thu Feb 13 03:02:08 1997  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* gripes.cc (gripe_wrong_type_arg (const char*, const string&)):
+	New function.
+
+Wed Feb 12 17:27:53 1997  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* syscalls.cc (symbols_of_syscalls): Add O_ASYNC and O_SYNC.
+
+Mon Feb 10 01:22:27 1997  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* dirfns.cc (Freaddir, Fmkdir, Frmdir, Frename):
+	Also return status and error message.
+
+	* syscalls.cc (Fdup2, Fexec, Ffork, Ffcntl, Funlink, Fmkfifo,
+	Fpipe, Fwaitpid): Also return error message.
+
 Sat Feb  8 17:16:09 1997  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* pt-exp.cc (tree_simple_assignment_expression::eval): Return
--- a/src/dirfns.cc
+++ b/src/dirfns.cc
@@ -441,14 +441,20 @@
 }
 
 DEFUN (readdir, args, ,
-  "readdir (NAME)\n\
+  "[FILES, STATUS, MSG] = readdir (NAME)\n\
 \n\
 Return an array of strings containing the list of all files in the
-named directory.  If sucessful, returns 0; otherwise an error message
-is printed.")
+named directory in FILES, or an empty matrix if an error occurs\n\
+\n\
+If successful, STATUS is 0 and MSG is an empty string.  Otherwise,\n\
+STATUS is nonzero and MSG contains a system-dependent error message.")
 {
   octave_value_list retval;
 
+  retval(2) = string ();
+  retval(1) = -1.0;
+  retval(0) = Matrix ();
+
   if (args.length () == 1)
     {
       string dirname = args(0).string_value ();
@@ -463,11 +469,11 @@
 	    {
 	      string_vector dirlist = dir.read ();
 	      retval(0) = dirlist.qsort ();
+	      retval(1) = 0.0;
 	    }
 	  else
 	    {
-	      string msg = dir.error ();
-	      error ("%s", msg.c_str ());
+	      retval(2) = dir.error ();
 	    }
 	}
     }
@@ -481,14 +487,17 @@
 // mode.
 
 DEFUN (mkdir, args, ,
-  "mkdir (NAME)\n\
+  "[STATUS, MSG] = mkdir (NAME)\n\
 \n\
-Create the directory named by NAME.  If successful, returns 0;\n\
-otherwise prints an error message.")
+Create the directory named by NAME.\n\
+\n\
+If successful, STATUS is 0 and MSG is an empty string.  Otherwise,\n\
+STATUS is nonzero and MSG contains a system-dependent error message.")
 {
   octave_value_list retval;
 
-  int status = 0;
+  retval(1) = string ();
+  retval(0) = -1.0;
 
   if (args.length () == 1)
     {
@@ -498,33 +507,35 @@
 	gripe_wrong_type_arg ("mkdir", args(0));
       else
 	{
-	  int mkdir_retval = oct_mkdir (oct_tilde_expand (dirname), 0777);
+	  string msg;
+
+	  int status = oct_mkdir (oct_tilde_expand (dirname),
+				  0777, msg);
 
-	  if (mkdir_retval < 0)
-	    {
-	      status = -1;
-	      error ("%s", strerror (errno));
-	    }
+	  retval(0) = (double) status;
+
+	  if (status < 0)
+	    retval(1) = msg;
 	}
     }
   else
     print_usage ("mkdir");
 
-  if (status == 0)
-    retval (0) = (double) status;
-
   return retval;
 }
 
 DEFUN (rmdir, args, ,
-  "rmdir (NAME)\n\
+  "[STATUS, MSG] = rmdir (NAME)\n\
 \n\
-Remove the directory named by NAME.  If successful, returns 0;\n\
-otherwise prints an error message.")
+Remove the directory named by NAME.\n\
+\n\
+If successful, STATUS is 0 and MSG is an empty string.  Otherwise,\n\
+STATUS is nonzero and MSG contains a system-dependent error message.")
 {
   octave_value_list retval;
 
-  int status = 0;
+  retval(1) = string ();
+  retval(0) = -1.0;
 
   if (args.length () == 1)
     {
@@ -534,33 +545,34 @@
 	gripe_wrong_type_arg ("rmdir", args(0));
       else
 	{
-	  int rmdir_retval = oct_rmdir (oct_tilde_expand (dirname));
+	  string msg;
+
+	  int status = oct_rmdir (oct_tilde_expand (dirname), msg);
 
-	  if (rmdir_retval < 0)
-	    {
-	      status = -1;
-	      error ("%s", strerror (errno));
-	    }
+	  retval(0) = (double) status;
+
+	  if (status < 0)
+	    retval(1) = msg;
 	}
     }
   else
     print_usage ("rmdir");
 
-  if (status == 0)
-    retval (0) = (double) status;
-
   return retval;
 }
 
 DEFUN (rename, args, ,
-  "rename (FROM, TO)\n\
+  "[STATUS, MSG] = rename (FROM, TO)\n\
 \n\
-Rename a file.  If successful, returns 0;\n\
-otherwise prints an error message and returns -1.")
+Rename a file.\n\
+\n\
+If successful, STATUS is 0 and MSG is an empty string.  Otherwise,\n\
+STATUS is nonzero and MSG contains a system-dependent error message.")
 {
   octave_value_list retval;
 
-  int status = 0;
+  retval(1) = string ();
+  retval(0) = -1.0;
 
   if (args.length () == 2)
     {
@@ -574,19 +586,22 @@
 
 	  if (error_state)
 	    gripe_wrong_type_arg ("rename", args(1));
-	  else if (oct_rename (from, to) < 0)
+	  else
 	    {
-	      status = -1;
-	      error ("%s", strerror (errno));
+	      string msg;
+
+	      int status = oct_rename (from, to, msg);
+
+	      retval(0) = (double) status;
+
+	      if (status < 0)
+		retval(1) = msg;
 	    }
 	}
     }
   else
     print_usage ("rename");
 
-  if (status == 0)
-    retval (0) = (double) status;
-
   return retval;
 }
 
--- a/src/syscalls.cc
+++ b/src/syscalls.cc
@@ -29,7 +29,9 @@
 #include <config.h>
 #endif
 
+#include <cerrno>
 #include <cstdio>
+#include <cstring>
 
 #ifdef HAVE_UNISTD_H
 #ifdef HAVE_SYS_TYPES_H
@@ -86,9 +88,17 @@
 }
 
 DEFUN (dup2, args, ,
- "fid = dup2 (old, new): duplicate a file descriptor")
+ "[FID, MSG] = dup2 (OLD, NEW)\n\
+\n\
+Duplicate a file descriptor.\n\
+\n\
+If successful, FID is greater than zero and contains the new file ID.\n\
+Otherwise, FID is negative and MSG contains a system-dependent error message.")
 {
-  double retval = -1.0;
+  octave_value_list retval;
+
+  retval(1) = string ();
+  retval(0) = -1.0;
 
   int nargin = args.length ();
 
@@ -107,7 +117,14 @@
 
 	      // XXX FIXME XXX -- are these checks sufficient?
 	      if (i_old >= 0 && i_new >= 0)
-		retval = (double) dup2 (i_old, i_new);
+		{
+		  int status = dup2 (i_old, i_new);
+
+		  retval(0) = (double) status;
+
+		  if (status < 0)
+		    retval(1) = strerror (errno);
+		}
 	      else
 		error ("dup2: invalid file id");
 	    }
@@ -125,9 +142,17 @@
 }
 
 DEFUN (exec, args, ,
- "exec (file, args): replace current process with a new process")
+ "[STATUS, MSG] = exec (FILE, ARGS)\n\
+\n\
+Replace current process with a new process.\n\
+\n\
+If successful, exec does not return.  If exec does return, status will
+be nonzero, and MSG will contain a system-dependent error message.")
 {
-  double retval = -1.0;
+  octave_value_list retval;
+
+  retval(1) = string ();
+  retval(0) = -1.0;
 
   int nargin = args.length ();
 
@@ -178,7 +203,14 @@
 	    }
 
 	  if (! error_state)
-	    execvp (exec_file.c_str (), exec_args);
+	    {
+	      int status = execvp (exec_file.c_str (), exec_args);
+
+	      retval(0) = (double) status;
+
+	      if (status < 0)
+		retval(1) = strerror (errno);
+	    }
 	}
       else
 	error ("exec: first argument must be a string");
@@ -193,9 +225,17 @@
 }
 
 DEFUN (fcntl, args, ,
- "fcntl (fid, request, argument): control open file descriptors")
+ "[STATUS, MSG] = fcntl (FID, REQUEST, ARGUMENT)\n\
+\n\
+Control open file descriptors.\n\
+\n\
+If successful, STATUS is 0 and MSG is an empty string.  Otherwise,\n\
+STATUS is nonzero and MSG contains a system-dependent error message.")
 {
-  double retval = -1.0;
+  octave_value_list retval;
+
+  retval(1) = string ();
+  retval(0) = -1.0;
 
   int nargin = args.length ();
 
@@ -219,7 +259,14 @@
 	  if (fid < 0)
 	    error ("fcntl: invalid file id");
 	  else
-	    retval = fcntl (fid, req, arg);
+	    {
+	      int status = fcntl (fid, req, arg);
+
+	      retval(0) = (double) status;
+
+	      if (status < 0)
+		retval(1) = strerror (errno);
+	    }
 	}
       else
 	error ("fcntl: file id must be an integer");
@@ -234,16 +281,30 @@
 }
 
 DEFUN (fork, args, ,
- "fork (): create a copy of the current process")
+ "[PID, MSG] = fork ()\n\
+\n\
+Create a copy of the current process.\n\
+\n\
+If successful, PID is either the process ID and you are in the parent,\n\
+or 0, and you are in the child.  If PID is less than zero, an error\n\
+has occured, and MSG contains a system-dependent error message.")
 {
-  double retval = -1.0;
+  octave_value_list retval;
+
+  retval(1) = string ();
+  retval(0) = -1.0;
 
   int nargin = args.length ();
 
   if (nargin == 0)
     {
 #if defined (HAVE_FORK)
-      retval = fork ();
+      pid_t pid = fork ();
+
+      retval(0) = (double) pid;
+
+      if (pid < 0)
+	retval(1) = strerror (errno);
 #else
       gripe_not_supported ("fork");
 #endif
@@ -439,16 +500,17 @@
 }
 
 DEFUN (mkfifo, args, ,
-  "STATUS = mkfifo (NAME, MODE)\n\
+  "[STATUS, MSG] = mkfifo (NAME, MODE)\n\
 \n\
-  Create a FIFO special file named NAME with file mode MODE\n\
+Create a FIFO special file named NAME with file mode MODE\n\
 \n\
-  STATUS is:\n\
-\n\
-    != 0 : if mkfifo failed\n\
-       0 : if the FIFO special file could be created")
+If successful, STATUS is 0 and MSG is an empty string.  Otherwise,\n\
+STATUS is nonzero and MSG contains a system-dependent error message.")
 {
-  double retval = -1.0;
+  octave_value_list retval;
+
+  retval(1) = string ();
+  retval(0) = -1.0;
 
   int nargin = args.length ();
 
@@ -462,7 +524,14 @@
 	    {
 	      long mode = (long) args(1).double_value ();
 
-	      retval = oct_mkfifo (name, mode);
+	      string msg;
+
+	      int status = oct_mkfifo (name, mode, msg);
+
+	      retval(0) = (double) status;
+
+	      if (status < 0)
+		retval(1) = msg;
 	    }
 	  else
 	    error ("mkfifo: MODE must be an integer");
@@ -478,9 +547,19 @@
 }
 
 DEFUN (pipe, args, ,
-  "[file_ids, status] = pipe (): create an interprocess channel")
+  "[FILE_IDS, STATUS, MSG] = pipe (): create an interprocess channel.\n\
+\n\
+Return the FILE_IDS corresponding to the reading and writing ends of\n\
+the pipe, as a vector.\n\
+\n\
+If successful, STATUS is 0 and MSG is an empty string.  Otherwise,\n\
+STATUS is nonzero and MSG contains a system-dependent error message.")
 {
-  octave_value_list retval (2, octave_value (-1.0));
+  octave_value_list retval;
+
+  retval(2) = string ();
+  retval(1) = -1.0;
+  retval(0) = Matrix ();
 
   int nargin = args.length ();
 
@@ -489,7 +568,13 @@
 #if defined (HAVE_PIPE)
       int fid[2];
 
-      if (pipe (fid) >= 0)
+      int status = pipe (fid);
+
+      if (status < 0)
+	{
+	  retval(2) = strerror (errno);
+	}
+      else
 	{
 	  FILE *in_file = fdopen (fid[0], "r");
 	  FILE *out_file = fdopen (fid[1], "w");
@@ -506,8 +591,8 @@
 	  file_ids (0, 1) = octave_stream_list::insert (os);
 
           retval(0) = file_ids;
-	  retval(1) = 0.0;
-	}	  
+	  retval(1) = (double) status;
+	}
 #else
       gripe_not_supported ("pipe");
 #endif
@@ -575,16 +660,17 @@
 }
 
 DEFUN (unlink, args, ,
-  "STATUS = unlink (NAME)\n\
+  "[STATUS, MSG] = unlink (NAME)\n\
 \n\
-  Delete the file NAME\n\
+Delete the file NAME\n\
 \n\
-  STATUS is:\n\
-\n\
-    != 0 : if unlink failed\n\
-       0 : if the file could be successfully deleted")
+If successful, STATUS is 0 and MSG is an empty string.  Otherwise,\n\
+STATUS is nonzero and MSG contains a system-dependent error message.")
 {
-  double retval = -1.0;
+  octave_value_list retval;
+
+  retval(1) = string ();
+  retval(0) = -1.0;
 
   int nargin = args.length ();
 
@@ -594,7 +680,14 @@
 	{
 	  string name = args(0).string_value ();
 
-	  retval = oct_unlink (name);
+	  string msg;
+
+	  int status = oct_unlink (name, msg);
+
+	  retval(0) = (double) status;
+
+	  if (status < 0)
+	    retval(1) = msg;	    
 	}
       else
 	error ("unlink: file name must be a string");
@@ -606,9 +699,9 @@
 }
 
 DEFUN (waitpid, args, ,
-  "STATUS = waitpid (PID, OPTIONS)\n\
+  "[PID, MSG] = waitpid (PID, OPTIONS)\n\
 \n\
-  wait for process PID to terminate\n\
+Wait for process PID to terminate\n\
 \n\
   PID can be:\n\
 \n\
@@ -627,12 +720,15 @@
          since they stopped\n\
      3 : implies both 1 and 2\n\
 \n\
-  STATUS is:\n\
-\n\
-     -1 : if an error occured\n\
-    > 0 : the process ID of the child process that exited")
+If successful, PID is greater than 0 and contains the process ID of\n\
+the child process that exited and MSG is an empty string.\n\
+Otherwise, PID is less than zero and MSG contains a system-dependent\n\
+error message.")
 {
-  double retval = -1.0;
+  octave_value_list retval;
+
+  retval(1) = string ();
+  retval(0) = -1.0;
 
   int nargin = args.length ();
 
@@ -669,7 +765,14 @@
 		}
 
 	      if (! error_state)
-		retval = waitpid (pid, 0, options);
+		{
+		  pid_t status = waitpid (pid, 0, options);
+
+		  retval(0) = (double) status;
+
+		  if (status < 0)
+		    retval(1) = strerror (errno);
+		}
 	    }
 	}
 #else
@@ -719,6 +822,11 @@
     "");
 #endif
 
+#if defined (O_ASYNC)
+  DEFCONST (O_ASYNC, (double) O_ASYNC, 0, 0,
+    "");
+#endif
+
 #if defined (O_CREAT)
   DEFCONST (O_CREAT, (double) O_CREAT, 0, 0,
     "");
@@ -744,6 +852,11 @@
     "");
 #endif
 
+#if defined (O_SYNC)
+  DEFCONST (O_SYNC, (double) O_SYNC, 0, 0,
+    "");
+#endif
+
 #if defined (O_TRUNC)
   DEFCONST (O_TRUNC, (double) O_TRUNC, 0, 0,
     "");