/****

* submitted to r/badcode

* entire program was in one 1280-line file

* written using RHIDE, a DOS (i.e. text-mode) IDE

* compiled using DJGPP

* ran on a Packard Bell 100(?) with 386SXL-25 (~ 2.9 bogomips),

* 40MB full-height IDE drive, ?? kB RAM

****/

//March 1, 2001

/****************************FPU?!********************************************

***************Link with -lemu to avoid SIGNOFP******************************/

void read_settings ( )

{

//open settings file - if not created, jump to function to get and write settings

//if it is opened successfully, read the settings from it and post to appropriate variables

int good = 1 ; //flag for continuation into nested IF statements

int already_tried_main = 0 , tried_bak = 0 , temp ;

FILE * settings ;

settings = fopen ( "config.dat" , "r" ) ;

printf ( "

Need to change read_settings()." ) ; //fix: does not detect corrupt cfg file!

do {

if ( already_tried_main != 0 ) //it doesn't exist

{

fprintf ( stderr , "

Normal configuration file cannot be opened! Trying backup file.

" ) ;

settings = fopen ( "config.bak" , "r" ) ;

tried_bak = 1 ;

good = 1 ;

//delete main config.dat file

if ( ! access ( "config.dat" , F_OK ) ) //true if it exists! (delete it)

system ( "del config.dat >NUL" ) ;

}

if ( settings != NULL ) //TRUE if either file will open

{

//file exists. hopefully data is OK.

//read data

if ( fscanf ( settings , "right.fan_set_pt \t %f

" ,& world. right . fan_set_pt ) != EOF && good != 0 )

{

if ( fscanf ( settings , "right.fan_off_dx \t %f

" ,& world. right . fan_off_dx ) != EOF && good != 0 )

{

if ( fscanf ( settings , "right.heater_offset \t %f

" ,& world. right . heater_offset ) != EOF && good != 0 )

{

if ( fscanf ( settings , "right.heater_off_dx \t %f

" ,& world. right . heater_off_dx ) != EOF && good != 0 )

{

if ( fscanf ( settings , "right.vent_secs_on \t %d

" ,& world. right . vent_secs_on ) != EOF && good != 0 )

{

if ( fscanf ( settings , "right.vent_secs_off \t %d

" ,& world. right . vent_secs_off ) != EOF && good != 0 )

{

if ( fscanf ( settings , "right.lights_on \t %d

" ,& world. right . lights_on ) != EOF && good != 0 )

{

if ( fscanf ( settings , "right.lights_off \t %d

" ,& world. right . lights_off ) != EOF && good != 0 )

{

if ( fscanf ( settings , "right.min_temp \t %f

" ,& world. right . min_temp ) != EOF && good != 0 )

{

if ( fscanf ( settings , "right.max_temp \t %f

" ,& world. right . max_temp ) != EOF && good != 0 )

{

if ( fscanf ( settings , "left.fan_set_pt \t %f

" ,& world. left . fan_set_pt ) != EOF && good != 0 )

{

if ( fscanf ( settings , "left.fan_off_dx \t %f

" ,& world. left . fan_off_dx ) != EOF && good != 0 )

{

if ( fscanf ( settings , "left.heater_offset \t %f

" ,& world. left . heater_offset ) != EOF && good != 0 )

{

if ( fscanf ( settings , "left.heater_off_dx \t %f

" ,& world. left . heater_off_dx ) != EOF && good != 0 )

{

if ( fscanf ( settings , "left.vent_secs_on \t %d

" ,& world. left . vent_secs_on ) != EOF && good != 0 )

{

if ( fscanf ( settings , "left.vent_secs_off \t %d

" ,& world. left . vent_secs_off ) != EOF && good != 0 )

{

if ( fscanf ( settings , "left.lights_on \t %d

" ,& world. left . lights_on ) != EOF && good != 0 )

{

if ( fscanf ( settings , "left.lights_off \t %d

" ,& world. left . lights_off ) != EOF && good != 0 )

{

if ( fscanf ( settings , "left.min_temp \t %f

" ,& world. left . min_temp ) != EOF && good != 0 )

{

if ( fscanf ( settings , "left.max_temp \t %f

" ,& world. left . max_temp ) != EOF && good != 0 )

{

temp = fscanf ( settings , "printer_port \t %x

" ,& world. printer_port ) ;

if ( temp == EOF || good == 0 || ( world. printer_port != 0x378 && world. printer_port != 0x278 && world. printer_port != 0x3BC ) )

{

good = 0 ; //set flag if it wasn't set

input_settings ( ) ; //get user input because file is incomplete

}

temp = world. printer_port ; //so I don't have to type as much. Optimization will take care of this.

if ( temp != 0x378 && temp != 0x278 && temp != 0x3BC )

{

puts ( "

printer port setting invalid. Resetting to default." ) ;

world. printer_port = 0x378 ; //revert to default if file is wrong.

}

}

else good = 0 ; //get user input and flag for rest of if statements

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

}

else good = 0 ;

fclose ( settings ) ;

}

else

{

good = 0 ;

}

already_tried_main ++;

} while ( ( tried_bak == 0 ) && ( good == 0 ) ) ; //loop if:

// 1) haven't tried backup file

// 2) failure reading file (good=0)

if ( good == 0 ) //read failed.

{

if ( ! access ( "config.bak" , F_OK ) ) //only delete if exists AND read failed.

system ( "delete config.bak>NUL" ) ;

input_settings ( ) ; //Neither file exists. Go through setup.

//input_settings() copies into world global variable,

//THEN calls function to write to file

//This way, we don't have to read back from file here.

}

else

{

/*****************************************************************

* *

** If we're here, read succeeded. Check if the main file exists;

** if it doesn't (access() returns *true*) copy backup to main.

**

** ALSO copy backup to main if tried_bak is nonzero, because this

** means that the config.dat is corrupted (if good != 0)

**

*****************************************************************/

if ( access ( "config.dat" , F_OK ) || tried_bak )

system ( "copy config.bak config.dat /y>NUL" ) ;

}