title: shell file redirection
body: <p>I'm writing a shell script for class in C and I got my program to execute all commands except for file redirection. Can someone help figure out how I need to go about file redirection, I know I need to do it when fork returns 0, but I'm not sure how to do it.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>CODE: 
  Here is an updated version of my code. Creates the file fine but then i'm not sure what to do after. I close the correct file descriptors I think then duplicate my file descriptor to the lowest open descriptor. after dup besides closing the correct file descriptor can you help me what to do?</p>
  
  <p>args[] is the array that i store the input (i.e. ls -l, ls -l > out)</p>
  
  <pre><code>if ((cpid = fork()) == 0)
  {
    for ( i = 0; i &lt; size; i++)
    {
      if ( *args[i] == '&lt;' )
      {
        if ((fd = open(args[i+1], O_RDONLY)) == -1)
        {
          perror("open");
          exit(2);
        }
        else
        {
          close(0);
          dup(fd);
          close(fd);
        }
      }
      if ( *args[i] == '&gt;' )
      {
        if ((fd = open(args[i+1], O_WRONLY | O_CREAT, 0644)) == -1)
        {
          perror("open");
          exit(2);
        }
        else
        {
          close(1);
          dup(fd);
          close(fd);
        }
      }
    }
  
    execve(path,args,environ);
    perror("execve");
    exit(2);
  }
  </code></pre>