Advertisement
Guest User

r/badcode submission

a guest
Apr 20th, 2013
1,611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.60 KB | None | 0 0
  1. /****
  2. * submitted to r/badcode
  3. * entire program was in one 1280-line file
  4. * written using RHIDE, a DOS (i.e. text-mode) IDE
  5. * compiled using DJGPP
  6. * ran on a Packard Bell 100(?) with 386SXL-25 (~ 2.9 bogomips),
  7. * 40MB full-height IDE drive, ?? kB RAM
  8. ****/
  9.  
  10. //March 1, 2001
  11.  
  12. /****************************FPU?!********************************************
  13.  ***************Link with -lemu to avoid SIGNOFP******************************/
  14.  
  15. void read_settings()
  16. {
  17.     //open settings file - if not created, jump to function to get and write settings
  18.     //if it is opened successfully, read the settings from it and post to appropriate variables
  19.     int good=1; //flag for continuation into nested IF statements
  20.     int already_tried_main=0, tried_bak=0,temp;
  21.     FILE *settings;
  22.     settings=fopen("config.dat","r");
  23.     printf("\nNeed to change read_settings()."); //fix: does not detect corrupt cfg file!
  24.     do {
  25.         if (already_tried_main!=0)                     //it doesn't exist
  26.         {
  27.             fprintf(stderr,"\nNormal configuration file cannot be opened! Trying backup file.\n");
  28.             settings=fopen("config.bak","r");
  29.             tried_bak=1;
  30.             good=1;
  31.             //delete main config.dat file
  32.             if (!access("config.dat",F_OK))     //true if it exists! (delete it)
  33.                 system("del config.dat >NUL");
  34.         }
  35.        
  36.         if (settings!=NULL)         //TRUE if either file will open
  37.         {
  38.             //file exists.  hopefully data is OK.
  39.             //read data
  40.             if (fscanf(settings,"right.fan_set_pt\t%f\n",&world.right.fan_set_pt)!=EOF && good!=0)
  41.             {
  42.                 if (fscanf(settings,"right.fan_off_dx\t%f\n",&world.right.fan_off_dx)!=EOF && good!=0)
  43.                 {
  44.                     if (fscanf(settings,"right.heater_offset\t%f\n",&world.right.heater_offset)!=EOF && good!=0)
  45.                     {
  46.                         if (fscanf(settings,"right.heater_off_dx\t%f\n",&world.right.heater_off_dx)!=EOF && good!=0)
  47.                         {
  48.                             if (fscanf(settings,"right.vent_secs_on\t%d\n",&world.right.vent_secs_on)!=EOF && good!=0)
  49.                             {
  50.                                 if (fscanf(settings,"right.vent_secs_off\t%d\n",&world.right.vent_secs_off)!=EOF && good!=0)
  51.                                 {
  52.                                     if (fscanf(settings,"right.lights_on\t%d\n",&world.right.lights_on)!=EOF && good!=0)
  53.                                     {
  54.                                         if (fscanf(settings,"right.lights_off\t%d\n",&world.right.lights_off)!=EOF && good!=0)
  55.                                         {
  56.                                             if (fscanf(settings,"right.min_temp\t%f\n",&world.right.min_temp)!=EOF && good!=0)
  57.                                             {
  58.                                                 if (fscanf(settings,"right.max_temp\t%f\n",&world.right.max_temp)!=EOF && good!=0)
  59.                                                 {
  60.                                                     if (fscanf(settings,"left.fan_set_pt\t%f\n",&world.left.fan_set_pt)!=EOF && good!=0)
  61.                                                     {
  62.                                                         if (fscanf(settings,"left.fan_off_dx\t%f\n",&world.left.fan_off_dx)!=EOF && good!=0)
  63.                                                         {
  64.                                                             if (fscanf(settings,"left.heater_offset\t%f\n",&world.left.heater_offset)!=EOF && good!=0)
  65.                                                             {
  66.                                                                 if (fscanf(settings,"left.heater_off_dx\t%f\n",&world.left.heater_off_dx)!=EOF && good!=0)
  67.                                                                 {
  68.                                                                     if (fscanf(settings,"left.vent_secs_on\t%d\n",&world.left.vent_secs_on)!=EOF && good!=0)
  69.                                                                     {
  70.                                                                         if (fscanf(settings,"left.vent_secs_off\t%d\n",&world.left.vent_secs_off)!=EOF && good!=0)
  71.                                                                         {
  72.                                                                             if (fscanf(settings,"left.lights_on\t%d\n",&world.left.lights_on)!=EOF && good!=0)
  73.                                                                             {
  74.                                                                                 if (fscanf(settings,"left.lights_off\t%d\n",&world.left.lights_off)!=EOF && good!=0)
  75.                                                                                 {
  76.                                                                                     if (fscanf(settings,"left.min_temp\t%f\n",&world.left.min_temp)!=EOF && good!=0)
  77.                                                                                     {
  78.                                                                                         if (fscanf(settings,"left.max_temp\t%f\n",&world.left.max_temp)!=EOF && good!=0)
  79.                                                                                         {
  80.                                                                                             temp=fscanf(settings,"printer_port\t%x\n",&world.printer_port);
  81.                                                                                             if (temp==EOF || good==0 || (world.printer_port != 0x378 && world.printer_port != 0x278 && world.printer_port != 0x3BC))
  82.                                                                                             {
  83.                                                                                                 good=0;       //set flag if it wasn't set
  84.                                                                                                 input_settings();  //get user input because file is incomplete
  85.                                                                                             }
  86.                                                                                             temp=world.printer_port;    //so I don't have to type as much.  Optimization will take care of this.
  87.                                                                                             if (temp!=0x378 && temp!=0x278 && temp!=0x3BC)
  88.                                                                                             {
  89.                                                                                                 puts("\nprinter port setting invalid. Resetting to default.");
  90.                                                                                                 world.printer_port=0x378; //revert to default if file is wrong.
  91.                                                                                             }
  92.                                                                                         }
  93.                                                                                         else good = 0;  //get user input and flag for rest of if statements
  94.                                                                                     }
  95.                                                                                     else good = 0;
  96.                                                                                 }
  97.                                                                                 else good = 0;
  98.                                                                             }
  99.                                                                             else good = 0;
  100.                                                                         }
  101.                                                                         else good = 0;
  102.                                                                     }
  103.                                                                     else good = 0;
  104.                                                                 }
  105.                                                                 else good = 0;
  106.                                                             }
  107.                                                             else good = 0;
  108.                                                         }
  109.                                                         else good = 0;
  110.                                                     }
  111.                                                     else good = 0;
  112.                                                 }
  113.                                                 else good = 0;
  114.                                             }
  115.                                             else good = 0;
  116.                                         }
  117.                                         else good = 0;
  118.                                     }
  119.                                     else good = 0;
  120.                                 }
  121.                                 else good = 0;
  122.                             }
  123.                             else good = 0;
  124.                         }
  125.                         else good = 0;
  126.                     }
  127.                     else good = 0;
  128.                 }
  129.                 else good = 0;
  130.             }
  131.             else good = 0;
  132.            
  133.             fclose(settings);
  134.         }
  135.         else
  136.         {
  137.             good=0;
  138.         }
  139.         already_tried_main++;
  140.     } while ((tried_bak==0) && (good==0));  //loop if:
  141.     //  1) haven't tried backup file
  142.     //  2) failure reading file (good=0)
  143.     if (good==0)  //read failed.
  144.     {
  145.         if (!access("config.bak",F_OK)) //only delete if exists AND read failed.
  146.             system("delete config.bak>NUL");
  147.         input_settings();   //Neither file exists.  Go through setup.
  148.         //input_settings() copies into world global variable,
  149.         //THEN calls function to write to file
  150.         //This way, we don't have to read back from file here.
  151.     }
  152.     else
  153.     {
  154.         /*****************************************************************
  155.          * *
  156.          **  If we're here, read succeeded.  Check if the main file exists;
  157.          **      if it doesn't (access() returns *true*) copy backup to main.
  158.          **
  159.          **  ALSO copy backup to main if tried_bak is nonzero, because this
  160.          **      means that the config.dat is corrupted (if good != 0)
  161.     **
  162.     *****************************************************************/
  163.         if (access("config.dat",F_OK) || tried_bak)
  164.             system("copy config.bak config.dat /y>NUL");
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement