Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 15th, 2010 | Syntax: C | Size: 2.16 KB | Hits: 72 | Expires: Never
Copy text to clipboard
  1. #include <stdio.h>
  2. #include <string.h>
  3. void decode_value(const char *key, char *value, int size)
  4. {
  5.           int length = 0, i = 0, j = 0;
  6.           char *pos1 = '\0', *pos2 = '\0';
  7.           //if the string key is in the query string
  8.           if( ( pos1 = strstr((char *) getenv("QUERY_STRING"), key)) != NULL )
  9.           {
  10.                       //find start of value for this key
  11.                       for(i=0; i<strlen(key); i++) pos1++;
  12.                       //find length of the value
  13.                       if( (pos2 = strstr(pos1,"&")) != NULL )
  14.                                  length = pos2 - pos1;
  15.                       else length = strlen(pos1);
  16.                       //character by character, copy value from query string
  17.                       for(i = 0, j = 0; i < length ; i++, j++)
  18.                       {
  19.                                  if(j < size) value[j] = pos1[i];
  20.                       }
  21.                       //add NULL character to end of the value
  22.                       if(j < size) value[j] = '\0';
  23.                       else value[size-1] = '\0';
  24.           }
  25. }
  26. int main(int argc, char *argv[], char *env[])
  27. {
  28.           printf("Content-type:text/html\n\n<html><body bgcolor=#23abe2>\n");
  29.           char value[255] = "";
  30.           strncpy(value,(char *) getenv("QUERY_STRING"),255);
  31.           printf("QUERY_STRING : %s<BR>\n", value );
  32.           printf("<form>\n");
  33.           //call the decode_value function to get value of "ITEM1"
  34.           decode_value( "ITEM1=", (char *) &value, 255);
  35.           if(strlen(value) > 0 )
  36.                       printf("<input type=\"TEXT\" name=\"ITEM1\" value=\"%s\">\n",value);
  37.           else
  38.                       printf("<input type=\"TEXT\" name=\"ITEM1\">\n");
  39.           //call the decode_value function to get value of "ITEM2"
  40.           decode_value( "ITEM2=", (char *) &value, 255);
  41.           if(strlen(value) > 0 )
  42.                       printf("<input type=\"TEXT\" name=\"ITEM2\" value=\"%s\">\n",value);
  43.           else
  44.                       printf("<input type=\"TEXT\" name=\"ITEM2\">\n");
  45.           printf("<input type=\"SUBMIT\">");
  46.           printf("</form></body></html>\n");
  47.    return 0;
  48. }