Next Slide
The Computerization of a University Course

CGIC Source Code
/* Program:     CGS 3422 Submit Program
   Programmer:  Scott Reynolds
   Version:     1.0
   Date:        4 October 1996
   Description: This program will take as inputs a username,
		passwordproject number, and a segment of code. 
		It then verify the username and password against 
		the specified file.  If they are correct the code 
		is written to a file and saved. */

/* Header files */
#include <stdio.h>
#include "cgic.h"
#include <ctype.h>

/* Function declarations */
void Password(char *password);
void Username(char *username);
void Project(char *projectchoice);
void Code(char *code);
int Check(char password[3], char username[3], char pronum[2]);

enum {False, True};  

/* Main Program */
int cgiMain() 
{
  /* Variable declarations */
  char password[3];
  char code[100000];
  char username[3];
  char projectchoice[2];
  char orgfilename[25] = "submit/pro";
  char filename[25];
  char End[4] = ".000";
  FILE *file;  

  /* output the necessary HTML headers and titles */
  cgiHeaderContentType("text/html");
  fprintf(cgiOut, "<HTML><HEAD>\n");
  fprintf(cgiOut, "<TITLE>Submitted Project</TITLE></HEAD>\n");
  fprintf(cgiOut, "<BODY BGCOLOR=\"#FFFFFF\">\n");
  
  /* reteive the inputted password */
  Password(password);

  /* reteive the inputted username */
  Username(username);

  /* reteive the inputted project number */
  Project(projectchoice); 

  /* verify the username and password and if they are ok continue*/
  if((Check(username, password, projectchoice)) == True)
  {
    /* reteive the inputted code segment */
    Code(code);
   
    /* concatenate the filename with the project number */
    strcat(orgfilename, projectchoice);
   
    /* concatenate the filename with the username */
    strncat(orgfilename, username, 3);
   
    /* make a copy of the filename */
    strcpy(filename, orgfilename);
   
    /* concatenate the filename with the specified END */
    strncat(filename, End, 4);

    /* while there already exists a file with that name and the
       END is less than 9 make a new END */
    while(((file = fopen(filename, "r")) != NULL) && (End[3] <= '9'))
    {
      fclose(file);
      strcpy(filename, orgfilename);
      End[3] = End[3] + 1;
      strncat(filename, End, 4);
    }
    /* if the filename was successful and END < 9 */
    if(End[3] <= '9')
    {
      /* open the file for writing and put the code in the file. Then
	 tell the user that their project was submitted */
      file = fopen(filename, "w");
      fprintf(file, "%s", code);
      fclose(file);
      fprintf(cgiOut, "<CENTER><H3>Your Project %s was submitted successfully.  Thank you %s.</H3></CENTER>", projectchoice, username);
    } 
    /* else tell the user they have exceeded the number of submits allowed */
    else
    {
      fprintf(cgiOut, "<CENTER><H3>You have exceeded the number of files allowed</H3></CENTER>");
    } 
  }


  /* output the HTML end and option buttons */
  fprintf(cgiOut, "<HR><CENTER>");
  fprintf(cgiOut, "<A HREF=\"http://grove.ufl.edu/~cgs3422\">
	 <IMG BORDER=0 SRC=\"http://grove.ufl.edu/~cgs3422/html/images/home.gif\" ALT=\"Home\"></A>");
  fprintf(cgiOut, "<A HREF=\"http://grove.ufl.edu/~cgs3422/html/submit.html\">
	 <IMG BORDER=0 SRC=\"http://grove.ufl.edu/~cgs3422/html/images/submit.gif\"></A>");
  fprintf(cgiOut, "<A HREF=\"http://grove.ufl.edu/~cgs3422/html/grades.html\">
	 <IMG BORDER=0 SRC=\"http://grove.ufl.edu/~cgs3422/html/images/grades.gif\"></A>");
  fprintf(cgiOut, "</CENTER></BODY></HTML>\n");
  return 0;
}

/* Function Implementation */

/* Password function 
   Purpose: Reads in password from HTML form and changes it to uppercase.
   Passed: character array by value
   Returns: none
*/
void Password(char *password) 
{
  int i;
  cgiFormStringNoNewlines("password",password,4);
  for(i = 0; i < 3; i++)
  {
    password[i] = toupper(password[i]);
  }
}

/* Username function 
   Purpose: Reads in username from HTML form and changes it to uppercase.
   Passed: character array by value
   Returns: none
*/
void Username(char *username) 
{
  int i;
  cgiFormStringNoNewlines("username",username,4);
  for(i = 0; i < 3; i++)
  {
    username[i] = toupper(username[i]);
  }
}

/* Project function 
   Purpose: Reads in project number from HTML form.
   Passed: character array by value
   Returns: none
*/
void Project(char *projectchoice) 
{
  cgiFormStringNoNewlines("projectchoice", projectchoice,3);
}

/* Code function 
   Purpose: Reads in full code from HTML form.
   Passed: character array by value
   Returns: none
*/
void Code(char *code) 
{
  cgiFormString("code",code,100001);
}

/* Check function 
   Purpose: Verifies if the username and password submitted are
	    valid from a secure username password file.
   Passed: password and username
   Returns: TRUE or FALSE depending on whether the test passed.
*/
 
int Check(char username[3], char password[3], char pronum[2])
{
  /* declare variables */
  int max = 30;
  char line[30];
  char low[1] = "0";
  char high[1] = "9";
  char *filecode;
  char *filepass;
  char *ssn;
  /* declare file to read from */
  char stu_file[25] = "stu_data/names.txt";
  /* initialize DONE to be false */
  int DONE = False;
  FILE *file;
  int i;

  /* if the file opens alright continue */
  if((file = fopen(stu_file, "r")) != NULL)
  {
    /* while it is not the end of the file and we are not done */
    while(fgets(line, max, file) != NULL && (DONE == False))
    {
      /* put delimiters between tokens */
      line[3] = ',';
      line[7] = ',';
      line[17] = ',';
     
      /* read in the next word and check if it is the username */
      filecode = (char*) (strtok(line, ","));
      filepass = (char*) (strtok(NULL, ","));
      ssn = (char*) (strtok(NULL, ","));

      if((strcmp(filecode, username)) == 0)
      {
	  
	  /* does the password check */
	  if((strcmp(filepass, password)) == 0) 
	{
	  DONE = True;
	}
      }
    }
    /* if the username and password did not check out */
    if(DONE == False)
    {
      fprintf(cgiOut, "<CENTER><H3>You have entered an invalid Username 
	      Password combination.</H3></CENTER>"); 
    }
  }
  /* if the file did not open properly */
  else
  {
    fprintf(cgiOut, "There is no file %s.\n", stu_file);
  }
  /* check the project number */
  if(DONE == True)
  {
    DONE = False;
    if((pronum[0] >= low[0] && pronum[0] <= high[0]) || (pronum[0] == ' '))
    {
      if((pronum[1] >= low[0] && pronum[1] <= high[0]) || (pronum[1] == NULL))
      {
	DONE = True;
      } 
    }
    /* if project number is not good print error message */
    if(DONE == False) 
    {
      fprintf(cgiOut, "<CENTER><H3>");
      fprintf(cgiOut, "Project number %s is not acceptable.  It must be between 0 and 99.\n", pronum);
      fprintf(cgiOut, "</CENTER></H3>");
    }
  }
  /* retun the outcome of the check */
  return DONE;
}