/* * adder.c - a minimal CGI program that adds two numbers together * * Jim: * - changed content-type: to text/plain * - put in some debugging comments * * command line test : * $ QUERY_STRING="2&3" ./adder * Connection: close * Content-length: 99 * Content-type: text/plain * * Welcome to add.com: THE Internet addition portal. * The answer is: 2 + 3 = 5 * Thanks for visiting! */ /* $begin adder */ #include "csapp.h" int main(void) { char *buf, *p; char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE]; int n1=0, n2=0; /* Extract the two arguments */ if ((buf = getenv("QUERY_STRING")) != NULL) { //printf("getenv(QUERY_STRING) = '%s'\n", buf); p = strchr(buf, '&'); *p = '\0'; strcpy(arg1, buf); strcpy(arg2, p+1); //printf("arg1 string : '%s'\n", buf); //printf("arg2 string : '%s'\n", p+1); n1 = atoi(arg1); //printf("n1 = %d\n", n1); n2 = atoi(arg2); //printf("n2 = %d\n", n2); } /* Make the response body */ sprintf(content, "Welcome to add.com: "); sprintf(content, "%sTHE Internet addition portal.\r\n", content); sprintf(content, "%sThe answer is: %d + %d = %d\r\n", content, n1, n2, n1 + n2); sprintf(content, "%sThanks for visiting!\r\n", content); /* Generate the HTTP response */ printf("Connection: close\r\n"); printf("Content-length: %d\r\n", (int)strlen(content)); printf("Content-type: text/plain\r\n\r\n"); printf("%s", content); fflush(stdout); exit(0); } /* $end adder */