title: getting url param value encoded in some format
body: <p>I wrote below C code to get the value of url parameter.</p>
  
  <pre><code>char *param(char *attribute_name) {
      char *query_str = getenv("QUERY_STRING");
      urlDecode(query_str);
      printf("qstr = %s&lt;br/&gt;", query_str);
      int i;
      char *param_value;
      char *temp = malloc(strlen(attribute_name) + 3);
      char *x;
      strcpy(temp, "?");
      strcat(temp, attribute_name);
      strcat(temp, "=");
  
      x = strstr(query_str, temp);
      attribute_name = strdup(attribute_name);
  
      if (x == NULL) {
          free(temp);
          temp = malloc(strlen(attribute_name) + 3);
          strcpy(temp, "&amp;");
          strcat(temp, attribute_name);
          strcat(temp, "=");
          x = strstr(query_str, temp);
      }
  
      if (x != NULL) {
          param_value = malloc(strlen(x) - strlen(temp) + 1);
          int s = strlen(x) - strlen(temp);
          int t = strlen(x) - s;
          strncpy(param_value, &amp;x[t], 1);
          for (i = strlen(temp) + 1; i &lt;= strlen(x); i++) {
              if (x[i] != '&amp;') {
                  strncat(param_value, &amp;x[i], 1);
              } else {
                  break;
              }
          }
      }
      free(temp);
  
      return param_value;
  }
  
  
  
  int urlDecode(char *str) {
      unsigned int i;
      char tmp[BUFSIZ];
      char *ptr = tmp;
      memset(tmp, 0, sizeof(tmp));
      for (i=0; i &lt; strlen(str); i++) {
          if (str[i] != '%') {
              *ptr++ = str[i];
              continue;
          }
          if (!isdigit(str[i+1]) || !isdigit(str[i+2])) {
              *ptr++ = str[i];
              continue;
          }
          *ptr++ = ((str[i+1] - '0') &lt;&lt; 4) | (str[i+2] - '0');
          i += 2;
     }
     *ptr = '\0';
     strcpy(str, tmp);
  
     return 0;
  }
  </code></pre>
  
  <p>And below is my main() function:</p>
  
  <pre><code>int main() {
      printf("Content-type: text/html\n\r\n\r");
      printf("&lt;!Doctype html&gt;");
      printf("&lt;html&gt;");
      printf("&lt;meta charset=\"UTF-8\"&gt;&lt;meta http-equiv=\"Content-type\" content=\"text/html; charset=UTF-8\"&gt;");
      printf("&lt;body&gt;");
  
      char attr[] = "u";
      char *value = param(attr);
      puts(value);
      //free(value);
  
      printf("&lt;/body&gt;&lt;/html&gt;");
      return 0;
  }
  </code></pre>
  
  <p>And below is the output I received on browser after hitting the url: <a href="http://example.com/cgi-bin/cgi_param?u=1212&amp;sa=232%203&amp;sdd=jdwjdjw" rel="nofollow">http://example.com/cgi-bin/cgi_param?u=1212&amp;sa=232%203&amp;sdd=jdwjdjw</a></p>
  
  <p>qstr = u=1212&amp;sa=232 3&amp;sdd=jdwjdjw</p>
  
  <p>�l12� </p>
  
  <p>Please correct me where am I doing mistake?
  Thanks</p>