Skip to content

Commit

Permalink
fix: wrong redirections for builtins.
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-cskn committed Mar 29, 2024
1 parent 34b40b4 commit 67eb839
Showing 1 changed file with 46 additions and 36 deletions.
82 changes: 46 additions & 36 deletions src/execute/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* By: hamza <hamza@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/17 17:34:07 by facetint #+# #+# */
/* Updated: 2024/03/28 17:44:54 by facetint ### ########.fr */
/* Updated: 2024/03/29 23:07:57 by hamza ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,17 +15,41 @@
#include "../../includes/minishell.h"
#include "../../includes/env.h"
#include "../../memory-allocator/allocator.h"
#include <stdio.h>

int should_run_in_child(t_command *cmd)
{
return (cmd->prev || cmd->next || !isbuiltin(cmd->args[0]));
int result = cmd->prev || cmd->next || !isbuiltin(cmd->args[0]);
return (result);
}

void handle_command(int inp_fd, int out_fd, t_command *cmd, int *prev_p, int *next_p)
int get_input_fd(int *pipe, t_command *cmd)
{
if (cmd->input != STDIN_FILENO)
return (cmd->input);
if (pipe == NULL)
return (STDIN_FILENO);
return (pipe[0]);
}

int get_output_fd(int *pipe, t_command *cmd)
{
if (cmd->output != STDOUT_FILENO)
return (cmd->output);
if (pipe == NULL)
return (STDOUT_FILENO);
return (pipe[1]);
}

void handle_command(t_command *cmd, int *prev_p, int *next_p)
{
int pid;
char *path_cmd;
int inp_fd;
int out_fd;

inp_fd = get_input_fd(prev_p, cmd);
out_fd = get_output_fd(next_p, cmd);
if (inp_fd == -1 || out_fd == -1)
return ;
pid = 0;
Expand All @@ -38,6 +62,13 @@ void handle_command(int inp_fd, int out_fd, t_command *cmd, int *prev_p, int *ne
cmd->pid = pid;
return ;
}
if (isbuiltin(cmd->args[0]))
{
handle_builtin(cmd, (int[]){inp_fd, out_fd});
if (should_run_in_child(cmd))
exit(0);
return;
}
dup2_and_close(inp_fd, out_fd, prev_p, next_p);
path_cmd = find_path(cmd->args[0]);
if (!path_cmd)
Expand All @@ -46,47 +77,30 @@ void handle_command(int inp_fd, int out_fd, t_command *cmd, int *prev_p, int *ne
exit(127);
}

int get_input_fd(int *pipe, t_command *cmd)
void close_pipe(int *pipe)
{
if (cmd->input != STDIN_FILENO)
return (cmd->input);
if (pipe == NULL)
return (STDIN_FILENO);
return (pipe[0]);
}

int get_output_fd(int *pipe, t_command *cmd)
{
if (cmd->output != STDOUT_FILENO)
return (cmd->output);
if (pipe == NULL)
return (STDOUT_FILENO);
return (pipe[1]);
if (pipe)
{
close(pipe[0]);
close(pipe[1]);
}
}

void execute(t_command *cmds)
void execute(t_command *cur)
{
t_command *cur;
t_command *latest;
int exit_status;
int *prev_p;
int *next_p;

if (!cmds->next && isbuiltin(cmds->args[0]) && cmds->args[0])
return (handle_builtin (cmds, (int [])
{STDIN_FILENO, STDOUT_FILENO}));
*get_exit_status() = 0;

prev_p = NULL;
next_p = NULL;
cur = cmds;
latest = NULL;
while (cur)
{
if (prev_p)
{
close(prev_p[0]);
close(prev_p[1]);
}
close_pipe(prev_p);
prev_p = next_p;
if (cur->next)
{
Expand All @@ -97,15 +111,11 @@ void execute(t_command *cmds)
{
next_p = NULL;
}
handle_command(get_input_fd(prev_p, cur), get_output_fd(next_p, cur), cur, prev_p, next_p);
handle_command(cur, prev_p, next_p);
latest = cur;
cur = cur->next;
}
if (prev_p)
{
close(prev_p[0]);
close(prev_p[1]);
}
close_pipe(prev_p);
if (latest->pid == 0)
{
*get_exit_status() = 1;
Expand Down

0 comments on commit 67eb839

Please sign in to comment.