diff --git a/Makefile b/Makefile index 7888311..a5cd193 100644 --- a/Makefile +++ b/Makefile @@ -135,6 +135,7 @@ SRCS += env.c SRCS += export.c SRCS += display_sorted_exportables_variables.c SRCS += pwd.c +SRCS += exit.c # srcs/builtins/echo diff --git a/includes/minishell.h b/includes/minishell.h index 8405279..b0272b1 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: linux +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/09 16:11:56 by hucherea #+# #+# */ -/* Updated: 2025/02/15 04:50:18 by linux ### ########.fr */ +/* Updated: 2025/02/15 17:24:42 by linux ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,7 +78,8 @@ typedef enum e_builtin_type EXPORT, PWD, CD, - ECHO + ECHO, + EXIT } t_builtin_type; typedef enum e_new_line_status @@ -146,6 +147,7 @@ char *add_last_arg(char *line, char *arg); int cd(t_command *command); char *get_target_from_args(char **args); int update_env_variables(char *old_pwd); +int exit_builtin(t_command *command); // EXIT STATUS diff --git a/srcs/builtins/exit.c b/srcs/builtins/exit.c new file mode 100644 index 0000000..903df83 --- /dev/null +++ b/srcs/builtins/exit.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hucherea +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/15 16:45:34 by linux #+# #+# */ +/* Updated: 2025/02/16 17:15:40 by hucherea ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +bool ft_isnumber(char *args) +{ + int i; + + i = 0; + if (args[i] == '-' || args[i] == '+') + i++; + while (args[i] != '\0') + { + if (!ft_isdigit(args[i])) + return (false); + i++; + } + return (true); +} + +int error_exit(char *arg) +{ + ft_putstr_fd("exit\n", 2); + ft_putstr_fd("minishell: exit: ", 2); + ft_putstr_fd(arg, 2); + ft_putstr_fd(": numeric argument required\n", 2); + return (EXIT_FAILURE); +} + +int exit_builtin(t_command *command) +{ + int exit_code; + char **args; + + args = list_to_strs_array(command->command_args, args_list_to_args_array); + exit_code = 0; + if (ft_lstsize(command->command_args) > 2) + { + ft_putstr_fd("exit\n", 2); + ft_putstr_fd("minishell: exit: too many arguments\n", 2); + return (EXIT_FAILURE); + } + if (ft_lstsize(command->command_args) > 1) + { + if (ft_isnumber(args[1])) + { + exit_code = ft_atoi(args[1]); + } + else + { + return ((unsigned char)error_exit(args[1])); + } + } + ft_putstr_fd("exit\n", 2); + exit((unsigned char)exit_code); + return ((unsigned char)exit_code); +} diff --git a/srcs/command_interpretation/launch_builtin.c b/srcs/command_interpretation/launch_builtin.c index 27d3914..b7df3b1 100644 --- a/srcs/command_interpretation/launch_builtin.c +++ b/srcs/command_interpretation/launch_builtin.c @@ -6,7 +6,7 @@ /* By: linux +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/01 18:21:50 by tchobert #+# #+# */ -/* Updated: 2025/02/15 05:08:32 by linux ### ########.fr */ +/* Updated: 2025/02/15 17:24:07 by linux ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ static t_builtin_type get_builtin_type(const char *command_name) "pwd", "cd", "echo", + "exit", NULL, }; t_builtin_type type; @@ -65,7 +66,8 @@ int execute_builtin(t_minishell_context *minishell_context, export, pwd, cd, - echo + echo, + exit_builtin, }; int builtin_return;