/* querystring.c
Break up a QUERY_STRING variable.
Output is in SETENV format, ready to be eval'd
Source: Steve Shipway. <
http://www.steveshipway.org/software/utils/querystring.c> accessed 2011/5/17.
*/
#include <stdio.h>
#include <string.h>
parse( char *s
/* we can't use strtok since we are already using it at the higher level */
{
char *vname;
char *val;
int c;
char buf[3];
if (!s) {
printf("X_ERR='Null phrase'\n");
return;
}
vname = s;
while (*s && (*s != '=')) s++;
if (!*s) {
printf("X_ERR='Null assignment'\n");
return;
}
*(s++) = '\0';
printf("%s='", vname);
for (val=s; *val; val++) {
switch (*val) {
case '%':
buf[0]=*(++val); buf[1]=*(++val); buf[2]='\0';
sscanf(buf, "%2x", &c);
break;
case '+':
c = ' ';
break;
default:
c = *val;
}
switch (c) {
case '\\': printf("\\\\"); break;
case '\'': printf("\\'"); break;
case '\n': printf("\\n"); break;
default: putchar((char)c); break;
}
}
printf("'\n");
}
main()
{
char *query_string;
char *phrase;
query_string = (char *)getenv("QUERY_STRING");
if (!query_string) {
printf("X_ERR='QUERY_STRING not set'\n");
exit(0);
}
phrase = strtok(query_string, "&");
while (phrase) {
parse(phrase);
phrase = strtok((char *)0, "&");
}
}