MiniDevil As beautiful as a shell
builtin_exit.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* builtin_exit.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/11 17:53:47 by baelgadi #+# #+# */
9 /* Updated: 2026/03/11 03:02:15 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "builtins.h"
14 #include "libft.h"
15 
29 int builtin_exit(char **args, t_shell *shell)
30 {
31  long long exit_code;
32  int overflow;
33 
34  if (shell->interactive)
35  ft_putstr_fd("exit\n", STDERR_FILENO);
36  if (!args[1])
37  {
38  shell->running = 0;
39  return (shell->exit_status);
40  }
41  exit_code = ft_atoll(args[1], &overflow);
42  if (!ft_str_is_numeric(args[1]) || overflow)
43  {
44  ft_putstr_fd("minishell: exit: ", STDERR_FILENO);
45  ft_putstr_fd(args[1], STDERR_FILENO);
46  ft_putstr_fd(": numeric argument required\n", STDERR_FILENO);
47  shell->running = 0;
48  return (2);
49  }
50  if (ft_arrlen(args) > 2)
51  return (ft_putstr_fd("minishell: exit: too many arguments\n",
52  STDERR_FILENO), 1);
53  shell->running = 0;
54  return ((unsigned char)exit_code);
55 }
int builtin_exit(char **args, t_shell *shell)
Implement the exit command.
Definition: builtin_exit.c:29
Shell builtin commands prototypes.
Shell state.
Definition: structs.h:164
int interactive
1 if STDIN is a tty
Definition: structs.h:168
int running
1 while main loop is active
Definition: structs.h:167
int exit_status
Last command's exit status.
Definition: structs.h:166