1 This file is exec.def, from which is created exec.c.
2 It implements the builtin "exec" in Bash.
4 Copyright (C) 1987-2003 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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 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"
49 #include "../bashintl.h"
52 #include "../execute_cmd.h"
53 #include "../findcmd.h"
54 #if defined (JOB_CONTROL)
60 # include "../bashhist.h"
63 #include "bashgetopt.h"
65 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
70 extern int subshell_environment;
71 extern REDIRECT *redirection_undo_list;
73 int no_exit_on_failed_exec;
75 /* If the user wants this to look like a login shell, then
76 prepend a `-' onto NAME and return the new name. */
83 ret = (char *)xmalloc (2 + strlen (name));
85 strcpy (ret + 1, name);
93 int exit_value = EXECUTION_FAILURE;
94 int cleanenv, login, opt;
95 char *argv0, *command, **args, **env, *newname, *com2;
100 reset_internal_getopt ();
101 while ((opt = internal_getopt (list, "cla:")) != -1)
121 /* First, let the redirections remain. */
122 dispose_redirects (redirection_undo_list);
123 redirection_undo_list = (REDIRECT *)NULL;
126 return (EXECUTION_SUCCESS);
128 #if defined (RESTRICTED_SHELL)
131 sh_restricted ((char *)NULL);
132 return (EXECUTION_FAILURE);
134 #endif /* RESTRICTED_SHELL */
136 args = strvec_from_word_list (list, 1, 0, (int *)NULL);
138 /* A command with a slash anywhere in its name is not looked up in $PATH. */
139 command = absolute_program (args[0]) ? args[0] : search_for_command (args[0]);
143 sh_notfound (args[0]);
144 exit_value = EX_NOTFOUND; /* As per Posix.2, 3.14.6 */
148 com2 = full_pathname (command);
151 if (command != args[0])
159 args[0] = login ? mkdashname (argv0) : savestring (argv0);
163 newname = mkdashname (args[0]);
168 /* Decrement SHLVL by 1 so a new shell started here has the same value,
169 preserving the appearance. After we do that, we need to change the
170 exported environment to include the new value. */
172 adjust_shell_level (-1);
178 maybe_make_export_env ();
182 #if defined (HISTORY)
183 if (interactive_shell && subshell_environment == 0)
184 maybe_save_shell_history ();
187 restore_original_signals ();
189 #if defined (JOB_CONTROL)
190 if (subshell_environment == 0)
192 #endif /* JOB_CONTROL */
194 shell_execve (command, args, env);
196 /* We have to set this to NULL because shell_execve has called realloc()
197 to stuff more items at the front of the array, which may have caused
198 the memory to be freed by realloc(). We don't want to free it twice. */
199 args = (char **)NULL;
201 adjust_shell_level (1);
203 if (executable_file (command) == 0)
205 builtin_error (_("%s: cannot execute: %s"), command, strerror (errno));
206 exit_value = EX_NOEXEC; /* As per Posix.2, 3.14.6 */
209 file_error (command);
214 if (subshell_environment || (interactive == 0 && no_exit_on_failed_exec == 0))
215 exit_shell (exit_value);
218 strvec_dispose (args);
221 initialize_signals (1);
223 #if defined (JOB_CONTROL)
224 restart_job_control ();
225 #endif /* JOB_CONTROL */