This commit is contained in:
Etienne Rey-bethbeder 2023-03-09 15:08:15 +01:00
commit 86f01779cc
8 changed files with 23 additions and 19 deletions

Binary file not shown.

View File

@ -7,7 +7,7 @@ NAME = minishell
CC = clang CC = clang
CFLAGS = -Werror -Wextra -g CFLAGS = -Werror -Wextra -Wall -g
LIBS = libftx/libftx.a LIBS = libftx/libftx.a

2
cmd.c
View File

@ -12,6 +12,7 @@
#include "libftx/libftx.h" #include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
#include <sys/wait.h>
void ft_cmddel(void *ptr) 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->args = args;
content->executable = args[0]; content->executable = args[0];
content->pid = -1;
return (0); 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; current = *cmds;
cmd = current->content; cmd = current->content;
cmd->fd_in = infile; cmd->fd_in = infile;
while (current->next != NULL) cmd = ft_lstlast(*cmds)->content;
current = current->next;
cmd = current->content;
cmd->fd_out = outfile; cmd->fd_out = outfile;
return (0); return (0);
} }

View File

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

6
main.c
View File

@ -45,10 +45,10 @@ static int ft_minishell(t_data *data, char *line)
int outfile; int outfile;
if (ft_syntatic_verif(data, line)) if (ft_syntatic_verif(data, line))
return (1); return (0);
line_clean = ft_normalizer(line); line_clean = ft_normalizer(line);
if (line_clean == NULL) if (line_clean == NULL)
return (1); return (0);
outfile = ft_outfile(data, line_clean); outfile = ft_outfile(data, line_clean);
if (outfile == -2) if (outfile == -2)
{ {
@ -69,8 +69,6 @@ static int ft_minishell(t_data *data, char *line)
close(outfile); close(outfile);
if (infile > 2) if (infile > 2)
close(infile); close(infile);
ft_lstclear(cmds, ft_cmddel);
free(cmds);
free(line_clean); free(line_clean);
return (1); return (1);
} }

View File

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

View File

@ -69,7 +69,7 @@ static int ft_get_outfile(t_data *data, const char *line)
ft_eprintf("minishell: malloc failed\n"); ft_eprintf("minishell: malloc failed\n");
return (-2); return (-2);
} }
fd = 0; fd = 1;
i = 0; i = 0;
while (tab[i + 1] != NULL) while (tab[i + 1] != NULL)
{ {