

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "i_f_eng.c"

#define ERRORLEVEL_SUCCESS       0   /* .profile runs wds program. */
#define ERRORLEVEL_LOGIN_FAILURE 1   /* For 1 or above, .profile hangs up. */
#define ERRORLEVEL_BAD_PARAMETER 2
#define ERRORLEVEL_CONFIGURATION 3
#define ERRORLEVEL_ENGINE        4
#define ERRORLEVEL_FATAL         5

struct data_record_type  {
  char security_number[32];
  long customer_number;
  }  data_record;

int
  temp_int,
  data_file_handle, /* Holds the file handle for the file engine. */
  errcode,          /* Used for holding results of indexed file engine calls. */
  result;           /* This is used to hold the result of memcmp() calls. */

char temp_string[128],     /* For general crap. */
  environment_string[32],  /* Must be defined globally for putenv() to be reliable! */
  data_file_name[]="security.ife";


/*----------------------- Program functions ------------------------*/
void get_security_number()
{
printf("Please enter the security number: ");
gets(data_record.security_number);
}

void get_customer_number()
{
char customer_number_entered[32];
printf("Please enter the customer number: ");
gets(customer_number_entered);
data_record.customer_number = atol(customer_number_entered);
}

void do_maint()
{
char menu_choice[32];
maint_loop:
printf("'A'dd, 'D'elete, or 'E'nd? ");
gets(menu_choice);
switch(menu_choice[0])
  {
  case 'e':
  case 'E': break;     /* ...which returns from doing maintenance. */
  case 'a':
  case 'A': get_security_number();   /* Try to add this record. */
            get_customer_number();
            errcode = indexed_file(&data_file_handle,I_F_WRITE,&data_record,0);
            if (errcode)
              {
              printf("?Duplicate security number or customer number\n");
              printf(" Indexed file system error number: %d\n",errcode);
              goto maint_loop;
              }
            printf("Record added!\n");
            goto maint_loop;
  case 'd':
  case 'D': get_security_number();
            if (indexed_file(&data_file_handle,I_F_READ,&data_record,0))
              {
              printf("?Invalid security number\n");
              goto maint_loop;
              }
            if (indexed_file(&data_file_handle,I_F_DELETE,&data_record,0))
              {
              printf("?Unable to delete record after reading to delete; aborting!\n");
              exit(ERRORLEVEL_ENGINE);
              }
            printf("Record deleted!\n");
            goto maint_loop;
  default:  printf("?Invalid choice%c\n",7);
            goto maint_loop;
  }
}

/*--------------------------- The Code -------------------------------------*/
int main(int argc, char *argv[])
{
int retry_count=0;

printf("----Begin program execution\n");

/* Open indexed file; create it if it's not there. */
errcode = indexed_file(&data_file_handle,I_F_OPEN_READ,data_file_name,0);
if (errcode)
  {
  printf("!Couldn't open data file; attempting to create new one\n");
  strcpy(I_F_FILE_ATTRIBUTES,"8 2 0 4 0 4 4 0");
  errcode = indexed_file(&data_file_handle,I_F_OPEN_WRITE,data_file_name,0);
  }
if (errcode)
  {
  printf("?Error checking/creating data file; error code: %d\n",errcode);
  exit(ERRORLEVEL_ENGINE);
  }
indexed_file(&data_file_handle,I_F_CLOSE,"",0);
errcode = indexed_file(&data_file_handle,I_F_OPEN_IO,data_file_name,0);
if (errcode)
  {
  printf("?Error reopening data file for i/o; error code: %d\n",errcode);
  exit(ERRORLEVEL_ENGINE);
  }

get_security_number:
retry_count = 0;

get_security_number_loop:
get_security_number();

if (!strcmp(data_record.security_number,"1939-7004-27"))  /* Run WDS? */
  {
  system("wds");
  goto get_security_number;
  }
if (!strcmp(data_record.security_number,"1939-7004-271"))  /* Maintain our database? */
  {
  do_maint();
  goto get_security_number;
  }

/* Here we are going to look in the database to try to find a security   */
/*  number matching this one.  If we do, we pass the customer number to  */
/*  the WDS program via an environment variable.  If not, just go back   */
/*  to the security number prompt!                                       */
if (indexed_file(&data_file_handle,I_F_READ,&data_record,0))
  goto invalid_security_number;

/* I guess the number was valid after all!  Run the WDS program. */
strcpy(environment_string,"CUSNO=");
ltoa(data_record.customer_number,temp_string,10);
strcat(environment_string,temp_string);
putenv(environment_string);
indexed_file(&data_file_handle,I_F_CLOSE,"",0);  /* Don't need this any more. */
printf("Now running WDS program....\n");
system("wds");
return(ERRORLEVEL_SUCCESS);

invalid_security_number:
if (retry_count++ == 5)     /* Too many wrong security numbers? */
  {
  indexed_file(&data_file_handle,I_F_CLOSE,"",0);
  printf("?Too many failures to enter a valid security number; hanging up!\n");
  return(ERRORLEVEL_LOGIN_FAILURE);
  }
printf("?Invalid security number; please try again....\n\n\n");
goto get_security_number_loop;
}

