Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / builtins / exec.def
index acfdae1..9cf76d5 100644 (file)
@@ -1,37 +1,44 @@
 This file is exec.def, from which is created exec.c.
 It implements the builtin "exec" in Bash.
 
-Copyright (C) 1987-2003 Free Software Foundation, Inc.
+Copyright (C) 1987-2012 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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 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>
@@ -69,6 +76,7 @@ extern int errno;
 
 extern int subshell_environment;
 extern REDIRECT *redirection_undo_list;
+extern char *exec_argv0;
 
 int no_exit_on_failed_exec;
 
@@ -95,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)
@@ -136,12 +144,24 @@ exec_builtin (list)
   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]);
+  command = absolute_program (args[0]) ? args[0] : search_for_command (args[0], 1);
 
   if (command == 0)
     {
-      sh_notfound (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;
     }
 
@@ -157,6 +177,7 @@ exec_builtin (list)
     {
       free (args[0]);
       args[0] = login ? mkdashname (argv0) : savestring (argv0);
+      exec_argv0 = savestring (args[0]);
     }
   else if (login)
     {
@@ -191,7 +212,7 @@ 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
@@ -200,7 +221,9 @@ exec_builtin (list)
   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));
       exit_value = EX_NOEXEC;  /* As per Posix.2, 3.14.6 */
@@ -221,7 +244,8 @@ failed_exec:
   initialize_signals (1);
 
 #if defined (JOB_CONTROL)
-  restart_job_control ();
+  if (interactive_shell || job_control)
+    restart_job_control ();
 #endif /* JOB_CONTROL */
 
   return (exit_value);