Can anyone help me with this?
I can't get the correct result, and don't know what is wrong.
After the program runs, the myName variable is supposed to contain the value I pass to the cgi program
#include <stdio.h>
#include <stdlib.h>
// Helper macro to convert two-character hex strings to character value
#define ToHex(Y) (Y>='0'&&Y<='9'?Y-'0':Y-'A'+10)
char InputData[4096];
void getAllParams() {
// Determing if it is a POST or GET method
if( getenv( "REQUEST_METHOD" ) == 0 ) {
printf("No REQUEST_METHOD, must be running in DOS mode");
return;
} else if (strcmp( getenv("REQUEST_METHOD"), "POST") == 0) {
// If POST
char *endptr; // quite useless, but required
char *len1 = getenv("CONTENT_LENGTH");
int contentlength = strtol(len1, &endptr, 10);
fread(InputData , contentlength, 1, stdin);
} else {
// If GET
strcpy(InputData, getenv("QUERY_STRING"));
}
}
void getParam(const char *Name, char *Dest, char *Default) {
char *pArg = strstr(InputData, Name);
if (pArg) {
pArg += strlen(Name);
if (*pArg == '=') { // Make sure there is an '=' where we expect it
pArg++;
while (*pArg && *pArg != '&') {
if (*pArg == '%') { // Convert it to a single ASCII character and store at our destination
Dest += (char)ToHex(pArg[1]) * 16 + ToHex(pArg[2]);
pArg += 3;
} else if( *pArg=='+' ) { // If it's a '+', store a space at our destination
Dest += ' ';
pArg++;
} else {
Dest += *pArg++; // Otherwise, just store the character at our destination
}
}
return;
}
}
Dest = Default; // If param not found, then use default parameter
return;
}
int main() {
char *myName = "";
printf("Content-Type:text/html \n\n");
getAllParams();
getParam("Name", myName, "No Name");
printf("QueryString: %s", InputData);
printf("<br>");
printf("Name: %s", myName);
return 0;
}