Skip to content

Commit

Permalink
Merge branch 'main' into roman_4pm_pre_merge
Browse files Browse the repository at this point in the history
  • Loading branch information
¨Roman committed Nov 8, 2024
2 parents 38ab296 + df5c117 commit b950657
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 62 deletions.
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));
}
56 changes: 27 additions & 29 deletions src/redirection/redirect_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 14:32:40 by dmdemirk #+# #+# */
/* Updated: 2024/11/07 19:40:50 by dmdemirk ### ########.fr */
/* Updated: 2024/11/08 13:14:21 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,44 +19,42 @@

int redirect_in(t_ast *node, t_ms_data *data);

static int handle_child_process(t_ast *node, t_ms_data *data)
static int setup_redirection(t_ast *node, int *original_stdin)
{
int local_fd;
int status;
int fd;

if (data->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);
}

0 comments on commit b950657

Please sign in to comment.