Bash-4.2 distribution sources and documentation
[platform/upstream/bash.git] / builtins / exec.def
index 4e1394d..5d1e625 100644 (file)
@@ -1,43 +1,50 @@
 This file is exec.def, from which is created exec.c.
 It implements the builtin "exec" in Bash.
 
-Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
+Copyright (C) 1987-2010 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
-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
-version.
+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 3 of the License, or
+(at your option) any later version.
 
-Bash is distributed in the hope that it will be useful, but WITHOUT ANY
-WARRANTY; without even the implied warranty of MERCHANTABILITY or
-FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-for more details.
+Bash is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
 
-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.
+You should have received a copy of the GNU General Public License
+along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 
 $PRODUCES exec.c
 
 $BUILTIN exec
 $FUNCTION exec_builtin
-$SHORT_DOC exec [-cl] [-a name] file [redirection ...]
-Exec FILE, replacing this shell with the specified program.
-If FILE is not specified, the redirections take effect in this
-shell.  If the first argument is `-l', then place a dash in the
-zeroth arg passed to FILE, as login does.  If the `-c' option
-is supplied, FILE is executed with a null environment.  The `-a'
-option means to make set argv[0] of the executed process to NAME.
-If the file cannot be executed and the shell is not interactive,
-then the shell exits, unless the shell option `execfail' is set.
+$SHORT_DOC exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
+Replace the shell with the given command.
+
+Execute COMMAND, replacing this shell with the specified program.
+ARGUMENTS become the arguments to COMMAND.  If COMMAND is not specified,
+any redirections take effect in the current shell.
+
+Options:
+  -a name      pass NAME as the zeroth argument to COMMAND
+  -c           execute COMMAND with an empty environment
+  -l           place a dash in the zeroth argument to COMMAND
+
+If the command cannot be executed, a non-interactive shell exits, unless
+the shell option `execfail' is set.
+
+Exit Status:
+Returns success unless COMMAND is not found or a redirection error occurs.
 $END
 
 #include <config.h>
 
 #include "../bashtypes.h"
-#include "../posixstat.h"
+#include "posixstat.h"
 #include <signal.h>
 #include <errno.h>
 
@@ -46,6 +53,7 @@ $END
 #endif
 
 #include "../bashansi.h"
+#include "../bashintl.h"
 
 #include "../shell.h"
 #include "../execute_cmd.h"
@@ -66,8 +74,9 @@ $END
 extern int errno;
 #endif /* !errno */
 
-extern int interactive, interactive_shell, subshell_environment;
+extern int subshell_environment;
 extern REDIRECT *redirection_undo_list;
+extern char *exec_argv0;
 
 int no_exit_on_failed_exec;
 
@@ -79,7 +88,7 @@ mkdashname (name)
 {
   char *ret;
 
-  ret = xmalloc (2 + strlen (name));
+  ret = (char *)xmalloc (2 + strlen (name));
   ret[0] = '-';
   strcpy (ret + 1, name);
   return ret;
@@ -94,7 +103,7 @@ exec_builtin (list)
   char *argv0, *command, **args, **env, *newname, *com2;
 
   cleanenv = login = 0;
-  argv0 = (char *)NULL;
+  exec_argv0 = argv0 = (char *)NULL;
 
   reset_internal_getopt ();
   while ((opt = internal_getopt (list, "cla:")) != -1)
@@ -127,20 +136,32 @@ exec_builtin (list)
 #if defined (RESTRICTED_SHELL)
   if (restricted)
     {
-      builtin_error ("restricted");
+      sh_restricted ((char *)NULL);
       return (EXECUTION_FAILURE);
     }
 #endif /* RESTRICTED_SHELL */
 
-  args = word_list_to_argv (list, 1, 0, (int *)NULL);
+  args = strvec_from_word_list (list, 1, 0, (int *)NULL);
 
   /* A command with a slash anywhere in its name is not looked up in $PATH. */
   command = absolute_program (args[0]) ? args[0] : search_for_command (args[0]);
 
   if (command == 0)
     {
-      builtin_error ("%s: not found", args[0]);
-      exit_value = EX_NOTFOUND;        /* As per Posix.2, 3.14.6 */
+      if (file_isdir (args[0]))
+       {
+#if defined (EISDIR)
+         builtin_error (_("%s: cannot execute: %s"), args[0], strerror (EISDIR));
+#else
+         builtin_error (_("%s: cannot execute: %s"), args[0], strerror (errno));
+#endif
+         exit_value = EX_NOEXEC;
+       }
+      else
+       {
+         sh_notfound (args[0]);
+         exit_value = EX_NOTFOUND;     /* As per Posix.2, 3.14.6 */
+       }
       goto failed_exec;
     }
 
@@ -156,6 +177,7 @@ exec_builtin (list)
     {
       free (args[0]);
       args[0] = login ? mkdashname (argv0) : savestring (argv0);
+      exec_argv0 = savestring (args[0]);
     }
   else if (login)
     {
@@ -190,33 +212,40 @@ exec_builtin (list)
     end_job_control ();
 #endif /* JOB_CONTROL */
 
-  shell_execve (command, args, env);
+  exit_value = shell_execve (command, args, env);
+
+  /* We have to set this to NULL because shell_execve has called realloc()
+     to stuff more items at the front of the array, which may have caused
+     the memory to be freed by realloc().  We don't want to free it twice. */
+  args = (char **)NULL;
   if (cleanenv == 0)
     adjust_shell_level (1);
 
-  if (executable_file (command) == 0)
+  if (exit_value == EX_NOTFOUND)       /* no duplicate error message */
+    goto failed_exec;
+  else if (executable_file (command) == 0)
     {
-      builtin_error ("%s: cannot execute: %s", command, strerror (errno));
+      builtin_error (_("%s: cannot execute: %s"), command, strerror (errno));
       exit_value = EX_NOEXEC;  /* As per Posix.2, 3.14.6 */
     }
   else
     file_error (command);
 
 failed_exec:
-  if (command)
-    free (command);
+  FREE (command);
 
   if (subshell_environment || (interactive == 0 && no_exit_on_failed_exec == 0))
     exit_shell (exit_value);
 
   if (args)
-    free_array (args);
+    strvec_dispose (args);
 
   initialize_traps ();
-  reinitialize_signals ();
+  initialize_signals (1);
 
 #if defined (JOB_CONTROL)
-  restart_job_control ();
+  if (interactive_shell || job_control)
+    restart_job_control ();
 #endif /* JOB_CONTROL */
 
   return (exit_value);