summaryrefslogtreecommitdiff
path: root/builtins/exec.def
diff options
context:
space:
mode:
Diffstat (limited to 'builtins/exec.def')
-rw-r--r--builtins/exec.def213
1 files changed, 131 insertions, 82 deletions
diff --git a/builtins/exec.def b/builtins/exec.def
index f950afc6..c14af75f 100644
--- a/builtins/exec.def
+++ b/builtins/exec.def
@@ -23,141 +23,190 @@ $PRODUCES exec.c
$BUILTIN exec
$FUNCTION exec_builtin
-$SHORT_DOC exec [ [-] file [redirection ...]]
+$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 `-', then place a dash in the
-zeroth arg passed to FILE. If the file cannot be exec'ed and
-the shell is not interactive, then the shell exits, unless the
-shell variable "no_exit_on_failed_exec" exists.
+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.
$END
-#include "../shell.h"
+#include <config.h>
+
#include <sys/types.h>
#include "../posixstat.h"
#include <signal.h>
#include <errno.h>
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
+
+#include "../bashansi.h"
+
+#include "../shell.h"
#include "../execute_cmd.h"
-#include "common.h"
+#if defined (JOB_CONTROL)
+# include "../jobs.h"
+#endif
#include "../flags.h"
+#include "../trap.h"
+#if defined (HISTORY)
+# include "../bashhist.h"
+#endif
+#include "common.h"
+#include "bashgetopt.h"
/* Not all systems declare ERRNO in errno.h... and some systems #define it! */
#if !defined (errno)
extern int errno;
#endif /* !errno */
+
extern int interactive, subshell_environment;
extern REDIRECT *redirection_undo_list;
+int no_exit_on_failed_exec;
+
+/* If the user wants this to look like a login shell, then
+ prepend a `-' onto NAME and return the new name. */
+static char *
+mkdashname (name)
+ char *name;
+{
+ char *ret;
+
+ ret = xmalloc (2 + strlen (name));
+ ret[0] = '-';
+ strcpy (ret + 1, name);
+ return ret;
+}
+
int
exec_builtin (list)
WORD_LIST *list;
{
int exit_value = EXECUTION_FAILURE;
+ int cleanenv, login, opt;
+ char *argv0, *command, **args, **env, *newname;
- maybe_make_export_env ();
+ cleanenv = login = 0;
+ argv0 = (char *)NULL;
+
+ reset_internal_getopt ();
+ while ((opt = internal_getopt (list, "cla:")) != -1)
+ {
+ switch (opt)
+ {
+ case 'c':
+ cleanenv = 1;
+ break;
+ case 'l':
+ login = 1;
+ break;
+ case 'a':
+ argv0 = list_optarg;
+ break;
+ default:
+ builtin_usage ();
+ return (EX_USAGE);
+ }
+ }
+ list = loptend;
/* First, let the redirections remain. */
dispose_redirects (redirection_undo_list);
redirection_undo_list = (REDIRECT *)NULL;
- if (!list)
+ if (list == 0)
return (EXECUTION_SUCCESS);
- else
+
+#if defined (RESTRICTED_SHELL)
+ if (restricted)
{
- /* Otherwise, execve the new command with args. */
- char *command, **args;
- int dash_name = 0;
+ builtin_error ("restricted");
+ return (EXECUTION_FAILURE);
+ }
+#endif /* RESTRICTED_SHELL */
- if (list->word->word[0] == '-' && !list->word->word[1])
- {
- /* The user would like to exec this command as if it was a
- login command. Do so. */
- list = list->next;
- dash_name++;
- }
+ args = word_list_to_argv (list, 1, 0, (int *)NULL);
- if (!list)
- return (EXECUTION_SUCCESS);
+ /* 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]);
-#if defined (RESTRICTED_SHELL)
- if (restricted)
- {
- builtin_error ("restricted");
- return (EXECUTION_FAILURE);
- }
-#endif /* RESTRICTED_SHELL */
+ if (command == 0)
+ {
+ builtin_error ("%s: not found", args[0]);
+ exit_value = EX_NOTFOUND; /* As per Posix.2, 3.14.6 */
+ goto failed_exec;
+ }
- args = make_word_array (list);
+ command = full_pathname (command);
- /* A command with a slash anywhere in its name is not looked up in
- the search path. */
- if (absolute_program (args[0]))
- command = args[0];
- else
- command = find_user_command (args[0]);
- if (!command)
- {
- builtin_error ("%s: not found", args[0]);
- exit_value = EX_NOTFOUND; /* As per Posix.2, 3.14.6 */
- goto failed_exec;
- }
+ if (argv0)
+ {
+ free (args[0]);
+ args[0] = login ? mkdashname (argv0) : savestring (argv0);
+ }
+ else if (login)
+ {
+ newname = mkdashname (args[0]);
+ free (args[0]);
+ args[0] = newname;
+ }
- command = full_pathname (command);
- /* If the user wants this to look like a login shell, then
- prepend a `-' onto the first argument (argv[0]). */
- if (dash_name)
- {
- char *new_name = xmalloc (2 + strlen (args[0]));
- new_name[0] = '-';
- strcpy (new_name + 1, args[0]);
- free (args[0]);
- args[0] = new_name;
- }
+ /* Decrement SHLVL by 1 so a new shell started here has the same value,
+ preserving the appearance. After we do that, we need to change the
+ exported environment to include the new value. */
+ if (cleanenv == 0)
+ adjust_shell_level (-1);
- /* Decrement SHLVL by 1 so a new shell started here has the same value,
- preserving the appearance. After we do that, we need to change the
- exported environment to include the new value. */
- adjust_shell_level (-1);
+ if (cleanenv)
+ env = (char **)NULL;
+ else
+ {
maybe_make_export_env ();
+ env = export_env;
+ }
#if defined (HISTORY)
- maybe_save_shell_history ();
+ maybe_save_shell_history ();
#endif /* HISTORY */
- restore_original_signals ();
+
+ restore_original_signals ();
#if defined (JOB_CONTROL)
- if (subshell_environment == 0)
- end_job_control ();
+ if (subshell_environment == 0)
+ end_job_control ();
#endif /* JOB_CONTROL */
- shell_execve (command, args, export_env);
-
- adjust_shell_level (1);
+ shell_execve (command, args, env);
+ if (cleanenv == 0)
+ adjust_shell_level (1);
- if (!executable_file (command))
- {
- builtin_error ("%s: cannot execute: %s", command, strerror (errno));
- exit_value = EX_NOEXEC; /* As per Posix.2, 3.14.6 */
- }
- else
- file_error (command);
+ 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 */
+ }
+ else
+ file_error (command);
- failed_exec:
- if (command)
- free (command);
+failed_exec:
+ if (command)
+ free (command);
- if (subshell_environment ||
- (!interactive && !find_variable ("no_exit_on_failed_exec")))
- exit (exit_value);
+ if (subshell_environment || (interactive == 0 && no_exit_on_failed_exec == 0))
+ exit_shell (exit_value);
- initialize_traps ();
- reinitialize_signals ();
+ initialize_traps ();
+ reinitialize_signals ();
#if defined (JOB_CONTROL)
- restart_job_control ();
+ restart_job_control ();
#endif /* JOB_CONTROL */
- return (exit_value);
- }
+ return (exit_value);
}