#include #include #include // Everything here is the same as sampleprogram.c except that // there are differences when we have to read in the string 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"); /* Here is where the difference is */ // data = getenv("QUERY_STRING"); This is what we used to have int length = atoi(getenv("CONTENT_LENGTH")); int j =0; char c; c = getchar(); data = malloc(sizeof(char) * (length + 1) ); // create space to store the whole string while ( c != EOF && j < length ) { //read in one character data[j] = c; c = getchar(); j++; } //add the null character to the string data[j] = '\0'; // Now everything is the same from here on out 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; }