source: vendor/bash/3.1/builtins/exec.def@ 3230

Last change on this file since 3230 was 3228, checked in by bird, 19 years ago

bash 3.1

File size: 5.5 KB
RevLine 
[3228]1This file is exec.def, from which is created exec.c.
2It implements the builtin "exec" in Bash.
3
4Copyright (C) 1987-2003 Free Software Foundation, Inc.
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
8Bash is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
12
13Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License along
19with Bash; see the file COPYING. If not, write to the Free Software
20Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21
22$PRODUCES exec.c
23
24$BUILTIN exec
25$FUNCTION exec_builtin
26$SHORT_DOC exec [-cl] [-a name] file [redirection ...]
27Exec FILE, replacing this shell with the specified program.
28If FILE is not specified, the redirections take effect in this
29shell. If the first argument is `-l', then place a dash in the
30zeroth arg passed to FILE, as login does. If the `-c' option
31is supplied, FILE is executed with a null environment. The `-a'
32option means to make set argv[0] of the executed process to NAME.
33If the file cannot be executed and the shell is not interactive,
34then the shell exits, unless the shell option `execfail' is set.
35$END
36
37#include <config.h>
38
39#include "../bashtypes.h"
40#include "posixstat.h"
41#include <signal.h>
42#include <errno.h>
43
44#if defined (HAVE_UNISTD_H)
45# include <unistd.h>
46#endif
47
48#include "../bashansi.h"
49#include "../bashintl.h"
50
51#include "../shell.h"
52#include "../execute_cmd.h"
53#include "../findcmd.h"
54#if defined (JOB_CONTROL)
55# include "../jobs.h"
56#endif
57#include "../flags.h"
58#include "../trap.h"
59#if defined (HISTORY)
60# include "../bashhist.h"
61#endif
62#include "common.h"
63#include "bashgetopt.h"
64
65/* Not all systems declare ERRNO in errno.h... and some systems #define it! */
66#if !defined (errno)
67extern int errno;
68#endif /* !errno */
69
70extern int subshell_environment;
71extern REDIRECT *redirection_undo_list;
72
73int no_exit_on_failed_exec;
74
75/* If the user wants this to look like a login shell, then
76 prepend a `-' onto NAME and return the new name. */
77static char *
78mkdashname (name)
79 char *name;
80{
81 char *ret;
82
83 ret = (char *)xmalloc (2 + strlen (name));
84 ret[0] = '-';
85 strcpy (ret + 1, name);
86 return ret;
87}
88
89int
90exec_builtin (list)
91 WORD_LIST *list;
92{
93 int exit_value = EXECUTION_FAILURE;
94 int cleanenv, login, opt;
95 char *argv0, *command, **args, **env, *newname, *com2;
96
97 cleanenv = login = 0;
98 argv0 = (char *)NULL;
99
100 reset_internal_getopt ();
101 while ((opt = internal_getopt (list, "cla:")) != -1)
102 {
103 switch (opt)
104 {
105 case 'c':
106 cleanenv = 1;
107 break;
108 case 'l':
109 login = 1;
110 break;
111 case 'a':
112 argv0 = list_optarg;
113 break;
114 default:
115 builtin_usage ();
116 return (EX_USAGE);
117 }
118 }
119 list = loptend;
120
121 /* First, let the redirections remain. */
122 dispose_redirects (redirection_undo_list);
123 redirection_undo_list = (REDIRECT *)NULL;
124
125 if (list == 0)