title: Problems of History for my own Shell in C
body: <p>As a project, I have to make my own shell. I did it but I have some problems with the history feature.</p>
  
  <p>Here's piece of my code:</p>
  
  <pre><code>int main(int argc, char* argv[])
  {
      char saisie[300], cwd[1024];
      char* nom = getenv("USER");
      char* backup[MAXCMD];
      int boucle = 1, n = 0, i, u = 0, b = 0;
  
      for(i = 0; i &lt; MAXCMD; i++) 
      {
          backup[i] = NULL;
      }
      for(i = 0; i &lt; MAX_INPUT_SZ; i++)
      {
          saisie[i] = 0;
      }
      char* cmd[MAXPARAMS];    //MAXPARAMS is 20
  
      while( boucle == 1)
      {   
          printf("%s@sam:~ %s&gt; ", nom, (getcwd(cwd, sizeof(cwd))));
          fgets(saisie,MAX_INPUT_SZ,stdin);
          printf("\n");
          split_input(saisie, cmd);
  
          free(backup[u]);
          backup[u] = strdup(saisie);
          u = (u + 1) % MAXCMD;
  
          b = switchcmd(cmd,backup,b,u);
          start(cmd,b);
          b = 0;                      //débloquage fonction start
      }
      return 0;
  }
  </code></pre>
  
  <p>I print the history with this fonction:</p>
  
  <pre><code>int historique(char* backup[], int u)
  {
      int i = u;
      int place = 1;
      do
      {
          if (backup[i])
          {
              printf("%4d:  %s\n", place, backup[i]);
              place++;
          }
          i = (i + 1) % MAXCMD;
      } while (i != u);
          return 0;   
  }
  </code></pre>
  
  <p>B is used to block the execution fonction (start) when user enter "cd" or "history", because it will generate an error.</p>
  
  <p>Here's the fonction triggered when user enters "cd", "history", or "exit":</p>
  
  <pre><code>int switchcmd(char** cmd,char** backup, int b,int u)
  
  {
      int i, n = 3, switch_value = 0;
      char* error;
      char* listcmd[n];
      listcmd[0] = "cd";
      listcmd[1] = "exit";
      listcmd[2]  = "history";
  
  for (i = 0; i &lt; n; ++i)
  {
      if(strcmp(cmd[0], listcmd[i]) == 0)
      {
          switch_value = i + 1;
          break;
      }
  }
  
  switch (switch_value)
  {
      case 1:
          chdir(cmd[1]);
          b = 1;
          error = strerror(errno);
          if (*error != 0)
          {
              printf("sam: %s: %s\n", cmd[0], error);
          }
          break;
  
      case 2:
  
          printf("Bye bye\n");
          exit(0);
  
      case 3:
          historique((char**)backup,u);
          b = 1;
          break;
  }
  return b;
  }
  </code></pre>
  
  <p>When I execute my shell, and enter these commands successively, they work. <strong>°i1</strong></p>
  
  <pre><code>&gt; clear    
  &gt; ls -a -l
  &gt; ls -a
  &gt; cd ..
  &gt; man chdir
  </code></pre>
  
  <p>Then "history" for printing the history, I have this :  <strong>°i2</strong></p>
  
  <pre><code>   1:  clear
     2:  ls
     3:  ls
     4:  cd
     5:  man
     6:  history
  </code></pre>
  
  <p>and I want this output, with all parameters: <strong>°i3</strong></p>
  
  <pre><code>1:  clear
  2:  ls -a -l
  3:  ls -a
  4:  cd ..
  5:  man chdir
  6:  history`
  </code></pre>
  
  <p>I dont know why, and I don't understand why <code>strdup</code> does not duplicate my cmd in backup at it should.</p>
  
  <p>Any help please?</p>