From b994c87ade8e6e17819f5fb125cebde5ca2d1ef3 Mon Sep 17 00:00:00 2001 From: Ofacy Date: Tue, 9 Apr 2024 16:40:47 +0200 Subject: [PATCH] Fix all tested exit code issues --- includes/minishell.h | 4 +++- srcs/builtins/cd.c | 8 ++------ srcs/exec/exec.c | 16 +++++++++++----- srcs/exec/filetype.c | 3 ++- srcs/exec/here_doc.c | 4 ++-- srcs/exec/here_doc_utils.c | 4 +++- srcs/exec/path.c | 8 +++++--- srcs/exec/wait.c | 4 ++-- srcs/main.c | 11 +++++++---- srcs/prompt.c | 4 +++- 10 files changed, 40 insertions(+), 26 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index 851d4b5..087afa2 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: bwisniew +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/13 17:55:03 by lcottet #+# #+# */ -/* Updated: 2024/04/08 11:37:09 by lcottet ### ########.fr */ +/* Updated: 2024/04/08 19:54:38 by lcottet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -136,4 +136,6 @@ void signal_stop(int sig); void signal_heredoc(int sig); void reset_signal(void); +int ft_atoll(const char *nptr); + #endif diff --git a/srcs/builtins/cd.c b/srcs/builtins/cd.c index ba18573..bdd9d55 100644 --- a/srcs/builtins/cd.c +++ b/srcs/builtins/cd.c @@ -6,7 +6,7 @@ /* By: bwisniew +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/14 13:57:43 by bwisniew #+# #+# */ -/* Updated: 2024/04/09 15:37:39 by bwisniew ### ########.fr */ +/* Updated: 2024/04/09 16:37:12 by lcottet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -80,11 +80,7 @@ int cd_home(t_mshell *sh) custom_error("cd", "HOME not set"); return (1); } - if (chdir(home->value) == -1) - { - error("cd"); - return (1); - } + chdir(home->value); return (cd_change_env(sh, NULL)); } diff --git a/srcs/exec/exec.c b/srcs/exec/exec.c index d36ebe8..8ae3f6b 100644 --- a/srcs/exec/exec.c +++ b/srcs/exec/exec.c @@ -6,7 +6,7 @@ /* By: bwisniew +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/10 17:18:34 by lcottet #+# #+# */ -/* Updated: 2024/04/08 14:29:39 by bwisniew ### ########.fr */ +/* Updated: 2024/04/09 16:28:56 by lcottet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,9 +44,15 @@ int exec_set_cmd(t_execute *exec, t_mshell *sh) if (!exec->cmd) { if (errno == ENOENT) + { custom_error(((char **)exec->args.tab)[0], "command not found"); + set_env_return(sh, 127); + } else + { error(((char **)exec->args.tab)[0]); + set_env_return(sh, 126); + } return (1); } return (0); @@ -87,7 +93,7 @@ pid_t exec_txt(t_execute *exec, t_mshell *sh) if (vector_add(&exec->args, &null) != 0) return (-1); if (exec_set_cmd(exec, sh)) - return (-1); + return (-4); if (exec->builtin && !exec->has_pipe) { if (exec_builtins(exec, sh) != 0) @@ -98,7 +104,7 @@ pid_t exec_txt(t_execute *exec, t_mshell *sh) if (filetype == -1) return (free(exec->cmd), -1); else if (!filetype) - return (free(exec->cmd), -4); + return (free(exec->cmd), set_env_return(sh, 126), -4); pid = exec_fork(exec, sh); free(exec->cmd); return (pid); @@ -119,11 +125,11 @@ pid_t exec(t_mshell *sh) { if (exec_prepare(sh, &exec, &i) != 0) return (close_exec(&exec), vector_free(&exec.args), -1); - pid = -3; + pid = -2; if (exec.in > 0 && exec.out > 0) { pid = exec_txt(&exec, sh); - if (pid < 0) + if (pid < 0 && pid == -1) return (close_exec(&exec), vector_free(&exec.args), pid); } exec_init(&exec, sh, true); diff --git a/srcs/exec/filetype.c b/srcs/exec/filetype.c index 785576e..92d69f4 100644 --- a/srcs/exec/filetype.c +++ b/srcs/exec/filetype.c @@ -6,12 +6,13 @@ /* By: lcottet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/03 23:12:15 by lcottet #+# #+# */ -/* Updated: 2024/04/03 23:39:57 by lcottet ### ########.fr */ +/* Updated: 2024/04/09 16:28:15 by lcottet ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include #include "minishell.h" diff --git a/srcs/exec/here_doc.c b/srcs/exec/here_doc.c index 6c4a86d..0b1dc4e 100644 --- a/srcs/exec/here_doc.c +++ b/srcs/exec/here_doc.c @@ -6,7 +6,7 @@ /* By: bwisniew +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/10 20:41:37 by lcottet #+# #+# */ -/* Updated: 2024/04/08 11:46:31 by lcottet ### ########.fr */ +/* Updated: 2024/04/08 18:58:18 by lcottet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,7 +50,7 @@ char *here_doc_getline(t_mshell *sh, size_t i, t_fd fd, int *err) char *tmp; line.txt = hd_get_user_input(); - if (g_signal != -1) + if (g_signal != 0) return (here_doc_signal(&line, sh, err)); else if (!line.txt) { diff --git a/srcs/exec/here_doc_utils.c b/srcs/exec/here_doc_utils.c index c6716b0..41a894b 100644 --- a/srcs/exec/here_doc_utils.c +++ b/srcs/exec/here_doc_utils.c @@ -6,7 +6,7 @@ /* By: lcottet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/08 11:22:29 by lcottet #+# #+# */ -/* Updated: 2024/04/08 11:43:58 by lcottet ### ########.fr */ +/* Updated: 2024/04/09 16:33:58 by lcottet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,13 @@ #include "minishell.h" #include #include +#include char *hd_get_user_input(void) { if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO)) return (readline(PROMPT_HEREDOC)); + errno = 0; return (get_next_line(STDIN_FILENO)); } diff --git a/srcs/exec/path.c b/srcs/exec/path.c index 1defef3..9c3a35d 100644 --- a/srcs/exec/path.c +++ b/srcs/exec/path.c @@ -6,7 +6,7 @@ /* By: bwisniew +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/10 17:23:03 by lcottet #+# #+# */ -/* Updated: 2024/04/08 13:03:54 by lcottet ### ########.fr */ +/* Updated: 2024/04/09 16:32:14 by lcottet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,6 +43,8 @@ char *path_valid(char *file, char *curr_path, int access_mode) char *tmp1; char *tmp; + if (file[0] == '\0') + return (NULL); tmp1 = ft_strjoin(curr_path, "/"); if (!tmp1) return (NULL); @@ -62,8 +64,6 @@ char *get_openable_path(char *file, int access_mode, t_mshell *sh) int i; char **path; - if (!file) - return (NULL); if (ft_strchr(file, '/') && access(file, access_mode) != -1) return (ft_strdup(file)); if (errno == EACCES) @@ -81,6 +81,8 @@ char *get_openable_path(char *file, int access_mode, t_mshell *sh) return (ft_freesplit(path), NULL); i++; } + if (errno == 0) + errno = ENOENT; ft_freesplit(path); return (NULL); } diff --git a/srcs/exec/wait.c b/srcs/exec/wait.c index f635f06..3a79f77 100644 --- a/srcs/exec/wait.c +++ b/srcs/exec/wait.c @@ -6,7 +6,7 @@ /* By: bwisniew +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/11 17:58:52 by bwisniew #+# #+# */ -/* Updated: 2024/04/08 14:04:46 by bwisniew ### ########.fr */ +/* Updated: 2024/04/08 19:16:31 by lcottet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ int wait_for_child(pid_t last_pid) return (1); if (errno != ECHILD) return (error("wait"), 127); - if (last_pid == -1 && g_signal == 0) + if (last_pid == -5 || (last_pid == -1 && g_signal == 0)) return (127); else if (last_pid == -1) return (128 + g_signal); diff --git a/srcs/main.c b/srcs/main.c index 4436746..2f2b1a6 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: bwisniew +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/14 13:11:23 by lcottet #+# #+# */ -/* Updated: 2024/04/08 14:10:03 by bwisniew ### ########.fr */ +/* Updated: 2024/04/09 16:35:22 by lcottet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ #include #include #include +#include int minishell(t_mshell *sh, char *input) { @@ -29,6 +30,7 @@ int minishell(t_mshell *sh, char *input) return (-1); if (sh->tokens.len != 0 && isatty(STDERR_FILENO)) add_history(input); + errno = 0; syntax = check_syntax(&sh->tokens); if (syntax == 0) { @@ -53,10 +55,12 @@ void finish_mshell(t_mshell *mshell) close_fd(&mshell->stdin); vector_foreach(&mshell->env, (void (*)(void *))env_free); vector_free(&mshell->env); - free(mshell->last_return.value); close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); + if (mshell->exit == -1) + mshell->exit = (unsigned int)ft_atoll(mshell->last_return.value); + free(mshell->last_return.value); } int prepare_mshell(t_mshell *mshell, char **env) @@ -96,8 +100,7 @@ int main(int argc, char **argv, char **env) } if (isatty(STDIN_FILENO)) printf("exit\n"); + errno = 0; finish_mshell(&mshell); - if (mshell.exit == -1) - return (0); return ((char)mshell.exit); } diff --git a/srcs/prompt.c b/srcs/prompt.c index 2937063..44b12a9 100644 --- a/srcs/prompt.c +++ b/srcs/prompt.c @@ -6,7 +6,7 @@ /* By: bwisniew +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/14 13:12:55 by lcottet #+# #+# */ -/* Updated: 2024/04/08 14:05:40 by bwisniew ### ########.fr */ +/* Updated: 2024/04/09 16:33:49 by lcottet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ #include #include #include +#include t_token prompt(t_mshell *sh) { @@ -52,6 +53,7 @@ char *get_user_input(t_mshell *sh) input = readline(c_prompt.txt); else input = get_next_line(STDIN_FILENO); + errno = 0; if (g_signal != 0) { set_env_return(sh, 128 + g_signal);