Skip to content

Commit

Permalink
fix: redirect -> one thread running
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Demirkylych committed Nov 8, 2024
1 parent ff9a3be commit ffee921
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
2 changes: 2 additions & 0 deletions e
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pepe
dem
Binary file added minishell
Binary file not shown.
1 change: 1 addition & 0 deletions output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2 2 9
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 ffee921

Please sign in to comment.