title: C - strtok and strcmp
body: <p>I am having a bit of trouble using strtok with strcmp.</p>
  
  <pre><code>//Handles the header sent by the browser
  char* handleHeader(char *header){
          //Method given by browser (will only take GET, POST, and HEAD)
          char *method,*path, *httpVer;
  
          method = (char*)malloc(strlen(header)+1);
          strcpy(method,header);
          method = strtok(method," ");
  
  
          path = strtok(NULL," ");
          httpVer = strtok(NULL, " ");
          printf("\nMethod: %s\nPath: %s\nHTTP: %s\n",method,path,httpVer);
  
  
          printf("\nc1: %d\nc2: %d\n",strcmp(httpVer,"HTTP/1.0"),strcmp(httpVer,"HTTP/1.1"));
  
          if(!(!strcmp(httpVer,"HTTP/1.0") || (!strcmp(httpVer,"HTTP/1.1")))){
                  printf("\ngive a 400 error\n");
                  return "400 foo";
          }
  
  
          if(!strcmp(method,"GET")){
                  //char *path = strtok(NULL," ");
  
                  //If they request the root file, change the path to index.html
                  if(!strcmp(path,"/")){
                          path = (char*)malloc(strlen(BASE_DIR) + strlen("/index.html")+1);
                          strcpy(path,"/index.html");
                  }
                   return readPage(path,2);
          }
  }
  </code></pre>
  
  <p>If I give it the following header</p>
  
  <pre><code>GET / HTTP/1.0
  </code></pre>
  
  <p>I get this output:</p>
  
  <pre><code>Method: GET
  Path: /
  HTTP: HTTP/1.0
  
  
  c1: 1
  c2: -1
  
  give a 400 error
  </code></pre>
  
  <p>As you can see, strtok() parses the string properly, but the values c1, and c2 dont seem to make sense (c1 should return 0, but instead it returns 1).</p>
  
  <p>Whats going on here?</p>