title: how to exec() in a for loop? in C
body: <p>I'm trying to implement the cat command invoking the exec() function and saving the output of it in a tmp file. My issue is that I know that after invoking exec() anything afterwards will be ignore, therefore there's no point of looping the exec().</p>
  
  <p>If I have N number of arguments to pass to the main program, how can I loop exec() in order to read all arguments?</p>
  
  <p>Note: Using system() is not a choice for me, that's how the assignment is.</p>
  
  <p>Right now I have the following code in a not very elegant way:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/times.h&gt;
  #include &lt;sys/wait.h&gt;
  
  int main( int argc,char *argv[] )
  {
      int fd;
      char filename[] = "tmp.txt";
  
      fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
      dup2(fd, 1);   // make stdout go to file
      dup2(fd, 2);   // make stderr go to file                
      close(fd);
  
      execl("/bin/cat", argv[0], argv[1], argv[2], argv[3], NULL);
  
      return(0);
  }
  </code></pre>