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)); } diff --git a/src/redirection/redirect_in.c b/src/redirection/redirect_in.c index 4f0d6502..7a0b51a6 100644 --- a/src/redirection/redirect_in.c +++ b/src/redirection/redirect_in.c @@ -6,7 +6,7 @@ /* By: dmdemirk std_in == -1) - { - data->std_in = open_file(node->right, "<"); - if (data->std_in == -1) - return (EXIT_FAILURE); - } - else + *original_stdin = dup(STDIN_FILENO); + if (*original_stdin == -1) + return (-1); + fd = open_file(node->right, "<"); + if (fd == -1) { - local_fd = open_file(node->right, "<"); - if (local_fd == -1) - return (EXIT_FAILURE); - dup2(local_fd, STDIN_FILENO); - close(local_fd); + close(*original_stdin); + return (-1); } - if (!node->left->args[0]) + if (dup2(fd, STDIN_FILENO) == -1) { - close(data->std_in); - exit(EXIT_SUCCESS); + close(fd); + close(*original_stdin); + return (-1); } - status = execute_ast(node->left, data); - exit(status); + close(fd); + return (EXIT_SUCCESS); } int redirect_in(t_ast *node, t_ms_data *data) { - pid_t pid; - int status; + int status; + int original_stdin; - pid = fork(); - if (pid == -1) + if (setup_redirection(node, &original_stdin) == -1) return (EXIT_FAILURE); - if (pid == 0) - handle_child_process(node, data); - waitpid(pid, &status, 0); - return (WEXITSTATUS(status)); + if (!node->left->args[0]) + status = EXIT_SUCCESS; + else + status = execute_ast(node->left, data); + if (dup2(original_stdin, STDIN_FILENO) == -1) + status = EXIT_FAILURE; + close(original_stdin); + return (status); }