Skip to content

Commit

Permalink
Dimadem fix redirect (#38)
Browse files Browse the repository at this point in the history
* fix: child process

* fix:redirect

---------

Co-authored-by: Dmitry Demirkylych <dmdemirk@c1r4s1.42london.com>
  • Loading branch information
dimadem and Dmitry Demirkylych authored Nov 7, 2024
1 parent ca14098 commit 841dbf5
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ NAME = minishell

# Comands
COMPILER = cc
DFLAGS = -g3 -gdwarf-2
DFLAGS = -g3 -gdwarf-2 -fsanitize=address -fsanitize=undefined
CFLAGS = -Wall -Wextra -Werror $(DFLAGS) -g -O0
AR = ar rcs
RM = rm -rf
Expand Down
2 changes: 1 addition & 1 deletion src/builtins/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ int builtin_exit(t_ms_data *data)
ft_putendl_fd("exit", STDOUT_FILENO);
handle_exit(data, 0);
}
return (0);
return (EXIT_SUCCESS);
}
4 changes: 1 addition & 3 deletions src/execute/execute_child.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,9 @@ static int new_process(t_ms_data *data)
child_process(data);
close_fds(data->std_in, data->std_out);
waitpid(pid, &exit, 0);
data->exit_status = WEXITSTATUS(exit);
if (WIFSIGNALED(exit) && WTERMSIG(exit) == SIGQUIT)
ft_printf("\n");
set_shell_var_handler(data);
return (EXIT_SUCCESS);
return (WEXITSTATUS(exit));
}

