un pipe fonctionne pour sleep et ls, code tres incomplet et brouillon dans main et readme
This commit is contained in:
@@ -59,6 +59,9 @@
|
|||||||
## 2. lexer (lexique analyser :
|
## 2. lexer (lexique analyser :
|
||||||
---
|
---
|
||||||
|
|
||||||
|
[wait for all childs to terminate](https://stackoverflow.com/questions/19461744/how-to-make-parent-wait-for-all-child-processes-to-finish)
|
||||||
|
[close pipes in child AND parents](https://stackoverflow.com/questions/33884291/pipes-dup2-and-exec)
|
||||||
|
|
||||||
### 2.1 methode arbre binaire :
|
### 2.1 methode arbre binaire :
|
||||||
|
|
||||||
[transformer arbre normal en arbre binaire](https://fr.wikipedia.org/wiki/Arbre_binaire#Transformation_d'un_arbre_quelconque_en_un_arbre_binaire)
|
[transformer arbre normal en arbre binaire](https://fr.wikipedia.org/wiki/Arbre_binaire#Transformation_d'un_arbre_quelconque_en_un_arbre_binaire)
|
||||||
|
|||||||
45
srcs/main.c
45
srcs/main.c
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/10/18 22:29:02 by hulamy ### ########.fr */
|
/* Updated: 2021/10/18 23:44:34 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -96,47 +96,54 @@ while (++i < pipes->nb_pipes)
|
|||||||
void execute_cmd(t_all *c, t_pipe *pipes)
|
void execute_cmd(t_all *c, t_pipe *pipes)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
pid_t wpid;
|
||||||
|
int status;
|
||||||
int i;
|
int i;
|
||||||
int stdout_bak;
|
int stdout_bak;
|
||||||
|
|
||||||
|
status = 0;
|
||||||
if (pipes->nb_pipes)
|
if (pipes->nb_pipes)
|
||||||
stdout_bak = dup(STDOUT_FILENO);
|
stdout_bak = dup(STDOUT_FILENO);
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i <= pipes->nb_pipes)
|
while (i <= pipes->nb_pipes)
|
||||||
{
|
{
|
||||||
if (pipes->nb_pipes)
|
|
||||||
{
|
|
||||||
printf("%i\n", stdout_bak);
|
|
||||||
if (i == 0)
|
|
||||||
{
|
|
||||||
dup2(pipes->pipes_fd[0][1], STDOUT_FILENO);
|
|
||||||
}
|
|
||||||
if (i == 1)
|
|
||||||
{
|
|
||||||
dup2(pipes->pipes_fd[0][0], STDIN_FILENO);
|
|
||||||
dup2(stdout_bak, STDOUT_FILENO);
|
|
||||||
}
|
|
||||||
close(pipes->pipes_fd[0][0]);
|
|
||||||
close(pipes->pipes_fd[0][1]);
|
|
||||||
close(stdout_bak);
|
|
||||||
}
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == 0) // child
|
if (pid == 0) // child
|
||||||
{
|
{
|
||||||
|
if (pipes->nb_pipes)
|
||||||
|
{
|
||||||
|
if (i == 0)
|
||||||
|
{
|
||||||
|
dup2(pipes->pipes_fd[0][1], STDOUT_FILENO);
|
||||||
|
}
|
||||||
|
if (i == 1)
|
||||||
|
{
|
||||||
|
dup2(pipes->pipes_fd[0][0], STDIN_FILENO);
|
||||||
|
dup2(stdout_bak, STDOUT_FILENO);
|
||||||
|
}
|
||||||
|
close(pipes->pipes_fd[0][0]);
|
||||||
|
close(pipes->pipes_fd[0][1]);
|
||||||
|
close(stdout_bak);
|
||||||
|
}
|
||||||
if (!ft_strncmp(pipes->input_split[i], "sleep ", 6))
|
if (!ft_strncmp(pipes->input_split[i], "sleep ", 6))
|
||||||
execve("/bin/sleep", ft_split(pipes->input_split[i], ' '), c->envp);
|
execve("/bin/sleep", ft_split(pipes->input_split[i], ' '), c->envp);
|
||||||
if (!ft_strncmp(pipes->input_split[i], "ls ", 3))
|
if (!ft_strncmp(pipes->input_split[i], "ls ", 3))
|
||||||
{
|
{
|
||||||
write(1, "\na\n\n", 4);
|
|
||||||
execve("/bin/ls", ft_split(pipes->input_split[i], ' '), c->envp);
|
execve("/bin/ls", ft_split(pipes->input_split[i], ' '), c->envp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (pid > 0) // parent
|
if (pid > 0) // parent
|
||||||
{
|
{
|
||||||
wait(0);
|
if (pipes->nb_pipes)
|
||||||
|
{
|
||||||
|
close(pipes->pipes_fd[0][0]);
|
||||||
|
close(pipes->pipes_fd[0][1]);
|
||||||
|
close(stdout_bak);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
while ((wpid = wait(&status)) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void shell_loop(t_all *c)
|
void shell_loop(t_all *c)
|
||||||
|
|||||||
Reference in New Issue
Block a user