1 This file is exec.def, from which is created exec.c.
2 It implements the builtin "exec" in Bash.
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 1, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 $FUNCTION exec_builtin
26 $SHORT_DOC exec [-cl] [-a name] file [redirection ...]
27 Exec FILE, replacing this shell with the specified program.
28 If FILE is not specified, the redirections take effect in this
29 shell. If the first argument is `-l', then place a dash in the
30 zeroth arg passed to FILE, as login does. If the `-c' option
31 is supplied, FILE is executed with a null environment. The `-a'
32 option means to make set argv[0] of the executed process to NAME.
33 If the file cannot be executed and the shell is not interactive,
34 then the shell exits, unless the shell option `execfail' is set.
39 #include "../bashtypes.h"
40 #include "../posixstat.h"
44 #if defined (HAVE_UNISTD_H)
48 #include "../bashansi.h"
51 #include "../execute_cmd.h"
52 #include "../findcmd.h"
53 #if defined (JOB_CONTROL)
59 # include "../bashhist.h"
62 #include "bashgetopt.h"
64 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
69 extern int interactive, interactive_shell, subshell_environment;
70 extern REDIRECT *redirection_undo_list;
72 int no_exit_on_failed_exec;
74 /* If the user wants this to look like a login shell, then
75 prepend a `-' onto NAME and return the new name. */
82 ret = xmalloc (2 + strlen (name));
84 strcpy (ret + 1, name);
92 int exit_value = EXECUTION_FAILURE;
93 int cleanenv, login, opt;
94 char *argv0, *command, **args, **env, *newname, *com2;
99 reset_internal_getopt ();
100 while ((opt = internal_getopt (list, "cla:")) != -1)
120 /* First, let the redirections remain. */
121 dispose_redirects (redirection_undo_list);
122 redirection_undo_list = (REDIRECT *)NULL;
125 return (EXECUTION_SUCCESS);
127 #if defined (RESTRICTED_SHELL)
130 builtin_error ("restricted");
131 return (EXECUTION_FAILURE);
133 #endif /* RESTRICTED_SHELL */
135 args = word_list_to_argv (list, 1, 0, (int *)NULL);
137 /* A command with a slash anywhere in its name is not looked up in $PATH. */
138 command = absolute_program (args[0]) ? args[0] : search_for_command (args[0]);
142 builtin_error ("%s: not found", args[0]);
143 exit_value = EX_NOTFOUND; /* As per Posix.2, 3.14.6 */
147 com2 = full_pathname (command);
150 if (command != args[0])
158 args[0] = login ? mkdashname (argv0) : savestring (argv0);
162 newname = mkdashname (args[0]);
167 /* Decrement SHLVL by 1 so a new shell started here has the same value,
168 preserving the appearance. After we do that, we need to change the
169 exported environment to include the new value. */
171 adjust_shell_level (-1);
177 maybe_make_export_env ();
181 #if defined (HISTORY)
182 if (interactive_shell && subshell_environment == 0)
183 maybe_save_shell_history ();
186 restore_original_signals ();
188 #if defined (JOB_CONTROL)
189 if (subshell_environment == 0)
191 #endif /* JOB_CONTROL */
193 shell_execve (command, args, env);
195 adjust_shell_level (1);
197 if (executable_file (command) == 0)
199 builtin_error ("%s: cannot execute: %s", command, strerror (errno));
200 exit_value = EX_NOEXEC; /* As per Posix.2, 3.14.6 */
203 file_error (command);
209 if (subshell_environment || (interactive == 0 && no_exit_on_failed_exec == 0))
210 exit_shell (exit_value);
216 reinitialize_signals ();
218 #if defined (JOB_CONTROL)
219 restart_job_control ();
220 #endif /* JOB_CONTROL */