From 38c400b79d834932d1222d0d4a978eb8b97879d9 Mon Sep 17 00:00:00 2001 From: arzelcm Date: Fri, 14 Jun 2024 15:42:58 +0200 Subject: [PATCH] Fix multiple bugs (minishell with redirections & no command argument redirections, >> cat) --- Makefile | 9 +++++++++ src/executor/execute_command.c | 2 ++ src/executor/executor.c | 2 +- src/minishell.c | 9 +++++++-- src/tokenizer/tokenizer.c | 2 ++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 1bb4d74..a824fc0 100644 --- a/Makefile +++ b/Makefile @@ -169,6 +169,15 @@ $(READLINE_DIR): libft_fclean \ b \ +test: + rm -rf 42_minishell_tester-master + rm -rf $(HOME)/42_minishell_tester + curl -sLO https://github.com/zstenger93/42_minishell_tester/archive/refs/heads/master.zip + unzip master.zip > /dev/null + chmod 744 ./42_minishell_tester-master/install.sh + ./42_minishell_tester-master/install.sh + ./42_minishell_tester-master/tester.sh m + -include $(DEPS) -include $(MDEPS) -include $(BDEPS) diff --git a/src/executor/execute_command.c b/src/executor/execute_command.c index 5603f09..d26492d 100644 --- a/src/executor/execute_command.c +++ b/src/executor/execute_command.c @@ -71,6 +71,8 @@ void execute_command(t_pdata *pdata, t_token *token, t_context *context) clean_exit(pdata); redirect_fds(pdata->fds[READ_FD], pdata->fds[WRITE_FD]); close_pdata_fds(pdata); + if (!token->argc) + exit(EXIT_SUCCESS); if (is_builtin(token->args[0])) exit(execute_builtin(token->args[0], token, context)); if (!is_directory(token->args[0])) diff --git a/src/executor/executor.c b/src/executor/executor.c index ca51e3f..032bb37 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -74,7 +74,7 @@ void execute(t_token *token, t_context *context) return (free_pdata(&p_data)); } config_echoctl_terminal(ON); - if (token->type == CMD && is_builtin(token->args[0])) + if (token->type == CMD && token->argc && is_builtin(token->args[0])) execute_cmd_builtin(&p_data, token, context); else if (token->type == CMD || token->type == PIPE) execute_pipe(&p_data, token, context); diff --git a/src/minishell.c b/src/minishell.c index eaf44c8..6e899c3 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -14,6 +14,7 @@ #include "tokenizer.h" #include "environment.h" #include "environment_helper.h" +#include "unistd.h" int g_sigval; @@ -33,12 +34,16 @@ int main(int argc, char **argv, char **envp) ft_bzero(&context, sizeof(t_context)); ft_bzero(&token, sizeof(t_token *)); init_env(&context, envp); - ft_printf(CREDITS); + if (isatty(STDIN_FILENO)) + ft_printf(CREDITS); while (42) { config_echoctl_terminal(OFF); listen_signals(MAIN, MAIN); - line = readline(PROMPT); + if (isatty(STDIN_FILENO)) + line = readline(PROMPT); + else + line = get_next_line(STDIN_FILENO, 0); if (g_sigval == SIGINT) { context.err_code = 1; diff --git a/src/tokenizer/tokenizer.c b/src/tokenizer/tokenizer.c index 6e12959..613490a 100644 --- a/src/tokenizer/tokenizer.c +++ b/src/tokenizer/tokenizer.c @@ -68,6 +68,8 @@ void set_arg(char *line, int *i, t_token *token, t_context *context) j = 0; while (words[j]) push_arg(&token->args, words[j++], &token->argc); + free(words); + free(word); } }