diff --git a/e b/e deleted file mode 100644 index 3a964a04..00000000 --- a/e +++ /dev/null @@ -1,2 +0,0 @@ -pepe -dem diff --git a/minishell b/minishell deleted file mode 100755 index 2873df6a..00000000 Binary files a/minishell and /dev/null differ diff --git a/output.txt b/output.txt deleted file mode 100644 index d33653cc..00000000 --- a/output.txt +++ /dev/null @@ -1 +0,0 @@ -2 2 9 diff --git a/src/builtins/echo.c b/src/builtins/echo.c index eae4c965..9adcadbf 100644 --- a/src/builtins/echo.c +++ b/src/builtins/echo.c @@ -6,38 +6,45 @@ /* By: dmdemirk args[1] && (ft_strcmp(data->args[1], "-n") == 0)) + i = 1; + args = data->args; + if (!data || !data->args || !data->args[0]) + return (EXIT_FAILURE); + handle_std_io(&data->std_out, STDOUT_FILENO); + if (args[1] && ft_strcmp(args[1], "-n") == 0) { newline = 0; - data->args++; + i = 2; } - i = 0; - while (data->args[++i]) + while (args[i]) { - ft_putstr_fd(data->args[i], STDOUT_FILENO); - if (data->args[i + 1]) + ft_putstr_fd(args[i], STDOUT_FILENO); + if (args[i + 1]) ft_putstr_fd(" ", STDOUT_FILENO); + i++; } if (newline) - write(STDOUT_FILENO, "\n", 1); + ft_putchar_fd('\n', STDOUT_FILENO); return (EXIT_SUCCESS); } diff --git a/src/redirection/redirect_append.c b/src/redirection/redirect_append.c index 44ac1eab..febbf021 100644 --- a/src/redirection/redirect_append.c +++ b/src/redirection/redirect_append.c @@ -5,45 +5,58 @@ /* +:+ +:+ +:+ */ /* By: dmdemirk -int redirect_append(t_ast *node, t_ms_data *data); +static int open_and_redirect(t_ast *node, t_ms_data *data) +{ + int fd; -/** - - @brief redirect append ">>" to the end of the file output - - - - @param node current node in the AST - - @param data minishell data structure - - @return status: - - 0: success - - 1: error - */ + if (data->std_out == -1) + { + data->std_out = open_file(node->right, ">>"); + if (data->std_out == -1) + return (EXIT_FAILURE); + } + else + { + fd = open_file(node->right, ">>"); + if (fd == -1) + return (EXIT_FAILURE); + dup2(fd, STDOUT_FILENO); + close(fd); + } + return (EXIT_SUCCESS); +} int redirect_append(t_ast *node, t_ms_data *data) { pid_t pid; + int exec_status; int status; pid = fork(); if (pid == -1) - return (1); + return (EXIT_FAILURE); if (pid == 0) { - data->std_out = open_file(node->right, ">>"); - if (data->std_out == -1) - return (1); - execute_ast(node->left, data); - exit(0); + if (open_and_redirect(node, data) != 0) + exit(EXIT_FAILURE); + exec_status = execute_ast(node->left, data); + exit(exec_status); } - waitpid(pid, &status, 0); + if (waitpid(pid, &status, 0) == -1) + return (EXIT_FAILURE); + if (WIFSIGNALED(status)) + return (128 + WTERMSIG(status)); return (WEXITSTATUS(status)); }