title: Writing C Shell with combination pipes and redirection
body: <p>I am trying to implement a mini shell in C that will accept any combo of &lt;,>,>>,|,&amp;. I have it to where It reads commands and executes file redirection etc, however, I cannot seem to find a good tutorial through Google on how to program it to accept any combo of the above commands. The code I have so far is as follows (I deleted what Ive tried for the combination stuff bc I had no clue how to do it and it was a mess) (<strong>Some of the non-standard functions are from a library I am using.</strong>):</p>
  
  <pre><code>int main(int argc, char **argv)
  {
  int i,j,frv,status,x,fd1;
  IS is;
  is = new_inputstruct(NULL);
  
  /* read each line of input */
  while (get_line(is) &gt;= 0)
  {
      if (is-&gt;NF != 0)
      {
  
          /*fork off a new process to execute the command specified */
          frv = fork();
          /* if this is the child process, execute the desired command */
          if (frv == 0)
          {
              if (strcmp(is-&gt;fields[is-&gt;NF-1],"&amp;") == 0) is-&gt;fields[is-&gt;NF-1] = NULL;
              while (is-&gt;fields[frv] != NULL)
              {
                  /* if we see &lt;, make the next file standard input */
                  if (strcmp(is-&gt;fields[frv],"&lt;") == 0)
                  {
                      is-&gt;fields[frv] = NULL;
                      fd1 = open(is-&gt;fields[frv+1], O_RDONLY);
                      dup2(fd1, 0);
                      close(fd1);
  
                  }
                  /* if we see &gt;, make the next file standard output and create/truncate it if needed */
                  else if (strcmp(is-&gt;fields[frv],"&gt;") == 0)
                  {
                      is-&gt;fields[frv] = NULL;
                      fd1 = open(is-&gt;fields[frv+1],O_TRUNC | O_WRONLY | O_CREAT, 0666);
                      dup2(fd1, 1);
                      close(fd1);
  
                  }
                  /* if we see &gt;&gt;, make the next file standard output and create/append it if needed */
                  else if (strcmp(is-&gt;fields[frv],"&gt;&gt;") == 0)
                  {
                      is-&gt;fields[frv] = NULL;
                      fd1 = open(is-&gt;fields[frv+1], O_WRONLY | O_APPEND | O_CREAT, 0666);
                      dup2(fd1, 1);
                      close(fd1);
  
                  }
                  frv++;
              }
              /* execute the command */
              status = execvp(is-&gt;fields[0], is-&gt;fields);
              if (status == -1)
              {
                  perror(is-&gt;fields[0]);
                  exit(1);
              }
          }
          /* if this is parent process, check if need to wait or not */
          else
          {
              if (strcmp(is-&gt;fields[is-&gt;NF-1],"&amp;") != 0) while(wait(&amp;status) != frv);
          }
          /* clean up the values */
          for(j = 0; j &lt; is-&gt;NF; j++) is-&gt;fields[j] = NULL;
      }
  }
  return 0;
  }
  </code></pre>