title: implementing cd system call using C- if condition
body: <p>Here is code that implements the cd system call using C. The problem with this code is that it's not entering the <code>if</code> condition  <code>if(strcmp(buffer,"cd") == 0)</code>
  and I can't understand why.</p>
  
  <pre><code>#include&lt;sys/stat.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include&lt;dirent.h&gt;
  #include&lt;error.h&gt;
  
  #define BUFFERSIZE 20
  int main(){
  
  char *args[80]; 
  char buffer[BUFFERSIZE];
  char *prompt = "OS";
  char *a = "&gt;";
  printf("%s%s",prompt,a); 
  fgets(buffer, BUFFERSIZE, stdin);  
  
  char *tok; 
  tok = strtok (buffer," ");
  
  
  while(buffer != NULL){ 
     buffer[strlen(buffer)-1] = '\0';  
     pid_t pid;
     pid = fork();
     if(pid &lt; 0){
        fprintf(stderr, "Fork failed");
        return 1;
     }
     else if(pid == 0){
  
         if(strcmp(buffer,"cd") == 0){
           tok = strtok(NULL,"\n");
           cd(tok);
         }
         printf("%s%s",prompt,a); 
         fgets(buffer, BUFFERSIZE, stdin);
     }
     else{
       wait(NULL);
     }
  }
  return 0;
  }
  
  
  int cd(char *pth){
     char path[1000];
     strcpy(path,pth);
  
     static char *prompt = "OS";
     static char *a = "&gt;";
     char *token;
  
     char cwd[256]; 
     getcwd(cwd,sizeof(cwd));
  
     strcat(cwd,"/"); 
     strcat(cwd,path);
     chdir(cwd);    
  
     printf("%s-%s%s",prompt,path,a);
     return 0;
    }
  </code></pre>