]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/exit.def
37f1d7bf930290006cfcb1fa5eb09f66aebf52e8
[thirdparty/bash.git] / builtins / exit.def
1 This file is exit.def, from which is created exit.c.
2 It implements the builtins "exit", and "logout" in Bash.
3
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
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
11 version.
12
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
16 for more details.
17
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.
21
22 $PRODUCES exit.c
23
24 $BUILTIN exit
25 $FUNCTION exit_builtin
26 $SHORT_DOC exit [n]
27 Exit the shell with a status of N. If N is omitted, the exit status
28 is that of the last command executed.
29 $END
30
31 #include <config.h>
32
33 #include "../bashtypes.h"
34 #include <stdio.h>
35
36 #if defined (HAVE_UNISTD_H)
37 # include <unistd.h>
38 #endif
39
40 #include "../shell.h"
41 #include "../jobs.h"
42
43 #include "common.h"
44 #include "builtext.h" /* for jobs_builtin */
45
46 extern int interactive, login_shell;
47 extern int last_command_exit_value;
48 extern Function *this_shell_builtin;
49 extern Function *last_shell_builtin;
50
51 static int exit_or_logout ();
52 static int sourced_logout;
53
54 int
55 exit_builtin (list)
56 WORD_LIST *list;
57 {
58 if (interactive)
59 {
60 fprintf (stderr, login_shell ? "logout\n" : "exit\n");
61 fflush (stderr);
62 }
63
64 return (exit_or_logout (list));
65 }
66
67 $BUILTIN logout
68 $FUNCTION logout_builtin
69 $SHORT_DOC logout
70 Logout of a login shell.
71 $END
72
73 /* How to logout. */
74 int
75 logout_builtin (list)
76 WORD_LIST *list;
77 {
78 if (login_shell == 0 /* && interactive */)
79 {
80 builtin_error ("not login shell: use `exit'");
81 return (EXECUTION_FAILURE);
82 }
83 else
84 return (exit_or_logout (list));
85 }
86
87 static int
88 exit_or_logout (list)
89 WORD_LIST *list;
90 {
91 int exit_value;
92
93 #if defined (JOB_CONTROL)
94 int exit_immediate_okay;
95
96 exit_immediate_okay = (interactive == 0 ||
97 last_shell_builtin == exit_builtin ||
98 last_shell_builtin == logout_builtin ||
99 last_shell_builtin == jobs_builtin);
100
101 /* Check for stopped jobs if the user wants to. */
102 if (!exit_immediate_okay)
103 {
104 register int i;
105 for (i = 0; i < job_slots; i++)
106 if (jobs[i] && STOPPED (i))
107 {
108 fprintf (stderr, "There are stopped jobs.\n");
109
110 /* This is NOT superfluous because EOF can get here without
111 going through the command parser. Set both last and this
112 so that either `exit', `logout', or ^D will work to exit
113 immediately if nothing intervenes. */
114 this_shell_builtin = last_shell_builtin = exit_builtin;
115 return (EXECUTION_FAILURE);
116 }
117 }
118 #endif /* JOB_CONTROL */
119
120 /* Get return value if present. This means that you can type
121 `logout 5' to a shell, and it returns 5. */
122 exit_value = list ? get_numeric_arg (list, 1) : last_command_exit_value;
123
124 /* Run our `~/.bash_logout' file if it exists, and this is a login shell. */
125 if (login_shell && sourced_logout++ == 0)
126 {
127 maybe_execute_file ("~/.bash_logout", 1);
128 #ifdef SYS_BASH_LOGOUT
129 maybe_execute_file (SYS_BASH_LOGOUT, 1);
130 #endif
131 }
132
133 last_command_exit_value = exit_value;
134
135 /* Exit the program. */
136 jump_to_top_level (EXITPROG);
137 /*NOTREACHED*/
138 }