pipes working without valgrind errors :)
This commit is contained in:
59
srcs/exec.c
59
srcs/exec.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/21 23:25:10 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/22 13:50:06 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,6 +14,8 @@
|
|||||||
|
|
||||||
int exec_builtin(t_msh *msh)
|
int exec_builtin(t_msh *msh)
|
||||||
{
|
{
|
||||||
|
if (!msh->cmds->token)
|
||||||
|
return (STDIN_FILENO);
|
||||||
if (!ft_strcmp(msh->cmds->token, "echo"))
|
if (!ft_strcmp(msh->cmds->token, "echo"))
|
||||||
g_return_code = echo(msh->cmds->next);
|
g_return_code = echo(msh->cmds->next);
|
||||||
else if (!ft_strcmp(msh->cmds->token, "ret"))
|
else if (!ft_strcmp(msh->cmds->token, "ret"))
|
||||||
@ -74,7 +76,7 @@ int get_args_count(t_cmd *cmds)
|
|||||||
return (count);
|
return (count);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
void redirect_input(t_msh *msh)
|
void redirect_input(t_msh *msh)
|
||||||
{
|
{
|
||||||
if (dup2(msh->fds[0], 0) < 0)
|
if (dup2(msh->fds[0], 0) < 0)
|
||||||
@ -87,15 +89,19 @@ void redirect_output(t_msh *msh)
|
|||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
//close(msh->fds[1]);
|
//close(msh->fds[1]);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
void pipe_child(t_msh *msh, char **cmd_args)
|
void pipe_child(t_msh *msh, char **cmd_args)
|
||||||
{
|
{
|
||||||
//redirect_input(msh);
|
if (msh->cmds->type != ARG)
|
||||||
//redirect_output(msh);
|
{
|
||||||
|
redirect_input(msh);
|
||||||
//close(msh->fds[0]);
|
redirect_output(msh);
|
||||||
//close(msh->fds[1]);
|
close(msh->fds[0]);
|
||||||
|
close(msh->fds[1]);
|
||||||
|
}
|
||||||
|
if (exec_builtin(msh))
|
||||||
|
return ;
|
||||||
if (msh->cmds->token)
|
if (msh->cmds->token)
|
||||||
execve(msh->cmds->token, cmd_args, env_to_char_tab(msh->env));
|
execve(msh->cmds->token, cmd_args, env_to_char_tab(msh->env));
|
||||||
close(0);
|
close(0);
|
||||||
@ -135,8 +141,12 @@ int exec(t_msh *msh, t_cmd *cmd, char **cmd_args)
|
|||||||
{
|
{
|
||||||
pipe_parent(msh);
|
pipe_parent(msh);
|
||||||
rl_redisplay();
|
rl_redisplay();
|
||||||
if (waitpid(pid, 0, 0) < 0)
|
//if (waitpid(pid, 0, 0) < 0)
|
||||||
ft_exit(msh, 1);
|
// ft_exit(msh, 1);
|
||||||
|
int i = 0;
|
||||||
|
while (msh->pids[i])
|
||||||
|
i++;
|
||||||
|
msh->pids[i] = pid;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
@ -187,19 +197,21 @@ int remove_command_from_msh(t_msh *msh)
|
|||||||
t_cmd *cmd_tmp;
|
t_cmd *cmd_tmp;
|
||||||
|
|
||||||
cmd_cur = msh->cmds;
|
cmd_cur = msh->cmds;
|
||||||
while (cmd_cur)
|
while (cmd_cur && cmd_cur->next)
|
||||||
{
|
{
|
||||||
if (cmd_cur->type != ARG)
|
if (cmd_cur->type != ARG)
|
||||||
{
|
{
|
||||||
cmd_tmp = cmd_cur;
|
cmd_tmp = cmd_cur;
|
||||||
cmd_cur = cmd_cur->next;
|
cmd_cur = cmd_cur->next;
|
||||||
free_cmd(cmd_tmp);
|
free(cmd_tmp->token);
|
||||||
|
free(cmd_tmp);
|
||||||
msh->cmds = cmd_cur;
|
msh->cmds = cmd_cur;
|
||||||
return (cmd_cur->type);
|
return (cmd_cur->type);
|
||||||
}
|
}
|
||||||
cmd_tmp = cmd_cur;
|
cmd_tmp = cmd_cur;
|
||||||
cmd_cur = cmd_cur->next;
|
cmd_cur = cmd_cur->next;
|
||||||
free_cmd(cmd_tmp);
|
free(cmd_tmp->token);
|
||||||
|
free(cmd_tmp);
|
||||||
msh->cmds = cmd_cur;
|
msh->cmds = cmd_cur;
|
||||||
}
|
}
|
||||||
return (ARG);
|
return (ARG);
|
||||||
@ -212,17 +224,32 @@ void exec_command(t_msh *msh)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (!msh->cmds || exec_builtin(msh))
|
if (!msh->cmds)
|
||||||
return ;
|
return ;
|
||||||
msh->fds = ft_calloc(2, sizeof(int *));
|
msh->fds = ft_calloc(2, sizeof(int *));
|
||||||
msh->fds[1] = 1;
|
|
||||||
type = ARG;
|
type = ARG;
|
||||||
cmd_count = get_cmd_count(msh->cmds);
|
cmd_count = get_cmd_count(msh->cmds);
|
||||||
|
msh->pids = ft_calloc(cmd_count, sizeof(int *));
|
||||||
|
if (!msh->pids)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
if (cmd_count == 1)
|
||||||
|
msh->fds[1] = 1;
|
||||||
while (i < cmd_count)
|
while (i < cmd_count)
|
||||||
{
|
{
|
||||||
|
//if (type = PIPE)
|
||||||
|
|
||||||
get_cmd_path(msh);
|
get_cmd_path(msh);
|
||||||
exec(msh, msh->cmds, get_cmd_args(msh));
|
exec(msh, msh->cmds, get_cmd_args(msh));
|
||||||
type = remove_command_from_msh(msh);
|
//type = remove_command_from_msh(msh);
|
||||||
|
remove_command_from_msh(msh);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i = cmd_count;
|
||||||
|
while (i > 0)
|
||||||
|
{
|
||||||
|
if (waitpid(msh->pids[i], 0, 0) < 0)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
i--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/21 21:48:01 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/22 13:41:57 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -58,6 +58,7 @@ typedef struct s_msh
|
|||||||
struct s_env *env;
|
struct s_env *env;
|
||||||
struct s_cmd *cmds;
|
struct s_cmd *cmds;
|
||||||
int *fds;
|
int *fds;
|
||||||
|
int *pids;
|
||||||
} t_msh;
|
} t_msh;
|
||||||
|
|
||||||
extern int g_return_code;
|
extern int g_return_code;
|
||||||
|
Reference in New Issue
Block a user