I have some code to parse POST and GET parameters passed to a CGI program written in C and I'd like to test it out. To parse GET parameters, I need to access the QUERY_STRING environment variable, so I'd like to set that environment variable to different things and see if my code can handle the test cases. My problem is that I cannot "set" and "get" QUERY_STRING. Here's my code (the part relevant to this topic):
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main ()
{
char* queryStringGet;
char* queryStringSet;
char* command;
queryStringGet = (char*) malloc (100); // more memory than needed
queryStringSet = (char*) malloc (100); // more memory than needed
command = (char*) malloc (100); // more memory than needed
strcpy (command, "export QUERY_STRING=");
strcpy (queryStringSet, "a=1&b=2&c=3");
strcat (command, queryStringSet);
printf ("command = %s\n", command); // display to make sure we have the correct command
system (command); // execute command. This should set the QUERY_STRING env. variable
queryStringGet = getenv ("QUERY_STRING"); // should be the same as queryStringSet
printf ("QUERY_STRING = %s\n", queryStringGet);
free (queryStringGet);
free (queryStringSet);
free (command);
return 0;
}
Results of line 19 (as expected):
command = export QUERY_STRING=a=1&b=2&c=3
Results of line 23 (not what I wanted):
QUERY_STRING = (null)
Any ideas of what I am doing wrong? I'm using Linux. Thanks.