#include #include #include #include /* ... 'InputFile' is site dependent */ #define INPUTFILE "/home/leslim/WWW/C/FUNCTIONS/function.ref.html" #define LINELENGTH 1024 void TopBit(FILE *fp); void GetString(char **String); void SelectBit(FILE *fp, char *String); void BottomBit(FILE *fp); void UpperCase(char *String); /************************************************************************/ main() { FILE *fp; char *String; /* ... Open the file */ fp = fopen(INPUTFILE, "r"); if (!fp) { printf(" Open if %s failed.", INPUTFILE); exit(0); } TopBit(fp); GetString(&String); /* ... Now filter using the supplied information. */ SelectBit(fp, String); BottomBit(fp); fclose(fp); } /************************************************************************/ void TopBit(FILE *fp) { char Line[LINELENGTH]; puts("Content-type: text/html"); puts(""); /* BLANK LINE IS REQUIRED */ /* ... Read the heading */ do { fgets(Line, LINELENGTH, fp); printf("%s", Line); } while (!strstr(Line, "")); } /************************************************************************/ void GetString(char **String) { char *TmpPtr; char *Ptr; TmpPtr = getenv("QUERY_STRING"); Ptr = (char *) malloc(strlen(TmpPtr)+1); strcpy(Ptr, TmpPtr); TmpPtr = Ptr; /* QUERY_STRING will look like. * * String=Text * */ if (TmpPtr) { *String = strtok(TmpPtr, "="); *String = strtok(NULL, "="); } else { *String = NULL; } /* Convert to uppercase */ UpperCase(*String); } /************************************************************************/ void SelectBit(FILE *fp, char *String) { char Line[LINELENGTH]; char UpperLine[LINELENGTH]; do { fgets(Line, LINELENGTH, fp); strcpy(UpperLine, Line); UpperCase(UpperLine); if (strstr(UpperLine, String)) { printf("%s", Line); } } while (!strstr(Line, "")); } /************************************************************************/ void BottomBit(FILE *fp) { char Line[LINELENGTH]; while(fgets(Line, LINELENGTH, fp) != NULL) { printf("%s", Line); } } /************************************************************************/ void UpperCase(char *String) { int Pos; for (Pos = 0; Pos < strlen(String); Pos++) { String[Pos] = toupper(String[Pos]); } }