/* Please note: You are free to use and distribute and modify this code. I accept no responsibility for any misbehaviour of the program. It works great for me (http://www.stumptown.com/brews/). I opted against comand line arguements for security reasons. Just alter the file names for QFILE,CFILE,and HFILE to suit yourself. */ #include #include /* for atoi */ #include /* for time(): used in srand() */ const char *QFILE="quotes.txt"; const char *CFILE="count.txt"; const char *HFILE="head.html"; const int LSIZE=500; char *get_quote(int); int get_random(float); float count_quotes(); char *mal_fname(char *); int get_count(); void put_count(int); void print_html(int, char *); void print_error(char *); int main() { int curCount=1; char *quote=""; int lineNo=1,usedQuote=0; float quoteNo=1.0; if(curCount=get_count()){ curCount++; put_count(curCount); } // else print_error("count file in main"); if(quoteNo=count_quotes()) { lineNo=get_random(quoteNo); quote=get_quote(lineNo); usedQuote=1; } // else print_error("Quote File in main"); print_html(curCount,quote); if(usedQuote) free(quote); return 0; } int get_count() { FILE *countFile; char *curCount; int c=0, j=0, fpos=0; if(countFile=fopen(CFILE, "r")){ fpos=ftell(countFile); while((c=fgetc(countFile))!=EOF && (isdigit(c)||iscntrl(c))) j++; if(c==EOF || c=='\n'){ fseek(countFile,fpos,0); curCount = (char *)malloc(j*sizeof(char)); fgets(curCount, j, countFile); c=atoi(curCount); free(curCount); } else{ c=0; // print_error("count file is corrupt or contains text."); } fclose(countFile); } else{ c=0; // print_error("Cannot open count file for read."); } return c; } void put_count(int cCount) { FILE *countFile; if(countFile=fopen(CFILE, "w")){ fprintf(countFile, "%d\n", cCount); fclose(countFile); } // else print_error("Cannot open count file for write."); } void print_html(int cCount,char *quote) { FILE *htmlFile; int c; if(htmlFile=fopen(HFILE, "r")){ printf("Content-type: text/html\n\n"); while((c=fgetc(htmlFile))!=EOF){ if((c!='*')&&(c!='~')) putchar(c); else{ if(c!='~') printf("%d",cCount); else printf("%s",quote); } } fclose(htmlFile); } else print_error("Unable to open html template file for read."); } void print_error(char *msg) { printf("Content-type: text/html\n\n"); printf("\n"); printf("\n"); printf("Error\n"); printf("\n"); printf("\n"); printf("
"); printf("

An Error occured loading this page

\n"); printf("
Please Notify Webmaster the following error occured:
\n"); printf("%s\n", msg); printf("\n"); printf("\n"); } float count_quotes() { FILE *quoteFile; int c; float j=0.0; if(quoteFile=fopen(QFILE, "r")){ while((c=fgetc(quoteFile))!=EOF) if(c=='*') j++; fclose(quoteFile); } return j; } char *get_quote(int lineNo) { FILE *quoteFile; char tquote[LSIZE]; char *quote; char *errorMsg="An Error Occured: No quotes available"; int c, i=0, j=0, done=0, finished=0; if((quoteFile=fopen(QFILE, "r"))&&(lineNo>=0)){ while(((c=fgetc(quoteFile))!=EOF) && (!done)) { if(j==lineNo){ done=1; for(i=0;(!finished);i++){ tquote[i]=c; c=fgetc(quoteFile); if(c=='*'){ tquote[i]='\0'; quote=(char *) malloc((i+1)*sizeof(char)); strcpy(quote,tquote); finished=1; } } } if(c=='*') j++; } fclose(quoteFile); // quote[i]='\0'; } else { quote=(char *) malloc((strlen(errorMsg)+1)*sizeof(char)); strcpy(quote,errorMsg); } return quote; } int get_random(float quoteNo) { int j=0; srand((unsigned)(time(NULL))); j=(int) ((quoteNo-1.0)*rand()/(RAND_MAX+1.0)); return j; } void out_count(int cCount) { FILE *htmlFile; int c; if(htmlFile=fopen(HFILE, "r")){ printf("Content-type: text/html\n\n"); while((c=fgetc(htmlFile))!=EOF){ if(c!='*') putchar(c); else printf("%d",cCount); } fclose(htmlFile); } else print_error("Unable to open html template file for read."); }