Skip to content

Commit

Permalink
style: edit the norminette
Browse files Browse the repository at this point in the history
  • Loading branch information
facetint committed Mar 28, 2024
1 parent 627420f commit 76b5695
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/execute/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/17 17:34:07 by facetint #+# #+# */
/* Updated: 2024/03/28 17:15:40 by facetint ### ########.fr */
/* Updated: 2024/03/28 17:44:54 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,12 +16,12 @@
#include "../../includes/env.h"
#include "../../memory-allocator/allocator.h"

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

void handle_command(int inp_fd, int out_fd, t_command *cmd, int *prev_p, int *next_p)
void handle_command(int inp_fd, int out_fd, t_command *cmd, int *prev_p, int *next_p)
{
int pid;
char *path_cmd;
Expand All @@ -33,7 +33,7 @@ void handle_command(int inp_fd, int out_fd, t_command *cmd, int *prev_p, int *ne
pid = fork();
if (pid < 0)
pid_error(pid, prev_p, next_p);
if (pid > 0)
if (pid > 0)
{
cmd->pid = pid;
return ;
Expand All @@ -58,36 +58,43 @@ int get_input_fd(int *pipe, t_command *cmd)
int get_output_fd(int *pipe, t_command *cmd)
{
if (cmd->output != STDOUT_FILENO)
return cmd->output;
return (cmd->output);
if (pipe == NULL)
return STDOUT_FILENO;
return pipe[1];
return (STDOUT_FILENO);
return (pipe[1]);
}

void execute(t_command *cmds)
{
t_command *cur;
t_command *latest;
int exit_status;
int *prev_p = NULL;
int *next_p = NULL;
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}));
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) {
if (prev_p)
{
close(prev_p[0]);
close(prev_p[1]);
}
prev_p = next_p;
if (cur->next){
if (cur->next)
{
next_p = safe_malloc(sizeof(int) * 2);
pipe(next_p);
} else if (next_p) {
}
else if (next_p)
{
next_p = NULL;
}
handle_command(get_input_fd(prev_p, cur), get_output_fd(next_p, cur), cur, prev_p, next_p);
Expand All @@ -99,14 +106,13 @@ void execute(t_command *cmds)
close(prev_p[0]);
close(prev_p[1]);
}

if (latest->pid == 0)
{
*get_exit_status() = 1;
return;
return ;
}
waitpid(latest->pid, &exit_status, 0);
if (!*get_exit_status())
*get_exit_status() = exit_status >> 8;;
while(wait(NULL) > 0);
*get_exit_status() = exit_status >> 8;
while (wait(NULL) > 0);
}

0 comments on commit 76b5695

Please sign in to comment.