You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

95 lines
2.6 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hroussea <hroussea@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/06 18:04:33 by hroussea #+# #+# */
/* Updated: 2021/06/10 20:43:07 by hroussea ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <pipex/pipex.h>
#include <libft/string.h>
#include <sys/wait.h>
void pipex_exec_cmd1(t_ppipex ppxi, t_pipe pipeobj, char **envp)
{
char buf[1024];
int i;
i = 0;
dup2(ppxi->cmd1.fd, 0);
dup2(pipeobj.stc.write_fd, 1);
close(pipeobj.stc.read_fd);
close(pipeobj.stc.write_fd);
close(ppxi->cmd1.fd);
close(ppxi->cmd2.fd);
ft_sprintf(buf, "%s", ppxi->cmd1.command[0]);
while (ppxi->path[i++])
{
execve(buf, &ppxi->cmd1.command[0], envp);
ft_sprintf(buf, "%s/%s", ppxi->path[i - 1], ppxi->cmd1.command[0]);
}
perror(ppxi->cmd1.command[0]);
}
void pipex_exec_cmd2(t_ppipex ppxi, t_pipe pipeobj, char **envp)
{
char buf[1024];
int i;
i = 0;
dup2(pipeobj.stc.read_fd, 0);
dup2(ppxi->cmd2.fd, 1);
close(pipeobj.stc.read_fd);
close(pipeobj.stc.write_fd);
close(ppxi->cmd1.fd);
close(ppxi->cmd2.fd);
ft_sprintf(buf, "%s", ppxi->cmd2.command[0]);
while (ppxi->path[i++])
{
execve(buf, &ppxi->cmd2.command[0], envp);
ft_sprintf(buf, "%s/%s", ppxi->path[i - 1], ppxi->cmd2.command[0]);
}
perror(ppxi->cmd2.command[0]);
}
int create_pipes(t_pipe *pipeobj)
{
if (pipe(pipeobj->tab) < 0)
{
perror("pipe");
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
int pipex_kill(t_ppipex ppxi)
{
pipex_despawn(ppxi);
return (EXIT_FAILURE);
}
int main(int argc, char **argv, char **envp)
{
t_ppipex ppxi;
t_pipe pipeobj;
int status;
ppxi = pipex_spawn();
if (pipex_parse(ppxi, argc, argv, envp))
return (pipex_kill(ppxi));
if (create_pipes(&pipeobj) != EXIT_SUCCESS)
return (pipex_kill(ppxi));
if (fork() == 0)
pipex_exec_cmd1(ppxi, pipeobj, envp);
else if (fork() == 0)
pipex_exec_cmd2(ppxi, pipeobj, envp);
close(pipeobj.stc.read_fd);
close(pipeobj.stc.write_fd);
wait(&status);
wait(&status);
return (!pipex_kill(ppxi));
}