Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need to pass the <code>end</code> argument, instead increment the pointer to your command array. You are passing the initial array to the execvp call so it tries to execute <code>ls</code> multiple times. Further, you need a break statement after setting the <code>listaCommand[cont]</code> to <code>NULL</code> because after the iteration <code>cont</code> is incremented. Also I think you need to protect the <code>execvp</code> call so that the parent does not call it after the processing is done. </p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;stdlib.h&gt; #include &lt;sys/wait.h&gt; #define fatal() exit(1) void pipes (char *listaCommand[], char **argv) { printf("pipes %s\n", listaCommand[0]); int cont = 0; for (;listaCommand[cont]; cont++) { if (listaCommand[cont][0] == '|'){ int pid2, status; int pipefd[2], ret; listaCommand[cont] = NULL; ret = pipe (pipefd); if (ret &lt; 0) fatal(); /* Now fork. */ pid2 = fork (); if (pid2 &lt;0) fatal (); if (pid2 &gt; 0) { printf ("P: waiting for child\n"); wait (&amp;status); close(STDIN_FILENO); dup(pipefd[0]); close(pipefd[0]); close(pipefd[1]); /*execvp (auxCommand[0], auxCommand);*/ pipes(listaCommand + cont + 1, argv); /*break;*/ } else { close (STDOUT_FILENO); dup (pipefd[1]); close (pipefd[1]); close (pipefd[0]); break; } } } if (listaCommand[0]) { execvp (listaCommand[0], listaCommand); printf ("%s: command not found.\n", listaCommand[0]); /* Exec failed. */ exit(EXIT_FAILURE); } } int main() { char *args[] = { "ls", "-al", "|", "sort", "|" , "tr", "[a-z]", "[A-Z]", 0 }; pipes(args, 0); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload