From: Dominik Kaiser Date: Thu, 16 Jan 2025 18:16:44 +0000 (+0100) Subject: Create files X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=3392f2b811269f174620832d663b09ef4f4e43f3;p=42%2Fminishell.git Create files --- diff --git a/Makefile b/Makefile index 650f5e4..243e9fc 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \ get_cmd_path.c env_to_strlst.c execute_cmd.c format_string.c \ builtins_part_one.c builtins_part_two.c env_tools.c error.c \ - read_heredoc.c + read_heredoc.c create_files.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/ast.h b/include/ast.h index cd2f9c9..0335473 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,7 +6,7 @@ /* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 18:24:09 by dkaiser ### ########.fr */ +/* Updated: 2025/01/16 18:38:44 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ void repl(const char *prompt, t_env **env, int *promptflag); t_list *parse(t_token *tokens, t_env **env); t_node *parse_cmd(t_token *tokens, t_env **env); -t_redirection *collect_redirs(t_token **tokens, t_env *env); +t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **cf); void print_ast(t_node *ast); int eval(t_node *node, t_env **env); @@ -47,4 +47,5 @@ int handle_redirections(t_redirection *redirs); void *error(int err_code, char *err_text, int exit_code, int *ret_code); char *read_heredoc(char *delimiter); +void create_files(t_list *files); #endif diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 4decda7..171dc06 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,20 +6,21 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 18:54:42 by dkaiser ### ########.fr */ +/* Updated: 2025/01/16 18:19:36 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include -static void collect_and_check_redir(t_redirection *result, t_token **cur, - t_env *env); -static void set_redir(t_redirection *redir, int type, char *spec, - t_env *env); -static int set_heredoc_data(t_token *cur, t_redirection *result, - t_env *env); +static void collect_and_check_redir(t_redirection *result, + t_token **cur, t_env *env, t_list **create_files); +static t_redirection *set_redir(t_redirection *redir, int type, char *spec, + t_env *env); +static int set_heredoc_data(t_token *cur, t_redirection *result, + t_env *env); -t_redirection *collect_redirs(t_token **tokens, t_env *env) +t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_files) { t_redirection *result; t_token *cur; @@ -33,7 +34,7 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env) while (cur != NULL && cur->next != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - collect_and_check_redir(result, &cur, env); + collect_and_check_redir(result, &cur, env, create_files); else if (cur->type == REDIR_TOKEN) return (free(result), NULL); else @@ -44,17 +45,30 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env) return (result); } -static void set_redir(t_redirection *redir, int type, char *spec, t_env *env) +static t_redirection *set_redir(t_redirection *redir, int type, char *spec, + t_env *env) { + t_redirection *result; + redir->type = type; if (spec != NULL) redir->specifier = format_string(spec, env); else redir->specifier = spec; + if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE) + { + result = malloc(sizeof(t_redirection)); + if (!result) + return (NULL); + result->type = type; + result->specifier = spec; + return (result); + } + return (NULL); } static void collect_and_check_redir(t_redirection *result, t_token **cur, - t_env *env) + t_env *env, t_list **create_files) { t_token *next_token; char *str; @@ -69,9 +83,11 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, else if ((*cur)->content.redir_type == INPUT_FILE) set_redir(&result[0], INPUT_FILE, str, env); else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) - set_redir(&result[1], OUTPUT_OVERRIDE, str, env); + ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], + OUTPUT_OVERRIDE, str, env))); else if ((*cur)->content.redir_type == OUTPUT_APPEND) - set_redir(&result[1], OUTPUT_APPEND, str, env); + ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], + OUTPUT_APPEND, str, env))); next_token = (*cur)->next; free_token_and_connect(*cur); if (next_token) diff --git a/src/create_files.c b/src/create_files.c new file mode 100644 index 0000000..faee27f --- /dev/null +++ b/src/create_files.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* create_files.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser + +void create_files(t_list *files) +{ + t_redirection *file; + int fd; + + while (files) + { + dbg("Test"); + if (files->content == NULL) + continue; + file = (t_redirection *)files->content; + if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1) + break; + if (file->type == OUTPUT_OVERRIDE) + { + fd = open(file->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + close(fd); + } + else if (file->type == OUTPUT_APPEND) + { + fd = open(file->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + close(fd); + } + /* if (files->next == NULL) */ + /* break; */ + /* if (((t_redirection *) files->next->content)->type == 0) */ + /* break; */ + files = files->next; + } +} diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 83addd2..8f5b541 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/15 15:52:08 by dkaiser ### ########.fr */ +/* Updated: 2025/01/16 18:38:00 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,7 @@ 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) { establish_pipeline(original_std[0], original_std[1]); diff --git a/src/interpreter.c b/src/interpreter.c index 0a6d781..c7fe67c 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */ -/* Updated: 2025/01/15 18:10:25 by dkaiser ### ########.fr */ +/* Updated: 2025/01/16 18:44:39 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ int handle_redirections(t_redirection *redirs) } else if (redirs[0].type == INPUT_LIMITER) { - fd = open("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644); if (fd < 0) { perror("open"); @@ -49,7 +49,7 @@ int handle_redirections(t_redirection *redirs) } if (redirs[1].type == OUTPUT_OVERRIDE) { - fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open(redirs[1].specifier, O_WRONLY | O_TRUNC, 0644); if (fd < 0) { perror("open"); @@ -60,7 +60,7 @@ int handle_redirections(t_redirection *redirs) } else if (redirs[1].type == OUTPUT_APPEND) { - fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + fd = open(redirs[1].specifier, O_WRONLY | O_APPEND, 0644); if (fd < 0) { perror("open"); diff --git a/src/new_node.c b/src/new_node.c index c58d291..83d9159 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -6,7 +6,7 @@ /* By: dkaiser content.cmd.redirs[0] = redirs[0]; node->content.cmd.redirs[1] = redirs[1]; + node->content.cmd.create_files = create_files; free(redirs); return (node); } diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 9ca741b..92dfd12 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 17:22:58 by dkaiser ### ########.fr */ +/* Updated: 2025/01/16 19:06:03 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,8 +18,10 @@ t_node *parse_cmd(t_token *tokens, t_env **env) { char **args; t_redirection *redirs; + t_list *create_files; - redirs = collect_redirs(&tokens, *env); + create_files = NULL; + redirs = collect_redirs(&tokens, *env, &create_files); if (redirs == NULL) return (NULL); args = collect_args(&tokens, env); @@ -28,7 +30,7 @@ t_node *parse_cmd(t_token *tokens, t_env **env) free(redirs); return (NULL); } - return (new_cmd_node(args, redirs)); + return (new_cmd_node(args, redirs, create_files)); } static char **collect_args(t_token **tokens, t_env **env) diff --git a/src/tokenizer.c b/src/tokenizer.c index 6d16f1d..eb5d8fe 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 15:58:41 by chuhlig ### ########.fr */ +/* Updated: 2025/01/16 18:57:38 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -101,6 +101,7 @@ void handle_special_chars(char *s, int *i, int *start, t_token **token_list) (*i)++; if (s[*i] == '>' && s[*i + 1] == '>') (*i)++; + print_token(*token_list); *start = *i + 1; }