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 [ [-] 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 `-', then place a dash in the
30 zeroth arg passed to FILE. If the file cannot be exec'ed and
31 the shell is not interactive, then the shell exits, unless the
32 shell variable "no_exit_on_failed_exec" exists.
36 #include <sys/types.h>
37 #include "../posixstat.h"
41 #include "../execute_cmd.h"
45 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
49 extern int interactive, subshell_environment;
50 extern REDIRECT *redirection_undo_list;
56 int exit_value = EXECUTION_FAILURE;
58 maybe_make_export_env ();
60 /* First, let the redirections remain. */
61 dispose_redirects (redirection_undo_list);
62 redirection_undo_list = (REDIRECT *)NULL;
65 return (EXECUTION_SUCCESS);
68 /* Otherwise, execve the new command with args. */
69 char *command, **args;
72 if (list->word->word[0] == '-' && !list->word->word[1])
74 /* The user would like to exec this command as if it was a
75 login command. Do so. */
81 return (EXECUTION_SUCCESS);
83 #if defined (RESTRICTED_SHELL)
86 builtin_error ("restricted");
87 return (EXECUTION_FAILURE);
89 #endif /* RESTRICTED_SHELL */
91 args = make_word_array (list);
93 /* A command with a slash anywhere in its name is not looked up in
95 if (absolute_program (args[0]))
98 command = find_user_command (args[0]);
101 builtin_error ("%s: not found", args[0]);
102 exit_value = EX_NOTFOUND; /* As per Posix.2, 3.14.6 */
106 command = full_pathname (command);
107 /* If the user wants this to look like a login shell, then
108 prepend a `-' onto the first argument (argv[0]). */
111 char *new_name = xmalloc (2 + strlen (args[0]));
113 strcpy (new_name + 1, args[0]);
118 /* Decrement SHLVL by 1 so a new shell started here has the same value,
119 preserving the appearance. After we do that, we need to change the
120 exported environment to include the new value. */
121 adjust_shell_level (-1);
122 maybe_make_export_env ();
124 #if defined (HISTORY)
125 maybe_save_shell_history ();
127 restore_original_signals ();
129 #if defined (JOB_CONTROL)
130 if (subshell_environment == 0)
132 #endif /* JOB_CONTROL */
134 shell_execve (command, args, export_env);
136 adjust_shell_level (1);
138 if (!executable_file (command))
140 builtin_error ("%s: cannot execute: %s", command, strerror (errno));
141 exit_value = EX_NOEXEC; /* As per Posix.2, 3.14.6 */
144 file_error (command);
150 if (subshell_environment ||
151 (!interactive && !find_variable ("no_exit_on_failed_exec")))
155 reinitialize_signals ();
157 #if defined (JOB_CONTROL)
158 restart_job_control ();
159 #endif /* JOB_CONTROL */