check file availability

This commit is contained in:
Quinten Mennen 2025-02-26 16:42:50 +01:00
parent f3bd69a2e9
commit 12ab3f5e12
5 changed files with 43 additions and 38 deletions

View File

@ -1,23 +1,23 @@
VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executor:src/parser:src/debug:src/expander:src/builtin:src/signal:src/redirect: VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executor:src/parser:src/debug:src/expander:src/builtin:src/signal:src/redirect:
SOURCES = history_load.c history_write.c prompt.c ft_lstclear_safe.c \ SOURCES = history_load.c history_write.c prompt.c print_banner.c \
ft_lstnew_safe.c init_minishell.c malloc_safe.c print_banner.c \ init_minishell.c ft_substr_safe.c check_malloc.c error_msg.c \
free_minishell_line.c error_msg.c free_command_list.c \ free_command_list.c free_freelist.c free_lexer.c free_minishell.c \
ft_strdup_safe.c ft_strjoin_safe.c ft_substr_safe.c check_malloc.c \ free_minishell_line.c free_safe.c free_token.c free_token_list.c \
free_freelist.c free_lexer.c free_minishell.c free_safe.c \ ft_lstclear_safe.c ft_lstnew_safe.c ft_strdup_safe.c \
free_token.c free_token_list.c lexer_parse_input.c lexer_read_word.c \ ft_strjoin_safe.c malloc_safe.c lexer_read_char.c lexer_new.c \
lexer_token_next.c lexer_read_char.c lexer_new.c token_parse.c \ lexer_parse_input.c lexer_read_word.c lexer_token_next.c token_new.c \
token_new.c environment_free.c environment_free_list.c \ token_parse.c environment_add.c environment_del.c environment_free.c \
environment_print.c environment_add.c environment_del.c \ environment_free_list.c environment_get.c environment_get_arr.c \
environment_get.c environment_get_arr.c environment_parse.c \ environment_parse.c environment_print.c executor_close_fds.c \
executor_close_fds.c executor_count_fds.c executor_create_pipes.c \ executor_absolute_path.c executor_child.c executor_count_fds.c \
executor_create_redirects.c executor_execute_pipeline.c \ executor_create_pipes.c executor_create_redirects.c \
executor_fork.c executor_open_fds.c executor_absolute_path.c \ executor_execute_pipeline.c executor_fork.c executor_open_fds.c \
executor_child.c parser_get_commands.c parser_get_arguments.c \ parser_get_arguments.c parser_new_command.c parser_get_commands.c \
parser_new_command.c print_commands.c print_freelist.c \ print_commands.c print_freelist.c expander_is_character.c \
expander_is_character.c expander_allocate_memory.c \ expander_expand_dollar.c expander_allocate_memory.c \
expander_expand_dollar.c expander_get_variable.c \ expander_parse_string.c expander_parse_variables.c \
expander_parse_string.c expander_parse_variables.c builtin_echo.c \ expander_get_variable.c builtin_cd.c builtin_echo.c builtin_env.c \
builtin_router.c simple_builtins.c builtin_cd.c builtin_env.c \ builtin_exit.c builtin_export.c builtin_pwd.c builtin_router.c \
builtin_exit.c builtin_export.c builtin_pwd.c builtin_unset.c \ builtin_unset.c is_builtin.c simple_builtins.c signal.c \
is_builtin.c signal.c redirect_valid_type.c redirect_get_inputs.c \ signal_init.c.c redirect_get_outputs.c redirect_new.c \
redirect_get_outputs.c redirect_new.c main.c \ redirect_valid_type.c redirect_get_inputs.c main.c \

View File

@ -21,7 +21,7 @@ t_environment *expander_get_var(const char *s, int idx, t_minishell *msh)
i = 0; i = 0;
while (expander_character_valid(s[idx + i])) while (expander_character_valid(s[idx + i]))
i++; i++;
name = ft_substr_safe(minishell, s, idx, i); name = ft_substr_safe(msh, s, idx, i);
if (!name || !*name) if (!name || !*name)
return (NULL); return (NULL);
env = environment_get(msh, name); env = environment_get(msh, name);

View File

