title: Creating a shell
body: <p>When i run the program with the <code>ls &gt; filename</code>  command it prints the results in the file but after that it doesn't respond to other commands like <code>ls</code> etc . Here is the code :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  
  void execute(char **argv, char ** k, int *l)
  {
      pid_t pid;
      int file_descriptor;
      int status;
  
      int file;
  
      if ((pid = fork()) &lt; 0)
      {
          printf("*** ERROR: forking child process failed\n");
          exit(1);
      }
      else if (pid == 0)
      {
  
          if (l = 1)
          {
              file = open(k, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
              file_descriptor = dup2(file, 1);
  
          }
  
          if (execvp(argv[0], argv) &lt; 0)
          {
              printf("*** ERROR: exec failed\n");
              exit(1);
          }
  
      }
      else
      {
  
          while (wait(&amp;status) != pid)
              ;
  
      }
  }
  
  void main(void)
  {
      char **name = (char**) malloc(256 * sizeof(char));
      char line[1024];
      char ** k;
      int file;
      int i;
      int *l;
  
      while (1)
      {
          l = 0;
          i = 0;
          printf("myShell3 -&gt; ");
          gets(line);              // gets the input command from user
          printf("\n");
          name[i] = strtok(line, " ");
  
          while (name[i] != NULL)
          {
              if (strcmp(name[i], "&gt;") == 0)
              {
                  i++;
  
                  l = 1;
  
                  k = strtok(NULL, " ");
                  name[i - 1] = name[i];
  
              }
  
              else if (!strcmp(name[i], "&lt;"))
              {
                  i++;
                  name[i] = strtok(NULL, " ");
                  name[i - 1] = name[i];
              }
              else
              {
  
                  i++;
  
                  name[i] = strtok(NULL, " ");
              }
  
          }
  
          execute(name, k, l);
  
      }
  
  }
  </code></pre>
  
  <p>I have spent many hours on how to fix this but didnt find solution . Any help would be very much appreciated  </p>