Skip to content

Commit

Permalink
fix: echo "new line" >> output.txt > new.txt > last.txt (#39)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Demirkylych <dmdemirk@c1r1s5.42london.com>
  • Loading branch information
dimadem and Dmitry Demirkylych authored Nov 8, 2024
1 parent ffee921 commit df5c117
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 36 deletions.
2 changes: 0 additions & 2 deletions e

This file was deleted.

Binary file removed minishell
Binary file not shown.
1 change: 0 additions & 1 deletion output.txt

This file was deleted.

33 changes: 20 additions & 13 deletions src/builtins/echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,45 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:31:33 by dmdemirk #+# #+# */
/* Updated: 2024/08/28 14:20:26 by dmdemirk ### ########.fr */
/* Updated: 2024/11/08 15:15:40 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"
#include "shell.h"
#include "exit_status.h"
#include "execute.h"

/*
Functionality:
- Print the argument
*/
Functionality:
- Print the argument
*/

int builtin_echo(t_ms_data *data)
{
int newline;
int i;
int newline;
int i;
char **args;

newline = 1;
if (data->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);
}
53 changes: 33 additions & 20 deletions src/redirection/redirect_append.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,58 @@
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 14:32:11 by dmdemirk #+# #+# */
/* Updated: 2024/07/11 14:54:19 by dmdemirk ### ########.fr */
/* Created: 2024/11/08 15:13:15 by dmdemirk #+# #+# */
/* Updated: 2024/11/08 15:13:40 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#include "shell.h"
#include "tokens.h"
#include "shell.h"
#include "redirection.h"
#include "execute.h"
#include "pipe.h"
#include <sys/wait.h>

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));
}

0 comments on commit df5c117

Please sign in to comment.