working on fixing pipes

This commit is contained in:
mdev9
2024-04-01 21:39:33 +02:00
parent 7d1fba69a9
commit dc73084ede
12 changed files with 61 additions and 97 deletions

View File

@ -6,12 +6,28 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
/* Updated: 2024/04/01 13:58:58 by marde-vr ### ########.fr */
/* Updated: 2024/04/01 21:39:20 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void get_redirections(t_msh *msh)
{
if (first_is_in_type(msh))
{
get_in_type(msh, msh->cmds);
if (!g_return_code)
get_out_type(msh, msh->cmds);
}
else
{
get_out_type(msh, msh->cmds);
if (!g_return_code)
get_in_type(msh, msh->cmds);
}
}
void exec_command_bonus(t_msh *msh, char *cmd_str)
{
t_cmd *cmds;
@ -23,11 +39,8 @@ void exec_command_bonus(t_msh *msh, char *cmd_str)
{
if (cmds->cmd_type == CMD)
{
if (cmds->next->cmd_type == PIPE)
{
}
msh->cmds = parse_command(cmds->value, msh->env);
get_redirections(msh);
print_parsed_cmd(cmds);
print_parsed_token(msh->cmds);
exec_commands(msh);
@ -37,6 +50,7 @@ void exec_command_bonus(t_msh *msh, char *cmd_str)
if (cmds && cmds->cmd_type == CMD)
{
msh->cmds = parse_command(cmds->value, msh->env);
get_redirections(msh);
print_parsed_cmd(cmds);
print_parsed_token(msh->cmds);
exec_commands(msh);
@ -46,14 +60,17 @@ void exec_command_bonus(t_msh *msh, char *cmd_str)
int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
{
pid_t pid;
int fds[2];
if (i != cmd_count - 1)
{
if (pipe(msh->fds[i]) == -1)
if (pipe(fds) == -1)
{
perror("pipe");
ft_exit(msh, 1);
}
msh->in_fd = fds[0];
msh->out_fd = fds[1];
}
pid = fork();
if (pid == -1)
@ -75,21 +92,6 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
void exec_command(t_msh *msh, int i, int cmd_count)
{
g_return_code = 0;
msh->fds[i] = ft_calloc(2, sizeof(int *));
if (!msh->fds[i])
ft_exit(msh, 1);
if (first_is_in_type(msh))
{
get_in_type(msh, msh->cmds);
if (!g_return_code)
get_out_type(msh, msh->cmds);
}
else
{
get_out_type(msh, msh->cmds);
if (!g_return_code)
get_in_type(msh, msh->cmds);
}
if (!cmd_is_builtin(msh, msh->cmds->value))
get_cmd_path(msh);
exec(msh, get_cmd_args(msh), i, cmd_count);
@ -108,15 +110,6 @@ void end_execution(t_msh *msh, int cmd_count)
g_return_code = WEXITSTATUS(status);
if (WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT)
printf("Quit (core dumped)\n");
i = 0;
while (i < cmd_count)
{
free(msh->fds[i]);
msh->fds[i] = 0;
i++;
}
free(msh->fds);
msh->fds = 0;
free(msh->pids);
msh->pids = 0;
//signal(SIGINT, signal_handler_interactive); //enables ctrl-C
@ -130,13 +123,10 @@ void exec_commands(t_msh *msh)
int i;
i = -1;
if (!msh->cmds)
return ;
cmd_count = get_cmd_count(msh->cmds);
ft_printf("cmd_count: %d\n", cmd_count);
msh->fds = ft_calloc(cmd_count, sizeof(int **));
msh->pids = ft_calloc(cmd_count, sizeof(int *));
if (!msh->pids || !msh->fds)
if (!msh->pids)
ft_exit(msh, 1);
while (++i < cmd_count)
exec_command(msh, i, cmd_count);