From f2ffaa70eb299c5e2bb7c181d2910337de7e99d3 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 20 Jan 2025 17:13:37 +0100 Subject: [PATCH] Fix some errors --- include/minishell.h | 4 ++-- src/collect_redirs.c | 13 +++++++------ src/create_files.c | 11 ++++++++--- src/execute_cmd.c | 8 +++++--- src/handle_redir.c | 8 ++++---- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/include/minishell.h b/include/minishell.h index 7a573e8..df89630 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/18 19:30:43 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 14:39:38 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,6 +55,6 @@ int handle_pipe_child(int p[2], t_node *node, t_env **env, int in_fd); int open_file(char *path, int flags, int mode); int eval_rec(t_node *node, t_env **env, int in_fd); -void create_files(t_list *files); +int create_files(t_list *files); #endif diff --git a/src/collect_redirs.c b/src/collect_redirs.c index aeaf177..db4ab00 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 13:17:59 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 17:12:42 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, return (free_tokens(*tokens), NULL); set_redir(&result[0], 0, NULL, env); set_redir(&result[1], 0, NULL, env); - while (cur != NULL && cur->next != NULL) + while (cur != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) collect_and_check_redir(result, &cur, env, create_files); @@ -60,17 +60,18 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, return ; } else if ((*cur)->content.redir_type == INPUT_FILE) - set_redir(&result[0], INPUT_FILE, format_string(str, env), env); + ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[0], INPUT_FILE, format_string(str, env, 0), env))); else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], - OUTPUT_OVERRIDE, format_string(str, env), env))); + OUTPUT_OVERRIDE, format_string(str, env, 0), env))); else if ((*cur)->content.redir_type == OUTPUT_APPEND) ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], - OUTPUT_APPEND, format_string(str, env), env))); + OUTPUT_APPEND, format_string(str, env, 0), env))); next_token = (*cur)->next; // free_token_and_connect(*cur); if (next_token) { + free_token_and_connect(*cur); *cur = next_token->next; free_token_and_connect(next_token); } @@ -88,7 +89,7 @@ static t_redirection *set_redir(t_redirection *redir, int type, char *spec, redir->specifier = format_string(spec, env, ft_atoi("0")); else redir->specifier = spec; - if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE) + if (redir->type != INPUT_LIMITER) { result = malloc(sizeof(t_redirection)); if (!result) diff --git a/src/create_files.c b/src/create_files.c index 8689f88..0550e57 100644 --- a/src/create_files.c +++ b/src/create_files.c @@ -6,24 +6,28 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */ -/* Updated: 2025/01/19 14:36:59 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 15:54:00 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" #include -void create_files(t_list *files) +int create_files(t_list *files) { t_redirection *file; int fd; while (files) { - dbg("Test"); if (files->content == NULL) + { + files = files->next; continue ; + } file = (t_redirection *)files->content; + if (file->type == INPUT_FILE && (access(file->specifier, F_OK) == -1 || access(file->specifier, R_OK) == -1)) + return (EXIT_FAILURE); if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1) break ; if (file->type == OUTPUT_OVERRIDE) @@ -42,4 +46,5 @@ void create_files(t_list *files) break ; files = files->next; } + return (EXIT_SUCCESS); } diff --git a/src/execute_cmd.c b/src/execute_cmd.c index e2b9d66..ac0d5b1 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */ -/* Updated: 2025/01/19 19:15:46 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 15:54:43 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,8 +51,8 @@ int execute_cmd(t_cmd *cmd, t_env **env) original_std[1] = dup(STDOUT_FILENO); original_std[0] = dup(STDIN_FILENO); - create_files(cmd->create_files); - if (handle_redirections(cmd->redirs) == -1) + result = create_files(cmd->create_files); + if (result != EXIT_SUCCESS || handle_redirections(cmd->redirs) == -1) { establish_pipeline(original_std[0], original_std[1]); return (EXIT_FAILURE); @@ -63,6 +63,8 @@ int execute_cmd(t_cmd *cmd, t_env **env) establish_pipeline(original_std[0], original_std[1]); return (result); } + if (result != EXIT_SUCCESS) + return (result); return (exec_cmd(cmd, env, original_std, EXIT_SUCCESS)); } diff --git a/src/handle_redir.c b/src/handle_redir.c index 29bba92..03fa453 100644 --- a/src/handle_redir.c +++ b/src/handle_redir.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/18 18:34:51 by chuhlig #+# #+# */ -/* Updated: 2025/01/18 18:47:31 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 15:24:49 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ int handle_input_redirection(t_redirection *redir) } else if (redir->type == INPUT_LIMITER) { - fd = open_file("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open_file("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644); if (fd < 0) return (-1); write(fd, redir->specifier, ft_strlen(redir->specifier)); @@ -46,7 +46,7 @@ int handle_output_redirection(t_redirection *redir) if (redir->type == OUTPUT_OVERRIDE) { - fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open_file(redir->specifier, O_WRONLY | O_TRUNC, 0644); if (fd < 0) return (-1); dup2(fd, STDOUT_FILENO); @@ -54,7 +54,7 @@ int handle_output_redirection(t_redirection *redir) } else if (redir->type == OUTPUT_APPEND) { - fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + fd = open_file(redir->specifier, O_WRONLY | O_APPEND, 0644); if (fd < 0) return (-1); dup2(fd, STDOUT_FILENO); -- 2.47.2