From a58f9a0c76439a75a57a72f809b4805dced341e8 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Mon, 20 Mar 2023 15:31:39 +0100 Subject: [PATCH] fix: exit code works --- bozo | 0 bozoman | 3 --- env/env_fill.c | 1 + execution/execution.c | 2 -- main.c | 7 ++++++- parse/parse.c | 22 +++++++++++++++++----- utils/ft_get_executable.c | 18 ++++++++++++------ utils/utils.h | 2 +- 8 files changed, 37 insertions(+), 18 deletions(-) delete mode 100644 bozo delete mode 100644 bozoman diff --git a/bozo b/bozo deleted file mode 100644 index e69de29..0000000 diff --git a/bozoman b/bozoman deleted file mode 100644 index a609f77..0000000 --- a/bozoman +++ /dev/null @@ -1,3 +0,0 @@ -hi >> ./bozoman -hi >> ./bozoman -hi diff --git a/env/env_fill.c b/env/env_fill.c index 504573b..df9885f 100644 --- a/env/env_fill.c +++ b/env/env_fill.c @@ -86,6 +86,7 @@ char *ft_env_filler(t_data *data, const char *str) char *temp; char *value; + ft_gen_exit_code(data); out = ft_strdup(str); if (out == NULL) { diff --git a/execution/execution.c b/execution/execution.c index cc921ef..8023e24 100644 --- a/execution/execution.c +++ b/execution/execution.c @@ -92,8 +92,6 @@ static int ft_cmd_executor(t_data *data, t_cmd *cmd, int fd) if (exit_code == 1) return (1); } - if (ft_gen_exit_code(data)) - return (1); return (0); } diff --git a/main.c b/main.c index e4a965d..12da898 100644 --- a/main.c +++ b/main.c @@ -10,6 +10,7 @@ /* */ /* ************************************************************************** */ +#include "data/data.h" #include "minishell.h" static char *ft_get_user_input() @@ -59,7 +60,12 @@ static int ft_minishell(t_data *data, char *line) { content = current->content; if (content->own_cmd == 0 && content->pid != -1) + { waitpid(content->pid, &data->exit_code, 0); + data->exit_code = data->exit_code / 256; + } + else + data->exit_code = 0; current = current->next; } ft_lstclear(data->cmds, ft_cmddel); @@ -103,7 +109,6 @@ int main(int ac, char **av, char **env) signal(SIGQUIT, ft_quit); data.exit_code = 0; data.exit_code_str = NULL; - ft_gen_exit_code(&data); data.cmds = malloc(sizeof(t_cmd *)); if (data.cmds == NULL) return (1); diff --git a/parse/parse.c b/parse/parse.c index ece8a33..be1dd7c 100644 --- a/parse/parse.c +++ b/parse/parse.c @@ -45,10 +45,14 @@ static int ft_executable_parse(t_data *data, t_cmd *cmd) else if (ft_strcmp(cmd->args[0], "cd") == 0) own = 1; else - path = ft_get_executable(data->env, cmd->args[0]); + { + path = ft_get_executable(data, cmd->args[0]); + if (path == NULL) + return (1); + } cmd->own_cmd = own; cmd->executable = path; - return (own); + return (0); } int ft_cmd_parser(t_data *data, char *cmd_str) @@ -65,14 +69,18 @@ int ft_cmd_parser(t_data *data, char *cmd_str) if (ft_redirection(data, cmd, cmd_str)) { ft_cmddel(cmd); - return (1); + return (0); } if (ft_args_parse(cmd_str, cmd)) { ft_cmddel(cmd); return (1); } - ft_executable_parse(data, cmd); + if (ft_executable_parse(data, cmd)) + { + ft_cmddel(cmd); + return (1); + } element = ft_lstnew(cmd); if (element == NULL) { @@ -98,7 +106,11 @@ int ft_cmds_parser(t_data *data, const char *line) i = 0; while (tab[i] != NULL) { - ft_cmd_parser(data, tab[i]); + if (ft_cmd_parser(data, tab[i])) + { + ft_freer_tab_ultimate(1, tab); + return (1); + } i++; } if (*data->cmds != NULL) diff --git a/utils/ft_get_executable.c b/utils/ft_get_executable.c index fa06552..2ad3c54 100644 --- a/utils/ft_get_executable.c +++ b/utils/ft_get_executable.c @@ -1,17 +1,19 @@ #include "./utils.h" #include -char *ft_get_executable_with_path(const char *name) +char *ft_get_executable_with_path(t_data *data, const char *name) { char *path; if (access(name, F_OK) != 0) { + data->exit_code = 127; ft_eprintf("minishell: %s bash: No such file or directery\n"); return (NULL); } if (access(name, X_OK) != 0) { + data->exit_code = 126; ft_eprintf("minishell: %s: permission denied\n", name); return (NULL); } @@ -24,7 +26,7 @@ char *ft_get_executable_with_path(const char *name) return (path); } -char *ft_get_executable_without_path(t_list **env, const char *name) +char *ft_get_executable_without_path(t_data *data, const char *name) { char **tab; char *paths; @@ -32,9 +34,10 @@ char *ft_get_executable_without_path(t_list **env, const char *name) size_t i; path = NULL; - paths = get_value_by_key("PATH", env); + paths = get_value_by_key("PATH", data->env); if (paths == NULL) { + data->exit_code = 127; ft_eprintf("minishell: %s: command not found\n", name); return (NULL); } @@ -61,17 +64,20 @@ char *ft_get_executable_without_path(t_list **env, const char *name) } ft_freer_tab_ultimate(1, tab); if (path == NULL) + { + data->exit_code = 127; ft_eprintf("minishell: %s: command not found\n", name); + } return (path); } -char *ft_get_executable(t_list **env, const char *name) +char *ft_get_executable(t_data *data, const char *name) { char *path; if (name[0] == '.' || name[0] == '/') - path = ft_get_executable_with_path(name); + path = ft_get_executable_with_path(data, name); else - path = ft_get_executable_without_path(env, name); + path = ft_get_executable_without_path(data, name); return (path); } diff --git a/utils/utils.h b/utils/utils.h index d54cce4..88f8725 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -28,7 +28,7 @@ char **ft_split_quoted(const char *s, char c); void ft_strshift(char *str, int shift); char *ft_quote_remover(char *str); int ft_atoi_check(const char *nptr); -char *ft_get_executable(t_list **env, const char *name); +char *ft_get_executable(t_data *data, const char *name); void ft_closer(int fds[2]); void ft_add_fd(int fds[2], int fd);