This file is exec.def, from which is created exec.c.
It implements the builtin "exec" in Bash.
-Copyright (C) 1987-2002 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 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>
#endif
#include "../bashansi.h"
+#include "../bashintl.h"
#include "../shell.h"
#include "../execute_cmd.h"
extern int subshell_environment;
extern REDIRECT *redirection_undo_list;
+extern char *exec_argv0;
int no_exit_on_failed_exec;
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)
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;
}
{
free (args[0]);
args[0] = login ? mkdashname (argv0) : savestring (argv0);
+ exec_argv0 = savestring (args[0]);
}
else if (login)
{
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
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);
initialize_signals (1);
#if defined (JOB_CONTROL)
- restart_job_control ();
+ if (interactive_shell || job_control)
+ restart_job_control ();
#endif /* JOB_CONTROL */
return (exit_value);