title: My chdir() function will not work. Why?
body: <p>I am writing a program that asks the user for a linux bash command and then stores them in pointer arrays (kind of like <code>char *argv[]</code>). The program must then make a check if this command is a normal bash command or a <code>cd (change directory)</code> command. If its a <code>cd</code> command then it should use something like <code>chdir()</code>. If the command is anything else I wanna use some variation of the <code>exec()</code> system call to execute that command.</p>
  
  <p>However I am not succeeding with the first part (<code>chdir()</code>).</p>
  
  <pre><code>int ii=-1
  printf("Enter the command: ");
  fgets(command, 100, stdin);
  command[strlen(command)-1]=0;
  printf("Command = %s\n", command);
  
  
  if (command[0]=='c' &amp;&amp; command[1]=='d' &amp;&amp; command[2]==' ')
  {
      printf("I am inside CD now.\n");
      cd_dump[0] = strtok(command," ");
      while(sub_string[++ii]=strtok(NULL, " ") != NULL)
      {
          printf("%s\n", sub_string[0]);
      }
  
      chdir(sub_string[0]);
  }
  </code></pre>
  
  <p>Edit:
      I have also tried the following if statement without luck.</p>
  
  <pre><code>if (command[0]=='c' &amp;&amp; command[1]=='d' &amp;&amp; command[2]==' ')
  {
      printf("I am inside CD now.\n");
      chdir(command+3);
  }
  </code></pre>
  
  <p>Sadly the program isn´t doing what I want it to, and even after hours trying to solve the issue I have no idea why. What have I done wrong? Also if I input <code>cd /home/</code> why does the output result in sub_string[0] end up with an extra "Enter key" on the output? Does strtok save the Enter key into the string?</p>
  
  <p>Any help on the subject is very much appreciated.</p>