#include #include #include #include "cgi.h" #define FIRSTNAME 0 #define SURNAME 1 #define TRUE 1 #define FALSE 0 #define MAXSTR 50 #define MAX_ENTRIES 10000 int numPersons; typedef struct personStruct { char firstName[MAXSTR]; char surName[MAXSTR]; char phoneNumber[MAXSTR]; } person; void printPerson(person aPerson) { printf("

%s

%s

%s

\n", aPerson.firstName, aPerson.surName, aPerson.phoneNumber); } person findMatch(char *str, int type) { char *point, tempStr[MAXSTR], *dataBuffer, line[1024]; FILE *dataFile; person aPerson; int i, match, noMatch = TRUE; /* open the data file */ dataFile = fopen("data.txt", "r"); if(dataFile == NULL) { printf("Error opening data file.\n"); exit(0); } dataBuffer = fgets(line, 256, dataFile); while(dataBuffer != NULL) { if(line[0] == '\t') { aPerson.firstName[0] = '\0'; point = &line[1]; strcpy(line, point); } else strcpy(aPerson.firstName, strtok(line, "\t")); strcpy(aPerson.surName, strtok(NULL, "\t")); strcpy(aPerson.phoneNumber, strtok(NULL, "\0")); if(type == SURNAME) strcpy(tempStr, aPerson.surName); else strcpy(tempStr, aPerson.firstName); i = 0; match = TRUE; while((match == TRUE) && (i < strlen(str)) && (i < strlen(tempStr))) { if(tempStr[i] != str[i]) match = FALSE; i++; } if((match) && (i == strlen(str))) { if(noMatch == TRUE) { noMatch = FALSE; printf("

Your search returned the following results:

\n"); printf("\n"); printf("\n"); } printPerson(aPerson); } dataBuffer = fgets(line, 256, dataFile); } if(noMatch == FALSE) printf("

First Name

Surname

Phone Number

\n"); else printf("

Your search for \"%s\" produced no results.

\n"); } typedef struct { char *name; char *val; } entry; char *makeword(char *line, char stop); char *fmakeword(FILE *f, char stop, int *len); char x2c(char *what); void unescape_url(char *url); void plustospace(char *str); void missing_entry(char * entry); int main(int argc, char *argv[]) { entry entries[MAX_ENTRIES]; register int x,m=0; int cl, count; char *dataBuffer, line[1024]; FILE *dataFile; printf("Content-type: text/html%c%c",10,10); if(strcmp(getenv("REQUEST_METHOD"),"POST")) { printf("This script should be referenced with a METHOD of POST.\n"); exit(1); } if(strcmp(getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded")) { printf("This script can only be used to decode form results. \n"); exit(1); } cl = atoi(getenv("CONTENT_LENGTH")); for(x=0;cl && (!feof(stdin));x++) { m=x; entries[x].val = fmakeword(stdin,'&',&cl); plustospace(entries[x].val); unescape_url(entries[x].val); entries[x].name = makeword(entries[x].val,'='); } /* print the header */ dataFile = fopen("header.txt", "r"); if(dataFile == NULL) { printf("Error opening file.\n"); exit(0); } dataBuffer = fgets(line, 1024, dataFile); while(dataBuffer != NULL) { printf("%s", line); dataBuffer = fgets(line, 1024, dataFile); } if(strlen(entries[0].val) < 2) printf("

Try something a bit longer than %s

\n", entries[0].val); else { /* need to get the capitalisation right - first letter upper, rest lower */ if(islower(entries[0].val[0])) entries[0].val[0] = toupper(entries[0].val[0]); for(count = 1; count < strlen(entries[0].val); count++) { if(isupper(entries[0].val[count])) entries[0].val[count] = tolower(entries[0].val[count]); } if(!strcmp(entries[1].val, "Surname")) findMatch(entries[0].val, SURNAME); else findMatch(entries[0].val, FIRSTNAME); } printf("

Search again:

"); printf("
"); printf("

", entries[0].val); printf(""); printf("

 

"); /* print the footer */ dataFile = fopen("footer.txt", "r"); if(dataFile == NULL) { printf("Error opening file.\n"); exit(0); } dataBuffer = fgets(line, 1024, dataFile); while(dataBuffer != NULL) { printf("%s\n", line); dataBuffer = fgets(line, 1024, dataFile); } return 1; }