#include #include #include int main(void) { char *data; long m,n; char* s = malloc(100 * sizeof(char)); char* s2 = malloc(100 * sizeof(char)); // char* s2; printf("%s%c%c\n", "Content-Type:text/html;charset=iso-8859-1",13,10); printf("Multiplication results\n"); printf("

Multiplication results

\n"); data = getenv("QUERY_STRING"); printf("

Data is: \n
%s " , data); //data ends up being a string that looks something like "m=3&n=4&s=somestuff&l=someotherstuff" // the syntax for this is "nameOfFirstField=valueInFirstField&nameOfSecondField=valueInSecondField& etc" // so we'll use string tokenizer to split at every &. // then we can use sscanf() to separate the part that is the name of the field from the value of the field. if(data == NULL) { printf("

Error! Error in passing data from form to script."); return 0; } char delimiters[] = "&"; char* str = strtok( data, delimiters); //printf("%s", data); if (sscanf(str, "m=%ld", &m ) == 0 ) { printf("

Invalid data! M must be a number!
\n"); } str = strtok(NULL, delimiters); if (sscanf(str, "n=%ld", &n ) == 0 ){ printf("

Invalid data! n must be a number! \n"); } str = strtok(NULL, delimiters); sscanf(str, "s=%s", s); //we need to go through string and turn the pluses into spaces! int i; for (i=0; i < strlen(s); i++ ) { if (s[i] == '+') s[i] = ' '; } str = strtok(NULL, delimiters); sscanf(str, "l=%s", s2); for (i=0; i < strlen(s2); i++) { if (s2[i] == '+') s2[i] = ' '; } printf("

The product of %ld and %ld is %ld.",m,n,m*n); //s stores the rest whole thing, we need to do a bit of tokenizing around the &... printf("

Your strings are \"%s\" and \"%s\"", s, s2 ); return 0; }