Q:

Segmentation fault C in writing a shell program

My code worked fine until I add the extra stuffs into it, like identifying and deal with cd,  >, <, >> and |.  Could you please check my code and see where did the error happens?  
Btw the requirement of the assignment is only up to 1 pipe.  And I think the problem starts somewhere from the for loop, cause I put printf right after it to check if it would print args[k], and it did but then the bug popped up and the program stopped. 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>

int main()
{
  const int MAX_ARGS = 10;
  char *token[MAX_ARGS + 1]; /*assume max number of token <=10*/
  char *temp;
  char line[256], command[MAX_ARGS];
  char prompt[] = "sh2 % ";
  pid_t pid;
  int i=0, j,k, status;

  printf("%s", prompt);

  while (fgets(line, sizeof line, stdin) ! = NULL)
  {
    line[strlen(line)-1] = '\0';    /*get rid of \n*/
    token[0] = strtok(line," ");
    strcpy(command,token[0]);    
    temp = strtok(NULL," ");
    while (temp ! = NULL)    
    {
      i = i+1;
      token[i] = temp;            
      temp = strtok(NULL," ");
    }
    char *args[i+2];
    for (j = 0; j < (i+1); j++) 
    {
      args[j] = token[j];  
    }
    args[i+1] = NULL;

    if (! strcmp("exit",command))
      exit(0);
    if (! strcmp("cd", command))
    {
      int success;
      if (success = chdir(args[1]) <0)
      {
        printf("Failed to change dir.\n");
      }
    }
    else 
    {
      int piping = 0;
      int fd;
      for (k = 0; k < sizeof args; k++)
      {         
        if (! strcmp(">",args[k])) 
        {   
          fd = open(args[k+1],O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR);
      if (fd <0) { printf("Open file failed.\n");}
          else 
      {
       dup2(fd, 1);
       args[k] = '\0';
       fflush(stdout);      
       close(fd);
      }
    }
    if (! strcmp("<", args[k]))
    {
      fd = open(args[k+1], O_RDONLY);
      if (fd <0) {printf("Open file failed.\n");}
      else
      {
        dup2(fd, 0);
        args[k] = '\0';
        close(fd);
      }
    }
    if (! strcmp(">>", args[k]))
    {
      fd = open(args[k+1], O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR);
      if (fd <0) {printf("Open file failed");}
      else
      {
            dup2(fd,1);
        args[k] = '\0';
        fflush(stdout);
        close(fd);
      }
    }
    if (! strcmp("|", args[k]))
    {
      piping = 1; 
        }
      } /*end for*/
      if (!( piping))
      {        
         pid = fork();
         if (pid <0) {}
         else if (pid == 0)
         {
           if ( (status = execvp(command, args)) < 0 )
       {
         printf("Command not found\n");
       }
         }
         else /*parent*/
         {
           wait(&status);          
         } /*end parent*/ 
      }/*end if not pipe*/
      else /*its pipe*/
      {
        int pfd[2];
    pipe(pfd);
    fd = fork();
    if (fd < 0) {}
    else if (fd ==0)
    {     
      close(pfd[1]);
      dup2(pfd[0],0);
      close(pfd[0]);
      execvp(args[2],args[2]);
    } 
    else /*parent*/
    {
      close(pfd[0]);
      dup2(pfd[1],1);
      close(pfd[1]);
      execvp(args[0],args[0]);
    }
      }/*end pipe*/
    } /*end outter else*/    

    printf("%s", prompt);

  }/*end  while*/
  return 0;
}  

A:

for (k = 0; k < sizeof args; k++)

This is not how you iterate through args: this will go far beyond the end of the array.  You want something like:
num = sizeof(args) / sizeof(*args);
for (k = 0; k < num; k++)

Alternatively, since you set the last element as NULL, you could do
for (char **arg = args; *arg; arg++)

Also note that you iterate with k until the end of the array and then use k + 1, which is very likely to cause problems.