#include #include #include #include /* By not defining WWW_PREFIX_REQUIRED, the printf and getenv functions are */ /* mapped to Web Server/400's functions. */ #include void SendBrowserError(char *pszTitle); typedef struct _SURVEYREC { char Quest1[1]; char Quest2[8]; } SURVEYREC; /* Entry point for the CGI script */ int main(void) { char *pszTmp; SURVEYREC SurveyRec; unsigned ulQuest1Yes; unsigned ulQuest1No; unsigned ulQuest2GIF; unsigned ulQuest2JPEG; unsigned ulQuest2DontCare; unsigned ulNumRespondents; _RFILE *pFile; _RIOFB_T *pRIOFB; /* Open the database file in appended mode. This file must exist. */ pFile = _Ropen("WWWSAMPLES/SURVEY", "ar, riofb=N"); if (pFile == NULL) { SendBrowserError("Could Not Open Database"); return 0; } memset(&SurveyRec, ' ', sizeof(SurveyRec)); /* Call Get Form Var to find out the respondent's answer to the questions. */ /* Get Form Var will parse the form input from the Query String if this */ /* script was submitted as a GET or from the standard input if this */ /* script was submitted as a POST. */ pszTmp = WwwGetFormVarc("Quest1"); memcpy(SurveyRec.Quest1, pszTmp, strlen(pszTmp)); pszTmp = WwwGetFormVarc("Quest2"); memcpy(SurveyRec.Quest2, pszTmp, strlen(pszTmp)); /* Add this respondent's answers to the database. */ pRIOFB = _Rwrite(pFile, &SurveyRec, sizeof(SurveyRec)); if (pRIOFB->num_bytes != sizeof(SurveyRec)) { SendBrowserError("Could Not Write to Database"); _Rclose(pFile); return 0; } /* Close the database. */ _Rclose(pFile); /* Re-open the database in read mode in preparation for tabulation. */ pFile = _Ropen("WWWSAMPLES/SURVEY", "rr, arrseq=Y, riofb=N"); if (pFile == NULL) { SendBrowserError("Could Not Open Database"); return 0; } /* Reset some counters. */ ulQuest1Yes = 0; ulQuest1No = 0; ulQuest2GIF = 0; ulQuest2JPEG = 0; ulQuest2DontCare = 0; ulNumRespondents = 0; /* Loop through all records counting the answers to each question. */ memset(&SurveyRec, ' ', sizeof(SurveyRec)); pRIOFB = _Rreadf(pFile, &SurveyRec, sizeof(SurveyRec), __NO_LOCK); while (pRIOFB->num_bytes == sizeof(SurveyRec)) { if (SurveyRec.Quest1[0] == 'Y') ulQuest1Yes++; else if (SurveyRec.Quest1[0] == 'N') ulQuest1No++; if (memcmp(SurveyRec.Quest2, "GIF ", sizeof(SurveyRec.Quest2)) == 0) ulQuest2GIF++; else if (memcmp(SurveyRec.Quest2, "JPEG ", sizeof(SurveyRec.Quest2)) == 0) ulQuest2JPEG++; else if (memcmp(SurveyRec.Quest2, "DONTCARE", sizeof(SurveyRec.Quest2)) == 0) ulQuest2DontCare++; ulNumRespondents++; memset(&SurveyRec, ' ', sizeof(SurveyRec)); pRIOFB = _Rreadn(pFile, &SurveyRec, sizeof(SurveyRec), __NO_LOCK); } /* Close the database again. */ _Rclose(pFile); /* Send the client the reponse headers. This just sets the Content Type */ /* of the data being sent back to the browser. */ printf("Content-type: text/html%c%c", CR, LF); /* Signify the end of the response headers and the beginning of the object */ /* body. */ printf("%c%c", CR, LF); /* The following will be the Web page displayed on the browser after the */ /* user submits their answers. This just presents the current totals. */ /* Send some HTML header information to make this a clean document. */ printf(""); printf("Web Server/400 Current Survey Results"); /* Send the text that is displayed in the section. */ printf(""); printf("

Web Server/400 Current Survey Results

"); printf("The following are the averages for the %d responses.", ulNumRespondents); printf("

Question 1

"); printf("%d%% respondents prefer to see images in Web pages.

", ulQuest1Yes * 100 / ulNumRespondents); printf("%d%% respondents do not prefer to see images in Web pages.

", ulQuest1No * 100 / ulNumRespondents); printf("%d%% respondents did not specify an answer.", (ulNumRespondents - ulQuest1Yes - ulQuest1No) * 100 / ulNumRespondents); printf("


Question 2

"); printf("%d%% respondents prefer GIF images.

", ulQuest2GIF * 100 / ulNumRespondents); printf("%d%% respondents prefer JPEG images.

", ulQuest2JPEG * 100 / ulNumRespondents); printf("%d%% respondents do not care which image type is used.

", ulQuest2DontCare * 100 / ulNumRespondents); printf("%d%% respondents did not specify an answer.

", (ulNumRespondents - ulQuest2GIF - ulQuest2JPEG - ulQuest2DontCare) * 100 / ulNumRespondents); /* End the section and end the HTML document. */ printf(""); return 0; /* Return value is not used. */ } void SendBrowserError(char *pszTitle) { printf("Status: 500 Internal Error%c%c", CR, LF); printf("Content-type: text/html%c%c", CR, LF); printf("%c%c", CR, LF); printf("%s", pszTitle); printf("A problem occurred accessing the 'SURVEY' database file."); printf("Please contact the owner of this Web site to have this function "); printf("repaired."); }