1 This file is exit.def, from which is created exit.c.
2 It implements the builtins "exit", and "logout" 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 exit_builtin
27 Exit the shell with a status of N. If N is omitted, the exit status
28 is that of the last command executed.
33 #include "../bashtypes.h"
36 #if defined (HAVE_UNISTD_H)
40 #include "../bashintl.h"
46 #include "builtext.h" /* for jobs_builtin */
48 extern int last_command_exit_value;
49 extern int running_trap, trap_saved_exit_value;
50 extern int subshell_environment;
51 extern sh_builtin_func_t *this_shell_builtin;
52 extern sh_builtin_func_t *last_shell_builtin;
54 static int exit_or_logout __P((WORD_LIST *));
55 static int sourced_logout;
63 fprintf (stderr, login_shell ? "logout\n" : "exit\n");
67 return (exit_or_logout (list));
71 $FUNCTION logout_builtin
73 Logout of a login shell.
81 if (login_shell == 0 /* && interactive */)
83 builtin_error (_("not login shell: use `exit'"));
84 return (EXECUTION_FAILURE);
87 return (exit_or_logout (list));
96 #if defined (JOB_CONTROL)
97 int exit_immediate_okay;
99 exit_immediate_okay = (interactive == 0 ||
100 last_shell_builtin == exit_builtin ||
101 last_shell_builtin == logout_builtin ||
102 last_shell_builtin == jobs_builtin);
104 /* Check for stopped jobs if the user wants to. */
105 if (!exit_immediate_okay)
108 for (i = 0; i < job_slots; i++)
109 if (jobs[i] && STOPPED (i))
111 fprintf (stderr, _("There are stopped jobs.\n"));
113 /* This is NOT superfluous because EOF can get here without
114 going through the command parser. Set both last and this
115 so that either `exit', `logout', or ^D will work to exit
116 immediately if nothing intervenes. */
117 this_shell_builtin = last_shell_builtin = exit_builtin;
118 return (EXECUTION_FAILURE);
121 #endif /* JOB_CONTROL */
123 /* Get return value if present. This means that you can type
124 `logout 5' to a shell, and it returns 5. */
126 /* If we're running the exit trap (running_trap == 1, since running_trap
127 gets set to SIG+1), and we don't have a argument given to `exit'
128 (list == 0), use the exit status we saved before running the trap
129 commands (trap_saved_exit_value). */
130 exit_value = (running_trap == 1 && list == 0) ? trap_saved_exit_value : get_exitstat (list);
134 last_command_exit_value = exit_value;
136 /* Exit the program. */
137 jump_to_top_level (EXITPROG);
144 /* Run our `~/.bash_logout' file if it exists, and this is a login shell. */
145 if (login_shell && sourced_logout++ == 0 && subshell_environment == 0)
147 maybe_execute_file ("~/.bash_logout", 1);
148 #ifdef SYS_BASH_LOGOUT
149 maybe_execute_file (SYS_BASH_LOGOUT, 1);