fix: use the bash waitpid implementation

This commit is contained in:
Camille Chauvet 2023-03-08 18:19:10 +01:00
parent 5c9be2d56b
commit a18c4cae82
5 changed files with 21 additions and 16 deletions

2
cmd.c
View File

@ -12,6 +12,7 @@
#include "libftx/libftx.h"
#include "minishell.h"
#include <sys/wait.h>
void ft_cmddel(void *ptr)
{
@ -51,5 +52,6 @@ int ft_cmd_filler(t_data *data, t_list *element, char **args)
}
content->args = args;
content->executable = args[0];
content->pid = -1;
return (0);
}

4
cmds.c
View File

@ -66,9 +66,7 @@ static int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile
current = *cmds;
cmd = current->content;
cmd->fd_in = infile;
while (current->next != NULL)
current = current->next;
cmd = current->content;
cmd = ft_lstlast(*cmds)->content;
cmd->fd_out = outfile;
return (0);
}

View File

@ -12,6 +12,7 @@
#include "libftx/libftx.h"
#include "minishell.h"
#include <sys/wait.h>
#include <unistd.h>
static char *ft_get_executable_path(t_data *data, char *executable_name)
@ -71,23 +72,21 @@ static char *ft_get_executable_path(t_data *data, char *executable_name)
return (path);
}
static int ft_executor(t_data *data, t_cmd *cmd)
static void ft_executor(t_data *data, t_cmd *cmd)
{
int pid;
int return_value;
char **tab;
return_value = -1;
if (cmd->fd_in == -1 || cmd->fd_out == -1)
return (1);
return ;
pid = fork();
if (pid == -1)
return (1);
return ;
if (pid == 0)
{
tab = env_to_strs(data->env);
if (tab == NULL)
return (1);
return ;
dup2(cmd->fd_out, 1);
dup2(cmd->fd_in, 0);
if (cmd->fd_out > 2)
@ -96,10 +95,7 @@ static int ft_executor(t_data *data, t_cmd *cmd)
close(cmd->fd_in);
execve(cmd->executable, cmd->args, tab);
}
else
waitpid(pid, &return_value, 0);
data->exit_code = return_value;
return (return_value);
cmd->pid = pid;
}
static int ft_own_cmd(t_data *data, t_cmd *cmd)
@ -168,7 +164,7 @@ int ft_cmds_executor(t_data *data, t_list **cmds)
content->executable = ft_get_executable_path(data,
content->executable);
if (content->executable != NULL)
exit_code = ft_executor(data, content);
ft_executor(data, content);
}
else if (exit_code == -2)
return (1);
@ -180,5 +176,13 @@ int ft_cmds_executor(t_data *data, t_list **cmds)
close(content->fd_out);
current = current->next;
}
current = *cmds;
while (current != NULL)
{
content = current->content;
if (content->pid != -1)
waitpid(content->pid, &(data->exit_code), 0);
current = current->next;
}
return (0);
}

4
main.c
View File

@ -45,10 +45,10 @@ static int ft_minishell(t_data *data, char *line)
int outfile;
if (ft_syntatic_verif(data, line))
return (1);
return (0);
line_clean = ft_normalizer(line);
if (line_clean == NULL)
return (1);
return (0);
outfile = ft_outfile(data, line_clean);
if (outfile == -2)
{

View File

@ -91,6 +91,7 @@ typedef struct s_cmd
{
int fd_in;
int fd_out;
int pid;
char *executable;
char **args;
} t_cmd;