static void handle_exec_errors(char *exec_path, t_ms_data *data)
Expand Down
1 change: 1 addition & 0 deletions src/exit_status/exit_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void set_shell_var_handler(t_ms_data *data)
{
char *tmp_var;

tmp_var = NULL;
tmp_var = ft_itoa(data->exit_status);
set_shell_var(&data->shell_variables, "?", tmp_var);
free(tmp_var);
Expand Down
18 changes: 11 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,41 @@
#include "exit_status.h"
#include "builtins.h"

static int status_handler(int status, \
int status_handler(int status, \
t_loop_data *loop_data, t_token *token_head);
static void process_ast_and_io(t_ms_data *data, \
void process_ast_and_io(t_ms_data *data, \
t_loop_data *loop_data, t_token *tokens_head);
static void main_loop(t_ms_data *data, t_loop_data *loop_data);
void main_loop(t_ms_data *data, t_loop_data *loop_data);
int main(int argc, char **argv, char **envp);

static int status_handler(int status, \
int status_handler(int status, \
t_loop_data *loop_data, t_token *tokens_head)
{
if (status == WAIT_NEXT_COMMAND)
if (status == EXIT_FAILURE)
{
loop_cleanup(loop_data, tokens_head);
return (0);
}
return (1);
}

static void process_ast_and_io(t_ms_data *data, \
void process_ast_and_io(t_ms_data *data, \
t_loop_data *loop_data, t_token *tokens_head)
{
int status;

status = execute_ast(loop_data->tree, data);
printf("execute ast exit status -> %d\n", status);
data->exit_status = status;
set_shell_var_handler(data);
if (status_handler(status, loop_data, tokens_head))
{
handle_io_fd(data);
loop_cleanup(loop_data, tokens_head);
}
}

static void main_loop(t_ms_data *data, t_loop_data *loop_data)
void main_loop(t_ms_data *data, t_loop_data *loop_data)
{
t_token *tokens_start;

Expand All @@ -70,6 +73,7 @@ static void main_loop(t_ms_data *data, t_loop_data *loop_data)
loop_data->tokens = tokenise(loop_data->trimmed_input);
tokens_start = loop_data->tokens;
loop_data->tree = parse_tokens(&loop_data->tokens, data);
print_ast_root(loop_data->tree);
process_ast_and_io(data, loop_data, tokens_start);
}
clear_history_file();
Expand Down
24 changes: 16 additions & 8 deletions src/pipe/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ int builtin_pipe(t_ast *node, t_ms_data *data)
int fd[2];
pid_t pid_1;
pid_t pid_2;
int status;
int status_1;
int status_2;

status_1 = 0;
status_2 = 0;
pid_2 = -1;
if (pipe(fd) == -1)
ft_perror("pipe");
Expand All @@ -52,11 +55,15 @@ int builtin_pipe(t_ast *node, t_ms_data *data)
return (WAIT_NEXT_COMMAND);
}
close_fds(fd[0], fd[1]);
if (pid_1 > 0)
waitpid(pid_1, &status, 0);
if (node->right != NULL && pid_2 > 0)
waitpid(pid_2, &status, 0);
return (WEXITSTATUS(status));
if (pid_1 > 0 && waitpid(pid_1, &status_1, 0) == -1)
return (ft_perror("waitpid"));
if (pid_2 > 0)
{
if (waitpid(pid_2, &status_2, 0) == -1)
return (ft_perror("waitpid"));
return (WEXITSTATUS(status_2));
}
return (WEXITSTATUS(status_1));
}

/**
Expand All @@ -72,6 +79,7 @@ pid_t execute_child(t_ast *node, t_ms_data *data, \
int fd[2], int direction)
{
pid_t pid;
int status;

pid = fork();
if (pid == -1)
Expand All @@ -83,8 +91,8 @@ pid_t execute_child(t_ast *node, t_ms_data *data, \
else
dup2(fd[0], STDIN_FILENO);
close_fds(fd[0], fd[1]);
execute_ast(node, data);
exit(EXIT_SUCCESS);
status = execute_ast(node, data);
\ exit(status);
}
return (pid);
}
4 changes: 2 additions & 2 deletions src/redirection/redirect_heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static int handle_heredoc_interruption(char *line, char *eof, int file_fd, \
unlink("/tmp/heredoc");
g_heredoc_interrupted = 0;
sigaction(SIGINT, sa_old, NULL);
return (1);
return (EXIT_FAILURE);
}

static void execute_child(t_ast *node, t_ms_data *data, int *file_fd)
Expand Down Expand Up @@ -110,5 +110,5 @@ int redirect_here_doc(t_ast *node, t_ms_data *data)
file_fd = open_tmp_file("r");
execute_child(node->left, data, &file_fd);
unlink("/tmp/heredoc");
return (0);
return (EXIT_SUCCESS);
}
41 changes: 28 additions & 13 deletions src/redirection/redirect_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@

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

/*
! FIX
wc < input.txt - work fine
wc -l < input.txt - not work (:bad address error)
*/

/**
- @brief redirection input in context of executing AST
-
Expand All @@ -38,18 +32,39 @@ int redirect_in(t_ast *node, t_ms_data *data);
int redirect_in(t_ast *node, t_ms_data *data)
{
pid_t pid;
int status;
int local_fd;

pid = fork();
if (pid == -1)
return (1);
return (EXIT_FAILURE);
if (pid == 0)
{
data->std_in = open_file(node->right, "<");
if (data->std_in == -1)
return (1);
execute_ast(node->left, data);
exit(0);
{
data->std_in = open_file(node->right, "<");
if (data->std_in == -1)
return (EXIT_FAILURE);
}
else
{
local_fd = open_file(node->right, "<");
if (local_fd == -1)
return (EXIT_FAILURE);
dup2(local_fd, STDIN_FILENO);
close(local_fd);
}
if (!node->left->args[0])
{
close(data->std_in);
exit(EXIT_SUCCESS);
}
else
{
status = execute_ast(node->left, data);
exit(status);
}
}
waitpid(pid, &data->exit_status, 0);
return (0);
waitpid(pid, &status, 0);
return (WEXITSTATUS(status));
}
22 changes: 13 additions & 9 deletions src/redirection/redirect_out.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,38 @@ static int open_and_redirect(t_ast *node, t_ms_data *data)
{
data->std_out = open_file(node->right, ">");
if (data->std_out == -1)
return (1);
return (EXIT_FAILURE);
}
else
{
fd = open_file(node->right, ">");
if (fd == -1)
return (1);
return (EXIT_FAILURE);
dup2(fd, STDOUT_FILENO);
close(fd);
}
return (0);
return (EXIT_SUCCESS);
}

int redirect_out(t_ast *node, t_ms_data *data)
{
pid_t pid;
int status;
int exec_status;
int status;

pid = fork();
if (pid == -1)
return (1);
return (EXIT_FAILURE);
if (pid == 0)
{
if (open_and_redirect(node, data) != 0)
exit(1);
execute_ast(node->left, data);
exit(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 841dbf5

Please sign in to comment.