/* Simple CGI program that returns grades */ /* This program assumes each request will include the following fields (coming from an HTML form): fname : first name lname : last name ssn : Social Security Number The program looks up the grades for the individual corresponding to the fields and returns a list of grades. We don't actually have a database of grades (or names, or anything else) - the grades are just created randomly... */ #include #include #include #include #include #include "cgilib.h" /* simple CGI request parsing routines */ #define MAXFIELDS 10 /* Max number of fields we will handle */ /* print HTML error message and exit */ void fatal_error( char *s) { printf("

\n"); printf("%s

\n",s); exit(1); } /* see if a string contains any nonblank chars */ int empty( char *s) { return(strlen(s)==strspn( s, " \t\n")); } char *grades[] = { "Test#1", "Test#2", "Test#2", "HW#1", "HW#2", "HW#3" }; int n_grades = sizeof(grades)/sizeof(char *); int random_grade(void) { static int seeded=0; if (!seeded) { srand(time(NULL)); seeded=1; } return(50 + (int) (50.0*rand()/(RAND_MAX+1.0))); } /* generate_grade_table just makes up the grades, there is no database!!!!! */ void generate_grade_table(char *f, char *l, char *s) { int i,tot,grade; /* print the student name and SSN */ printf("

%s %s (%s)

\n",f,l,s); /* print the grade table */ printf("
\n"); printf("\n"); printf("\n"); for (i=0;i%s\n",grades[i]); printf("\n"); printf("\n"); printf("\n"); tot=0; for (i=0;i%d\n",grade); } printf(" \n",tot/(double)n_grades); printf("\n"); printf("
Final
Average
%.2f
\n\n"); printf("
\n"); } int main() { char *query; char *names[MAXFIELDS]; char *vals[MAXFIELDS]; char *lname,*fname,*ssn; int i,n; /* always tell the browser what kind of document we are sending */ /* NOTE: the web server that started the CGI program will send back the appropriate HTTP header !!! */ printf("Content-type: text/html\n\n"); /* now send some HTML stuff */ printf("\n"); printf("

Netprog grades delivered to your desktop

\n"); query = get_request(); if (query==NULL) { /* we got nothing, or there is an error */ /* send back an HTML error message */ fatal_error("ERROR - Invalid query or something"); } /* parse the request string and chop into fields */ n = split_and_parse(query,names,vals,MAXFIELDS); /* If we got more then 3 fields, this is a bogus request */ if (n!=3) fatal_error("ERROR - Invalid query or something"); /* this is a crummy way to do it, we really need an associative array for handling the fields (C++?). */ lname=fname=ssn=NULL; for (i=0;i<3;i++) { if (strcasecmp(names[i],"fname")==0) { fname = vals[i]; } else if (strcasecmp(names[i],"lname")==0) { lname = vals[i]; } else if (strcasecmp(names[i],"ssn")==0) { ssn = vals[i]; } else { /* unknown field name - this is an error */ fatal_error("ERROR - Invalid field or something"); exit(1); } } /* Make sure we got them all */ if ((!lname) || (!fname) || (!ssn)) { /* missing field name - this is an error */ printf("ERROR - Missing field"); exit(1); } /* Make sure something was entered for each field */ if (empty(fname)) fatal_error("Error - no first name entered"); else if (empty(lname)) fatal_error("Error - no last name entered"); else if (empty(ssn)) fatal_error("Error - no SSN entered"); /* OK - everything checks out - now process the request */ generate_grade_table(fname,lname,ssn); return(0); }