@ -18,7 +18,6 @@ static int is_command_token(t_token *token)
return (token->type < 3 || redirect_token_type(token)); return (token->type < 3 || redirect_token_type(token));
} }
/*
static int validate_redirects(t_list *lst) static int validate_redirects(t_list *lst)
{ {
t_list *token; t_list *token;
@ -26,13 +25,12 @@ static int validate_redirects(t_list *lst)
token = lst; token = lst;
while (token) while (token)
{ {
if (((t_token *)token)->type == T_ERROR) if (((t_redirect *)token->content)->type == T_ERROR)
return (0); return (0);
token = token->next; token = token->next;
} }
return (1); return (1);
} }
*/
t_list *parser_get_commands(t_minishell *msh) t_list *parser_get_commands(t_minishell *msh)
{ {
@ -42,9 +40,9 @@ t_list *parser_get_commands(t_minishell *msh)
t_token *token; t_token *token;
command_list = NULL; command_list = NULL;
if (!minishell->tokens) if (!msh->tokens)
return (NULL); return (NULL);
current = minishell->tokens; current = msh->tokens;
while (current) while (current)
{ {
token = (t_token *) current->content; token = (t_token *) current->content;
@ -52,6 +50,10 @@ t_list *parser_get_commands(t_minishell *msh)
command->args = parser_get_arguments(current, msh); command->args = parser_get_arguments(current, msh);
command->redirect_in = redirect_get_inputs(msh, current); command->redirect_in = redirect_get_inputs(msh, current);
command->redirect_out = redirect_get_outputs(msh, current); command->redirect_out = redirect_get_outputs(msh, current);
if (!validate_redirects(command->redirect_in))
{
break ;
}
ft_lstadd_back(&command_list, ft_lstnew_safe(msh, command)); ft_lstadd_back(&command_list, ft_lstnew_safe(msh, command));
while (current && is_command_token((t_token *)current->content)) while (current && is_command_token((t_token *)current->content))
current = current->next; current = current->next;

View File

@ -19,7 +19,7 @@ static int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim)
int fd; int fd;
fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644); fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) // TODO: Will this work? if (fd < 0)
{ {
error_msg("heredoc", "unable to write to temp file"); error_msg("heredoc", "unable to write to temp file");
return (fd); return (fd);
@ -67,11 +67,11 @@ t_list *redirect_get_inputs(t_minishell *msh, t_list *list)
current = current->next; current = current->next;
continue ; continue ;
} }
if (token->type == T_HEREDOC && redirect_is_valid(current, token, F_OK | W_OK)) if (token->type == T_HEREDOC && redirect_is_valid(current, token, -1))
result = process_heredoc(msh, token, current->next->content); result = process_heredoc(msh, token, current->next->content);
if (redirect_is_valid(current, token, F_OK | R_OK)) if (redirect_is_valid(current, token, F_OK | R_OK))
{ {
ft_lstadd_back(&redirects, ft_lstnew_safe(minishell, ft_lstadd_back(&redirects, ft_lstnew_safe(msh,
redirect_new(msh, token->type, redirect_new(msh, token->type,
ft_strdup_safe(msh, ft_strdup_safe(msh,
((t_token *)current->next->content)->value)))); ((t_token *)current->next->content)->value))));

View File

@ -9,17 +9,17 @@
/* Updated: 2025/02/23 12:30:18 by Quinten ######## odam.nl */ /* Updated: 2025/02/23 12:30:18 by Quinten ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int redirect_token_type(t_token *token) int redirect_token_type(t_token *token)
{ {
return (token->type == T_REDIRECT_IN return (token->type == T_REDIRECT_IN
|| token->type == T_HEREDOC || token->type == T_HEREDOC
|| token->type == T_REDIRECT_OUT || token->type == T_REDIRECT_OUT
|| token->type == T_APPEND_OUT); || token->type == T_APPEND_OUT);
} }
int redirect_is_valid(t_list *lst, t_token *token, int mode) int redirect_is_valid(t_list *lst, t_token *token, int mode)
{ {
t_token *next; t_token *next;
@ -31,8 +31,11 @@ int redirect_is_valid(t_list *lst, t_token *token, int mode)
{ {
return (0); return (0);
} }
if (!access(next->value, mode)) if (mode >= 0 && access(next->value, mode) != 0)
{
error_msg("minishell", "unable to write to temp file");
return (0); return (0);
}
return (redirect_token_type(token) && next->type < 3); return (redirect_token_type(token) && next->type < 3);
} }
@ -43,4 +46,4 @@ int redirect_is_delimiter(t_token *token)
|| token->type == T_OR || token->type == T_OR
|| token->type == T_EOF || token->type == T_EOF
|| token->type == T_ERROR); || token->type == T_ERROR);
} }