diff --git a/data/data.c b/data/data.c index f7075fe..79a9bd7 100644 --- a/data/data.c +++ b/data/data.c @@ -6,16 +6,16 @@ /* By: cchauvet exit_code); + value = ft_itoa(*data->exit_code); else if (ft_strcmp(key, "$") == 0) value = ft_strdup("PID"); else if (key[0] == '\0') diff --git a/execution/execution.c b/execution/execution.c index 81e0645..c8c5003 100644 --- a/execution/execution.c +++ b/execution/execution.c @@ -6,7 +6,7 @@ /* By: cchauvet fd_out[0], cmd->args + 1)); else { - return_code = ft_exit(cmd->args + 1, data->exit_code); + return_code = ft_exit(cmd->args + 1, *data->exit_code); if (return_code >= 0) { - data->exit_code = return_code; + *data->exit_code = return_code; return (-2); } } - data->exit_code = return_code; + *data->exit_code = return_code; return (return_code); } @@ -46,11 +46,11 @@ static bool ft_executor(t_data *data, t_cmd *cmd, char **env) if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL) return (0); cmd->pid = fork(); - ft_get_data()->child_pid = cmd->pid; if (cmd->pid == -1) return (1); if (cmd->pid == 0) { + signal(SIGQUIT, ft_quit); dup2(cmd->fd_in[0], 0); dup2(cmd->fd_out[0], 1); ft_lstiter(*data->cmds, ft_cmdcloser); diff --git a/execution/execution_private.h b/execution/execution_private.h index 5b6aab0..2633787 100644 --- a/execution/execution_private.h +++ b/execution/execution_private.h @@ -6,12 +6,14 @@ /* By: cchauvet +# include "../signal/signal.h" # include "../data/data.h" # include "../libftx/libftx.h" # include "../cmd/cmd.h" diff --git a/libftx/printf/libftprintf.a b/libftx/printf/libftprintf.a deleted file mode 100644 index 968fe3a..0000000 Binary files a/libftx/printf/libftprintf.a and /dev/null differ diff --git a/main.c b/main.c index f497d15..9324ec3 100644 --- a/main.c +++ b/main.c @@ -6,14 +6,17 @@ /* By: cchauvet static char *ft_get_user_input(void) { @@ -56,17 +59,22 @@ static void ft_cmds_waiter(t_data *data) waitpid(cmd->pid, &exit_status, 0); if (WIFSIGNALED(exit_status)) { - if (WTERMSIG(exit_status) == SIGKILL) - data->exit_code = 131; + if (exit_status == 131) + { + ft_printf("Quit (core dumped)\n"); + *data->exit_code = 131; + } else - data->exit_code = 130; + { + ft_putchar_fd('\n', 1); + *data->exit_code = 130; + } } else - data->exit_code = WEXITSTATUS(exit_status); + *data->exit_code = WEXITSTATUS(exit_status); } current = current->next; } - data->child_pid = 0; } static int ft_minishell(t_data *data, char *line) @@ -93,8 +101,7 @@ static int ft_minishell(t_data *data, char *line) int ft_init_data(t_data *data, char **env) { - data->exit_code = 0; - data->child_pid = 0; + data->exit_code = ft_get_exit_code(); data->cmds = malloc(sizeof(t_cmd *)); if (data->cmds == NULL) { @@ -114,29 +121,28 @@ int ft_init_data(t_data *data, char **env) int main(int ac, char **av, char **env) { - t_data *data; + t_data data; char *line; (void) ac; (void) av; signal(SIGINT, ft_ctrlc); - signal(SIGQUIT, ft_quit); - data = ft_get_data(); - if (ft_init_data(data, env)) + signal(SIGQUIT, SIG_IGN); + if (ft_init_data(&data, env)) return (1); line = ft_get_user_input(); while (line != NULL) { - if (ft_minishell(data, line) == 1) + if (ft_minishell(&data, line) == 1) break ; free(line); line = ft_get_user_input(); if (line == NULL) break ; } - ft_lstclear(data->cmds, ft_cmddel); - free(data->cmds); - ft_lstclear(data->env, env_del); - free(data->env); - return (data->exit_code); + ft_lstclear(data.cmds, ft_cmddel); + free(data.cmds); + ft_lstclear(data.env, env_del); + free(data.env); + return (*data.exit_code); } diff --git a/minishell.h b/minishell.h index 5b8af0f..5e6ee2c 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: cchauvet # include # include # include diff --git a/redirection/check.c b/redirection/check.c index e97db5b..a089045 100644 --- a/redirection/check.c +++ b/redirection/check.c @@ -6,7 +6,7 @@ /* By: cchauvet fd_out[0] == -2) return (0); - if (ft_file_is_writable(data, redirection)) + if (ft_file_is_appendable(data, redirection)) { fd = open(redirection, O_WRONLY | O_APPEND | O_CREAT, 0644); diff --git a/redirection/file.c b/redirection/file.c index 4a49779..d7604b4 100644 --- a/redirection/file.c +++ b/redirection/file.c @@ -6,7 +6,7 @@ /* By: cchauvet exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: No such file or directory\n", path); return (0); } readable = read(fd, "", 0); if (readable == -1) { - data->exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: Permission denied\n", path); return (0); } @@ -43,14 +43,14 @@ int ft_file_is_writable(t_data *data, const char *path) fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644); if (fd == -1) { - data->exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: Permission denied\n", path); return (0); } writeable = write(fd, "", 0); if (writeable == -1) { - data->exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: Permission denied\n", path); return (0); } @@ -66,14 +66,14 @@ int ft_file_is_appendable(t_data *data, const char *path) fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0644); if (fd == -1) { - data->exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: Permission denied\n", path); return (0); } writeable = write(fd, "", 0); if (writeable == -1) { - data->exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: Permission denied\n", path); return (0); } diff --git a/redirection/heredoc.c b/redirection/heredoc.c index cb0ca04..f1f707d 100644 --- a/redirection/heredoc.c +++ b/redirection/heredoc.c @@ -6,7 +6,7 @@ /* By: cchauvet 2) + close(*ft_get_heredoc()); *ft_get_heredoc() = -1; return (-2); } diff --git a/signal/signal.c b/signal/signal.c index 2e166ca..538fcb6 100644 --- a/signal/signal.c +++ b/signal/signal.c @@ -6,61 +6,23 @@ /* By: cchauvet exit_code = 130; - if (*ft_get_heredoc() != -1) - { - if (*ft_get_heredoc() > 2) - close(*ft_get_heredoc()); - *ft_get_heredoc() = -1; - } - else - { - if (data->child_pid > 1) - { - data->child_pid = 0; - ft_putchar_fd('\n', 1); - } - else - ft_new_line(); - } -} - void ft_quit(int num) { - t_data *data; - (void) num; - data = ft_get_data(); - data->exit_code = 131; - if (data->child_pid > 1) - { - ft_printf("Quit (core dumped)\n"); - data->child_pid = 0; - } - else - { - rl_replace_line("", 0); - rl_redisplay(); - } + ft_printf("Quit (core dumped)\n"); } diff --git a/signal/signal_private.h b/signal/signal_private.h index 1817ef0..730d47c 100644 --- a/signal/signal_private.h +++ b/signal/signal_private.h @@ -6,13 +6,14 @@ /* By: cchauvet +# include # include # include # include "../libftx/libftx.h" diff --git a/syntax/syntax.c b/syntax/syntax.c index 754d133..32d0d5e 100644 --- a/syntax/syntax.c +++ b/syntax/syntax.c @@ -6,7 +6,7 @@ /* By: cchauvet exit_code = 2; + *data->exit_code = 2; return (1); } return (0); diff --git a/tester b/tester deleted file mode 160000 index 1c6111b..0000000 --- a/tester +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 1c6111b2fd281937d38ebfa7e8d87b38baef0802 diff --git a/utils/ft_get_executable.c b/utils/ft_get_executable.c index 5a84117..4e9c8f1 100644 --- a/utils/ft_get_executable.c +++ b/utils/ft_get_executable.c @@ -6,7 +6,7 @@ /* By: cchauvet exit_code = 127; + *data->exit_code = 127; ft_eprintf("minishell: %s: No such file or directery\n", name); return (NULL); } if (access(name, X_OK) != 0) { - data->exit_code = 126; + *data->exit_code = 126; ft_eprintf("minishell: %s: permission denied\n", name); return (NULL); } @@ -46,7 +46,7 @@ static char **ft_get_paths(t_data *data, const char *name) paths = get_value_by_key("PATH", data->env); if (paths == NULL) { - data->exit_code = 127; + *data->exit_code = 127; ft_eprintf("minishell: %s: command not found\n", name); return (NULL); } @@ -97,7 +97,7 @@ static char *ft_get_executable_without_path(t_data *data, const char *name) ft_freer_tab_ultimate(1, paths); if (path == NULL) { - data->exit_code = 127; + *data->exit_code = 127; ft_eprintf("minishell: %s: command not found\n", name); } return (path);