C 8
Security.c Guest on 21st November 2020 10:06:08 AM
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #include "i_f_eng.c"
  8.  
  9. #define ERRORLEVEL_SUCCESS       0   /* .profile runs wds program. */
  10. #define ERRORLEVEL_LOGIN_FAILURE 1   /* For 1 or above, .profile hangs up. */
  11. #define ERRORLEVEL_BAD_PARAMETER 2
  12. #define ERRORLEVEL_CONFIGURATION 3
  13. #define ERRORLEVEL_ENGINE        4
  14. #define ERRORLEVEL_FATAL         5
  15.  
  16. struct data_record_type  {
  17.   char security_number[32];
  18.   long customer_number;
  19.   }  data_record;
  20.  
  21. int
  22.   temp_int,
  23.   data_file_handle, /* Holds the file handle for the file engine. */
  24.   errcode,          /* Used for holding results of indexed file engine calls. */
  25.   result;           /* This is used to hold the result of memcmp() calls. */
  26.  
  27. char temp_string[128],     /* For general crap. */
  28.   environment_string[32],  /* Must be defined globally for putenv() to be reliable! */
  29.   data_file_name[]="security.ife";
  30.  
  31.  
  32. /*----------------------- Program functions ------------------------*/
  33. void get_security_number()
  34. {
  35. printf("Please enter the security number: ");
  36. gets(data_record.security_number);
  37. }
  38.  
  39. void get_customer_number()
  40. {
  41. char customer_number_entered[32];
  42. printf("Please enter the customer number: ");
  43. gets(customer_number_entered);
  44. data_record.customer_number = atol(customer_number_entered);
  45. }
  46.  
  47. void do_maint()
  48. {
  49. char menu_choice[32];
  50. maint_loop:
  51. printf("'A'dd, 'D'elete, or 'E'nd? ");
  52. gets(menu_choice);
  53. switch(menu_choice[0])
  54.   {
  55.   case 'e':
  56.   case 'E': break;     /* ...which returns from doing maintenance. */
  57.   case 'a':
  58.   case 'A': get_security_number();   /* Try to add this record. */
  59.             get_customer_number();
  60.             errcode = indexed_file(&data_file_handle,I_F_WRITE,&data_record,0);
  61.             if (errcode)
  62.               {
  63.               printf("?Duplicate security number or customer number\n");
  64.               printf(" Indexed file system error number: %d\n",errcode);
  65.               goto maint_loop;
  66.               }
  67.             printf("Record added!\n");
  68.             goto maint_loop;
  69.   case 'd':
  70.   case 'D': get_security_number();
  71.             if (indexed_file(&data_file_handle,I_F_READ,&data_record,0))
  72.               {
  73.               printf("?Invalid security number\n");
  74.               goto maint_loop;
  75.               }
  76.             if (indexed_file(&data_file_handle,I_F_DELETE,&data_record,0))
  77.               {
  78.               printf("?Unable to delete record after reading to delete; aborting!\n");
  79.               exit(ERRORLEVEL_ENGINE);
  80.               }
  81.             printf("Record deleted!\n");
  82.             goto maint_loop;
  83.   default:  printf("?Invalid choice%c\n",7);
  84.             goto maint_loop;
  85.   }
  86. }
  87.  
  88. /*--------------------------- The Code -------------------------------------*/
  89. int main(int argc, char *argv[])
  90. {
  91. int retry_count=0;
  92.  
  93. printf("----Begin program execution\n");
  94.  
  95. /* Open indexed file; create it if it's not there. */
  96. errcode = indexed_file(&data_file_handle,I_F_OPEN_READ,data_file_name,0);
  97. if (errcode)
  98.   {
  99.   printf("!Couldn't open data file; attempting to create new one\n");
  100.   strcpy(I_F_FILE_ATTRIBUTES,"8 2 0 4 0 4 4 0");
  101.   errcode = indexed_file(&data_file_handle,I_F_OPEN_WRITE,data_file_name,0);
  102.   }
  103. if (errcode)
  104.   {
  105.   printf("?Error checking/creating data file; error code: %d\n",errcode);
  106.   exit(ERRORLEVEL_ENGINE);
  107.   }
  108. indexed_file(&data_file_handle,I_F_CLOSE,"",0);
  109. errcode = indexed_file(&data_file_handle,I_F_OPEN_IO,data_file_name,0);
  110. if (errcode)
  111.   {
  112.   printf("?Error reopening data file for i/o; error code: %d\n",errcode);
  113.   exit(ERRORLEVEL_ENGINE);
  114.   }
  115.  
  116. get_security_number:
  117. retry_count = 0;
  118.  
  119. get_security_number_loop:
  120. get_security_number();
  121.  
  122. if (!strcmp(data_record.security_number,"1939-7004-27"))  /* Run WDS? */
  123.   {
  124.   system("wds");
  125.   goto get_security_number;
  126.   }
  127. if (!strcmp(data_record.security_number,"1939-7004-271"))  /* Maintain our database? */
  128.   {
  129.   do_maint();
  130.   goto get_security_number;
  131.   }
  132.  
  133. /* Here we are going to look in the database to try to find a security   */
  134. /*  number matching this one.  If we do, we pass the customer number to  */
  135. /*  the WDS program via an environment variable.  If not, just go back   */
  136. /*  to the security number prompt!                                       */
  137. if (indexed_file(&data_file_handle,I_F_READ,&data_record,0))
  138.   goto invalid_security_number;
  139.  
  140. /* I guess the number was valid after all!  Run the WDS program. */
  141. strcpy(environment_string,"CUSNO=");
  142. ltoa(data_record.customer_number,temp_string,10);
  143. strcat(environment_string,temp_string);
  144. putenv(environment_string);
  145. indexed_file(&data_file_handle,I_F_CLOSE,"",0);  /* Don't need this any more. */
  146. printf("Now running WDS program....\n");
  147. system("wds");
  148. return(ERRORLEVEL_SUCCESS);
  149.  
  150. invalid_security_number:
  151. if (retry_count++ == 5)     /* Too many wrong security numbers? */
  152.   {
  153.   indexed_file(&data_file_handle,I_F_CLOSE,"",0);
  154.   printf("?Too many failures to enter a valid security number; hanging up!\n");
  155.   return(ERRORLEVEL_LOGIN_FAILURE);
  156.   }
  157. printf("?Invalid security number; please try again....\n\n\n");
  158. goto get_security_number_loop;
  159. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Recent Pastes

Raw Paste

Login or Register to edit or fork this paste. It's free.