Imported from ../bash-2.05.tar.gz.
[platform/upstream/bash.git] / builtins / evalfile.c
index 14fbc4d..1116b09 100644 (file)
@@ -4,7 +4,7 @@
 
    Bash is free software; you can redistribute it and/or modify it under
    the terms of the GNU General Public License as published by the Free
-   Software Foundation; either version 1, or (at your option) any later
+   Software Foundation; either version 2, or (at your option) any later
    version.
 
    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
@@ -14,7 +14,7 @@
    
    You should have received a copy of the GNU General Public License along
    with Bash; see the file COPYING.  If not, write to the Free Software
-   Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+   Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 
 #include <config.h>
 
@@ -22,9 +22,9 @@
 #  include <unistd.h>
 #endif
 
-#include <sys/types.h>
-#include "../posixstat.h"
-#include "../filecntl.h"
+#include "../bashtypes.h"
+#include "posixstat.h"
+#include "filecntl.h"
 
 #include <stdio.h>
 #include <signal.h>
@@ -55,6 +55,8 @@ extern int errno;
 #define FEVAL_UNWINDPROT       0x004
 #define FEVAL_NONINT           0x008
 #define FEVAL_LONGJMP          0x010
+#define FEVAL_HISTORY          0x020
+#define FEVAL_CHECKBINARY      0x040
 
 extern int interactive, interactive_shell, posixly_correct;
 extern int indirection_level, startup_state, subshell_environment;
@@ -71,9 +73,10 @@ _evalfile (filename, flags)
 {
   volatile int old_interactive;
   procenv_t old_return_catch;
-  int return_val, fd, result;
+  int return_val, fd, result, pflags;
   char *string;
   struct stat finfo;
+  size_t file_size;
   VFunction *errfunc;
 
   fd = open (filename, O_RDONLY);
@@ -85,7 +88,7 @@ file_error_and_exit:
        file_error (filename);
 
       if (flags & FEVAL_LONGJMP)
-        {
+       {
          last_command_exit_value = 1;
          jump_to_top_level (EXITPROG);
        }
@@ -107,21 +110,40 @@ file_error_and_exit:
       return ((flags & FEVAL_BUILTIN) ? EXECUTION_FAILURE : -1);
     }
 
-  string = xmalloc (1 + (int)finfo.st_size);
-  result = read (fd, string, finfo.st_size);
+  file_size = (size_t)finfo.st_size;
+  /* Check for overflow with large files. */
+  if (file_size != finfo.st_size || file_size + 1 < file_size)
+    {
+      (*errfunc) ("%s: file is too large", filename);
+      return ((flags & FEVAL_BUILTIN) ? EXECUTION_FAILURE : -1);
+    }      
+
+#if defined (__CYGWIN__) && defined (O_TEXT)
+  setmode (fd, O_TEXT);
+#endif
+
+  string = xmalloc (1 + file_size);
+  result = read (fd, string, file_size);
   string[result] = '\0';
 
   return_val = errno;
   close (fd);
   errno = return_val;
 
-  if (result != (int)finfo.st_size)
+  if (result < 0)              /* XXX was != file_size, not < 0 */
     {
       free (string);
       goto file_error_and_exit;
     }
 
-  if (check_binary_file ((unsigned char *)string, (result > 80) ? 80 : result))
+  if (result == 0)
+    {
+      free (string);
+      return ((flags & FEVAL_BUILTIN) ? EXECUTION_SUCCESS : 1);
+    }
+      
+  if ((flags & FEVAL_CHECKBINARY) && 
+      check_binary_file ((unsigned char *)string, (result > 80) ? 80 : result))
     {
       free (string);
       (*errfunc) ("%s: cannot execute binary file", filename);
@@ -151,6 +173,9 @@ file_error_and_exit:
   return_catch_flag++;
   sourcelevel++;
 
+  /* set the flags to be passed to parse_and_execute */
+  pflags = (flags & FEVAL_HISTORY) ? 0 : SEVAL_NOHIST;
+
   if (flags & FEVAL_BUILTIN)
     result = EXECUTION_SUCCESS;
 
@@ -164,7 +189,7 @@ file_error_and_exit:
       result = return_catch_value;
     }
   else
-    result = parse_and_execute (string, filename, -1);
+    result = parse_and_execute (string, filename, pflags);
 
   if (flags & FEVAL_UNWINDPROT)
     run_unwind_frame ("_evalfile");
@@ -197,6 +222,20 @@ maybe_execute_file (fname, force_noninteractive)
   return result;
 }
 
+#if defined (HISTORY)
+int
+fc_execute_file (filename)
+     char *filename;
+{
+  int flags;
+
+  /* We want these commands to show up in the history list if
+     remember_on_history is set. */
+  flags = FEVAL_ENOENTOK|FEVAL_HISTORY;
+  return (_evalfile (filename, flags));
+}
+#endif /* HISTORY */
+
 int
 source_file (filename)
      char *filename;