/*Jing Chen PASSWORD.CPP */ #include #include #include #include #include #define strsize 42 #define EXTRA 5 #define datafile "../data.txt" /*-------------------------------- FUNCTIONS --------------------------------*/ void NoEntre(char *paswd) { char str[] = " INVALID PASSWORD! "; /** Print the CGI response header, required for all HTML output. **/ /** Note the extra \n, to send the blank line. **/ /** Print the HTML response page to STDOUT. **/ printf("\n") ; printf("CGI Output\n") ; printf("\n") ; printf("

%s

\n", str) ; printf("\n") ; printf("\n") ; exit(0) ; } int inputfile(char *pasw, int lensize, char *usern) { /*puts the stdin (environment variable) into username.*/ fgets(usern, lensize+1, stdin); return 1; } /* extract a string from s between the nth and (n+1)th coma in a line in greport.txt*/ void extract(char s[], char d[],int n) { int len,coma=0,j=0,i; len=strlen(s); for (i=0;i Jingchen 4444 */ void extract2(const char *username,char *name,char *passwd) { int j=0,i=0; while (username[i]!='&') { if (username[i]!='=') name[j]=username[i]; else j--; i++;j++; } i++; while (username[i]!='&') { if (username[i]!='=') name[j]=username[i]; else j--; i++;j++; } name[j]=0; i+=2; for (j=0;j<4;j++,i++) passwd[j]=username[i]; passwd[j]=0; } /*checks the password and username against the password and username in the file (passw.fle).*/ int readpasw(char *username) { char filename[] = "../greport.txt"; char CurLine[150],FirstName[20],LastName[20],Passward[5],name[30]; char buffer[80]; char passwd[10]; FILE *fname; if((fname = fopen(filename, "r")) == NULL) { printf("UNABLE TO OPEN //greport.txt"); return 0; } extract2(username,name,passwd); while( ! feof(fname)) { fgets(CurLine, 150, fname); extract(CurLine,FirstName,1); /* extract the first name */ extract(CurLine,LastName,2); /* extract the last name*/ extract(CurLine,Passward,4); /* extract the passward*/ /*strncat(FirstName," ",1);*/ strncat(FirstName,LastName,20); if ((strcmp(passwd,Passward)==0) && (strcmp(name,FirstName)==0)) { fclose(fname); return 1; } } fclose(fname); return 2; } void sum(const char *line) { int i=0,j=0; float total=0,tempf; char temp[10]; while (line[i]!=0) { while (line[i]!='=') i++; i++; j=0; while ( (line[i]!=',')&&(line[i]!=')')&& (line[i]!=';') ) { temp[j]=line[i]; j++;i++; } temp[j]=0; tempf=(float)(atof(temp)); total+=tempf; if (line[i]==')') break; i++; } printf("Total=%3.1f",total); printf("
"); } void out_page(char *file) /* output a html page to the browser */ { FILE *f; char line[300]; if ( (f=fopen(file,"rt"))==NULL ) { printf("Unable to open html file %s",file); exit(1); } while (! feof(f) ) { fgets(line,sizeof(line),f); printf("%s\n",line); } fclose(f); } void readfile(const char *username) { char filename[] = "../greport.txt"; char CurLine[150],FirstName[20],LastName[20],Passward[5],name[30]; char buffer[80]; char passwd[10]; FILE *fname; /*open file for read*/ if((fname = fopen(filename, "r")) == NULL) { printf("UNABLE TO OPEN //greport.txt"); exit(1); } extract2(username,name,passwd); while( ! feof(fname)) { fgets(CurLine, 150, fname); extract(CurLine,FirstName,1); extract(CurLine,LastName,2); extract(CurLine,Passward,4); strncat(FirstName,LastName,20); if ((strcmp(passwd,Passward)==0) && (strcmp(name,FirstName)==0)) { if ((strcmp("SKChang",FirstName)!=0)) { printf("\n") ; printf("CGI Output\n") ; printf("\n") ; printf("

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


"); printf("%s",CurLine ); printf("
"); printf("
"); sum(CurLine); printf("
"); printf("\n") ; printf("\n") ; } else { out_page("../instructor.html"); } break; } } fclose(fname); } /*----------------------------------- MAIN -----------------------------------*/ void main(void) { char *lenstr; char username[strsize + 1], password[strsize + 1]; long len; int j,p; username[0] = '\0'; password[0] = '\0'; printf("Content-type: text/html%c%c",10,10); lenstr = getenv("CONTENT_LENGTH"); if(lenstr == NULL || sscanf(lenstr,"%ld",&len)!=1 || len > (strsize - 1)) NoEntre(username); else { p = inputfile(password, len, username); if(p == 0) { exit(1); } j = readpasw(username); if(j == 0) { printf("\n

UNABLE TO OPEN PASSWORD FILE."); exit(1); } if(j == 1) { readfile(username); } if(j == 2) { NoEntre(username); } } }