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.

I tried single quotes in your quoted string...it appears to work

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main ()
{
	char *ca;
	char queryStringSet[100];
	char command[100];  

	strcpy (command, "export QUERY_STRING=");
	strcpy (queryStringSet, "'a=1&b=2&c=3'");
	strcat (command, queryStringSet);

	printf ("command = %s\n", command);  
	system (command);  

	ca = getenv ("QUERY_STRING"); 
	printf ("QUERY_STRING = %s\n", ca);

	return 0;
}

Edited 6 Years Ago by gerard4143: n/a

Comments
Thanks.

Actually you can slim down your code even more

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main ()
{
	char *ca;
	char command[100];

	command[0] = '\0';  

	strcat (command, "export QUERY_STRING=");
	strcat (command, "'a=1&b=2&c=3'");

	printf ("command = %s\n", command);  
	system (command);  

	ca = getenv ("QUERY_STRING"); 
	printf ("QUERY_STRING = %s\n", ca);

	return 0;
}

Hmm. I copied and pasted your code verbatim and I still get the null output. You're saying you successfully got it to display "a=1&b=2&c=3" for output on line 19 (of your second post)?

try this

#include <stdlib.h>
#include <stdio.h>

int main ()
{
	char *ca;

	setenv("QUERY_STRING", "a=1&b=2&c=3", 1);

	ca = getenv ("QUERY_STRING"); 
	printf ("QUERY_STRING = %s\n", ca);

	return 0;
}

Edited 6 Years Ago by gerard4143: n/a

Strange. Sometimes it's working, sometimes it's not. It seems to depend on where I am and whether I am root or not and whether I'm using the "sudo" command. I think the code must be right, but it's doing something based on permissions and users, etc. Maybe I'm getting too fancy. Anyway, when I execute it under an unprivileged account in my home directory, it works every time, so I'm going to chalk it up to my changing users and maybe not having the permissions set up or something. Thanks a bunch!

This question has already been answered. Start a new discussion instead.