title: Using fork() and execlp to count lines
body: <p>I'm trying to read a file, count the lines of that file, and then present the result in the end. I got everything working out great the only problem is the execlp command, I have no idea how to work with.</p>
  
  <p>The code is the following:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt; 
  #include &lt;sys/stat.h&gt; 
  #include &lt;fcntl.h&gt; 
  #include &lt;unistd.h&gt; 
  #include &lt;stdlib.h&gt; 
  #define exit_on_error(s,m) if (s &lt; 0) { perror(m); exit(1); } 
  
  int main(int argc, char *argv[]){
      int i, status, total=0, F, n=0;
  
      for(i=1; i&lt;argc; i++){
          if(fork()==0){
              F=open(argv[i], O_RDONLY);
              exit_on_error(F,"Erro na abertura do ficheiro");
              if(fork()==0)
              {
                  wait();
  
                  // ?????????????                                  
  
                  execlp("wc", "wc", "-l", NULL);
              }
              close (F);
              return (0);
          }
          else{
              wait(&amp;status);
              total += status &gt;&gt; 8;
          }
      }
  
  
      for(i=1; i&lt;argc; i++){
          printf("PID=(%d), Ficheiro %s\n", getpid(), argv[i]);
      }
      for(i=1; i&lt;argc; i++){
          printf("Numero de linhas do ficheiro %s: [%d]\n", argv[i], n);
      }
      printf("Ficheiros: [%d]---Erros: [%d]\n", --argc, total);
  
  }
  </code></pre>
  
  <hr>
  
  <p>And it shows something like this:
  name of the program is 2.c</p>
  
  <p>./2 phonecontacts.txt ddd</p>
  
  <p>Erro na abertura do ficheiro: No such file or directory</p>
  
  <p>PID=(3565), Ficheiro phonecontact.txt (File containing 20 lines)                               </p>
  
  <p>PID=(3565), Ficheiro ddd (this one doesn't exist)</p>
  
  <p>Numero de linhas do ficheiro phonecontact.txt: [0] (it was supposed to show 20 lines here)</p>
  
  <p>Numero de linhas do ficheiro ddd: [0] (this one doesn't exist)</p>
  
  <p>Ficheiros: [2]---Erros[1]</p>
  
  <p>wc: standard input: Input/output error 0</p>