From a40e6c2de2a396d67b0dbf903a7eadc5fc6ca687 Mon Sep 17 00:00:00 2001 From: narnaud Date: Sat, 7 May 2022 00:25:21 +0200 Subject: [PATCH] cleaner exit --- built-in.c | 10 ++++++---- caller.c | 24 +++++++++++------------- minishell.c | 5 ++--- minishell.h | 4 ++-- utils.c | 12 +++++++++++- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/built-in.c b/built-in.c index e22b423..5296e0a 100755 --- a/built-in.c +++ b/built-in.c @@ -69,13 +69,15 @@ int ft_cd(t_command *cmd) return (1); } else - { chdir(getenv("HOME")); - } return (0); } -int close_minishell(int exit_code) +int ft_exit(t_datas *datas, t_command *cmd) { - exit(exit_code); + if (cmd->argc > 1) + halt(datas, EXIT_SUCCESS); + else + halt(datas, ft_atoi(cmd->argv[1])); + return (1); } diff --git a/caller.c b/caller.c index cb80d78..48d1b3a 100755 --- a/caller.c +++ b/caller.c @@ -27,7 +27,7 @@ int builtin_call(t_datas *datas, t_command *cmd) else if (ft_strncmp(cmd->argv[0], "env", 4) == 0) return (ft_env(datas, cmd)); else if (ft_strncmp(cmd->argv[0], "exit", 5) == 0) - return (close_minishell(0)); + return (ft_exit(cmd)); else return (-1); } @@ -54,19 +54,17 @@ void exe(t_datas *datas, t_command *cmd) if (ft_strchr(cmd->argv[0], '/')) execve(cmd->argv[0], cmd->argv, datas->envp); - else + path_dirs = ft_split(getenv("PATH"), ':'); + while (path_dirs) { - path_dirs = ft_split(getenv("PATH"), ':'); - while (path_dirs) - { - path_length = ft_strlen(*path_dirs) + ft_strlen(cmd->argv[0]) + 2; - path = ft_calloc(path_length, sizeof(char)); - ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); - ft_memcpy(path + ft_strlen(*path_dirs),"/", 1); - ft_memcpy(path + ft_strlen(*path_dirs) + 1, cmd->argv[0], ft_strlen(cmd->argv[0])); - execve(path, cmd->argv, datas->envp); - path_dirs++; - } + path_length = ft_strlen(*path_dirs) + ft_strlen(cmd->argv[0]) + 2; + path = ft_calloc(path_length, sizeof(char)); + ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); + ft_memcpy(path + ft_strlen(*path_dirs),"/", 1); + ft_memcpy(path + ft_strlen(*path_dirs) + 1, cmd->argv[0], ft_strlen(cmd->argv[0])); + execve(path, cmd->argv, datas->envp); + free(path); + path_dirs++; } } diff --git a/minishell.c b/minishell.c index 76dea37..b16cf5d 100755 --- a/minishell.c +++ b/minishell.c @@ -62,7 +62,7 @@ void prompt(t_datas *datas) line = readline("$ "); termios(1); if (line == NULL) - halt(EXIT_FAILURE); + halt(datas, EXIT_FAILURE); if (is_empty(line)) return ; cmd = parser(datas, lexer(datas, line), NULL); @@ -88,6 +88,5 @@ int main(int argc, char **argv, char **envp) (void)argv; while (1) prompt(&datas); - clear_history(); - return (EXIT_SUCCESS); + halt(&datas, EXIT_SUCCESS); } diff --git a/minishell.h b/minishell.h index ca45c27..69fbd94 100755 --- a/minishell.h +++ b/minishell.h @@ -52,7 +52,7 @@ int is_empty(char *line); void handle_status(t_datas *datas, int status); void nothing(int sig_num); void sigs_handler(int sig_num); -void halt(int ret_code); +void halt(t_datas *datas, int ret_code); // ----------------------------------Utils_2.c int replace_var(t_datas *datas, char **line, \ @@ -61,10 +61,10 @@ int file_error(char *cmd, char *file, char *msg); char *expend_str(t_datas *datas, char *line); // ----------------------------------Builtins.c -int close_minishell(int exit_code); int ft_echo(t_command *cmd); int ft_pwd(t_command *cmd); int ft_cd(t_command *cmd); +int ft_exit(t_datas *datas, t_command *cmd); // ----------------------------------Env.c int is_valid_identifier(char *ident); diff --git a/utils.c b/utils.c index 7a8c965..a8eb2ea 100755 --- a/utils.c +++ b/utils.c @@ -50,8 +50,18 @@ void sigs_handler(int sig_num) return ; } -void halt(int ret_code) +void halt(t_datas *datas, int ret_code) { + int i; + + i = 0; + while (datas->envp[i]) + { + free(datas->envp[i]); + i++; + } + free(datas->envp); + clear_history(); printf("exit\n"); exit(ret_code); }