reput fds into arrays

This commit is contained in:
mdev9
2024-04-13 13:44:56 +02:00
parent 90e569f231
commit 6ea24e899f
5 changed files with 31 additions and 37 deletions

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
/* Updated: 2024/04/13 12:47:16 by tomoron ### ########.fr */ /* Updated: 2024/04/13 13:28:48 by babonnet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -78,9 +78,6 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
perror("minishell: pipe"); perror("minishell: pipe");
ft_exit(msh, 1); ft_exit(msh, 1);
} }
msh->in_fd = fds[0];
msh->out_fd = fds[1];
print_msh_struct(msh);
} }
pid = fork(); pid = fork();
if (pid == -1) if (pid == -1)
@ -102,6 +99,9 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
void exec_command(t_msh *msh, int i, int cmd_count) void exec_command(t_msh *msh, int i, int cmd_count)
{ {
g_return_code = 0; g_return_code = 0;
msh->fds[i] = ft_calloc(2, sizeof(int *));
if (!msh->fds[i])
ft_exit(msh, 1);
if (!cmd_is_builtin(msh, msh->tokens->value)) if (!cmd_is_builtin(msh, msh->tokens->value))
get_cmd_path(msh); get_cmd_path(msh);
exec(msh, get_cmd_args(msh), i, cmd_count); exec(msh, get_cmd_args(msh), i, cmd_count);
@ -137,6 +137,7 @@ void end_execution(t_msh *msh, int cmd_count)
// TODO: (core dumped) WCOREDUMP // TODO: (core dumped) WCOREDUMP
free(msh->pids); free(msh->pids);
msh->pids = 0; msh->pids = 0;
free(msh->fds);
// signal(SIGINT, signal_handler_interactive); //enables ctrl-C // signal(SIGINT, signal_handler_interactive); //enables ctrl-C
signal(SIGQUIT, signal_handler_interactive); signal(SIGQUIT, signal_handler_interactive);
set_echoctl(0); set_echoctl(0);
@ -160,6 +161,7 @@ void exec_commands(t_msh *msh)
while (i < cmd_count) while (i < cmd_count)
{ {
exec_command(msh, i, cmd_count); exec_command(msh, i, cmd_count);
free(msh->fds[i]);
i++; i++;
} }
end_execution(msh, cmd_count); end_execution(msh, cmd_count);

View File

@ -6,13 +6,13 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */ /* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */
/* Updated: 2024/04/13 12:54:23 by tomoron ### ########.fr */ /* Updated: 2024/04/13 13:42:07 by babonnet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void redirect_input(t_msh *msh) void redirect_input(t_msh *msh, int i)
{ {
if (msh->in_type != PIPE) if (msh->in_type != PIPE)
{ {
@ -22,14 +22,9 @@ void redirect_input(t_msh *msh)
} }
else else
{ {
printf("in_fd : %d\n", msh->in_fd); ft_printf_fd(2, "%d: 0 -> %d\n", i, msh->fds[i - 1][0]);
printf("cmd : %s\n", msh->cmds->value); if (dup2(msh->fds[i - 1][0], 0) < 0)
is_fd_open(msh->in_fd);
if (dup2(msh->in_fd, 0) < 0)
{
perror("dup2");
ft_exit(msh, 1); ft_exit(msh, 1);
}
} }
} }

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/04/13 12:56:29 by tomoron ### ########.fr */ /* Updated: 2024/04/13 13:31:40 by babonnet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -103,10 +103,10 @@ void remove_command_from_msh(t_msh *msh);
void ft_exit(t_msh *msh, int error_code); void ft_exit(t_msh *msh, int error_code);
void signal_handler_command(int signum); void signal_handler_command(int signum);
void ft_exit(t_msh *msh, int exit_code); void ft_exit(t_msh *msh, int exit_code);
void redirect_output(t_msh *msh); void redirect_output(t_msh *msh, int i);
char **split_paths_from_env(t_env *env); char **split_paths_from_env(t_env *env);
int add_return_code_to_str(char *res); int add_return_code_to_str(char *res);
void redirect_input(t_msh *msh); void redirect_input(t_msh *msh, int i);
void parse_var(t_msh *msh, char *line); void parse_var(t_msh *msh, char *line);
void print_parsed_token(t_token *cmd);//debug void print_parsed_token(t_token *cmd);//debug
int get_var_name_len(char *command); int get_var_name_len(char *command);

View File

@ -6,13 +6,13 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */ /* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */
/* Updated: 2024/04/07 17:34:58 by tomoron ### ########.fr */ /* Updated: 2024/04/13 13:42:29 by babonnet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void redirect_output(t_msh *msh) void redirect_output(t_msh *msh, int i)
{ {
if (msh->out_type != PIPE) if (msh->out_type != PIPE)
{ {
@ -21,7 +21,8 @@ void redirect_output(t_msh *msh)
} }
else else
{ {
if (dup2(msh->out_fd, 1) < 0) ft_printf_fd(2, "%d: 1 -> %d\n", i, msh->fds[i - 1][1]);
if (dup2(msh->fds[i][1], 1) < 0)
ft_exit(msh, 1); ft_exit(msh, 1);
} }
} }

View File

@ -6,25 +6,25 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */ /* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
/* Updated: 2024/04/13 13:21:52 by tomoron ### ########.fr */ /* Updated: 2024/04/13 13:39:31 by babonnet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void close_pipe_fds(t_msh *msh) void close_pipe_fds(t_msh *msh, int i)
{ {
if (msh->in_fd > 2) if (i != 0)
{ {
close(msh->in_fd); if (msh->fds[i - 1][0] > 2)
printf("close in"); close(msh->fds[i - 1][0]);
if (msh->fds[i - 1][1] > 2)
close(msh->fds[i - 1][1]);
} }
if (msh->out_fd > 2) if (msh->fds[i][0] > 2)
{ close(msh->fds[i][0]);
close(msh->out_fd); if (msh->fds[i][1] > 2)
printf("close out"); close(msh->fds[i][1]);
}
//double close
} }
void execute_command(t_msh *msh, char **cmd_args) void execute_command(t_msh *msh, char **cmd_args)
@ -52,11 +52,11 @@ void child(t_msh *msh, char **cmd_args, int i)
if ((msh->in_type != CMD && msh->in_type != PAREN && msh->in_type != AND if ((msh->in_type != CMD && msh->in_type != PAREN && msh->in_type != AND
&& msh->in_type != OR && msh->in_type != PIPE) && msh->in_type != OR && msh->in_type != PIPE)
|| (msh->in_type == PIPE && i > 0)) || (msh->in_type == PIPE && i > 0))
redirect_input(msh); redirect_input(msh, i);
if (msh->out_type == PIPE || msh->out_type == RED_O if (msh->out_type == PIPE || msh->out_type == RED_O
|| msh->out_type == RED_O_APP) || msh->out_type == RED_O_APP)
redirect_output(msh); redirect_output(msh, i);
close_pipe_fds(msh); close_pipe_fds(msh, i);
execute_command(msh, cmd_args); execute_command(msh, cmd_args);
close(0); close(0);
close(1); close(1);
@ -69,10 +69,6 @@ void parent(t_msh *msh, int i, int cmd_count)
{ {
//signal(SIGINT, signal_handler_command); //signal(SIGINT, signal_handler_command);
signal(SIGQUIT, signal_handler_command); signal(SIGQUIT, signal_handler_command);
(void)msh;
(void) i;
(void) cmd_count;
printf("i : %d\n", i);
if (i != 0) if (i != 0)
{ {
if (msh->fds[i - 1][0] > 2) if (msh->fds[i - 1][0] > 2)