initialize automatically if needed ans conditions are met

This commit is contained in:
Gwenhael Le Moine 2023-09-20 13:33:16 +02:00
parent 0f5b59afb0
commit f515cc09f7
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
3 changed files with 84 additions and 32 deletions

View file

@ -1,5 +1,4 @@
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -27,33 +26,6 @@ long port2_size;
long port2_mask;
short port2_is_ram;
void get_home_directory( char* path ) {
char* p;
struct passwd* pwd;
if ( homeDirectory[ 0 ] == '/' )
strcpy( path, homeDirectory );
else {
p = getenv( "HOME" );
if ( p ) {
strcpy( path, p );
strcat( path, "/" );
} else {
pwd = getpwuid( getuid() );
if ( pwd ) {
strcpy( path, pwd->pw_dir );
strcat( path, "/" );
} else {
if ( verbose )
fprintf( stderr, "can\'t figure out your home directory, "
"trying /tmp\n" );
strcpy( path, "/tmp" );
}
}
strcat( path, homeDirectory );
}
}
int read_rom( const char* fname ) {
int ram_size;
@ -105,8 +77,6 @@ void saturn_config_init( void ) {
}
void init_saturn( void ) {
int i;
memset( &saturn, 0, sizeof( saturn ) - 4 * sizeof( unsigned char* ) );
saturn.PC = 0x00000;
saturn.magic = X48_MAGIC;
@ -124,7 +94,7 @@ void init_saturn( void ) {
saturn.timer1 = 0;
saturn.timer2 = 0x2000;
saturn.bank_switch = 0;
for ( i = 0; i < NR_MCTL; i++ ) {
for ( int i = 0; i < NR_MCTL; i++ ) {
if ( i == 0 )
saturn.mem_cntl[ i ].unconfigured = 1;
else if ( i == 5 )
@ -138,14 +108,18 @@ void init_saturn( void ) {
}
int init_emulator( void ) {
/* If not forced to initialize and files are readble => let's go */
if ( !initialize && read_files() ) {
if ( resetOnStartup )
saturn.PC = 0x00000;
return 0;
}
init_saturn();
/* if forced initialize or files were not readble => initialize */
if ( verbose )
fprintf( stderr, "initialization of %s\n", homeDirectory );
init_saturn();
if ( !read_rom( romFileName ) )
exit( 1 );

View file

@ -1,6 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/stat.h>
#include <getopt.h>
@ -52,6 +55,33 @@ char* mediumFont = "-*-fixed-bold-r-normal-*-15-*-*-*-*-*-iso8859-1";
char* largeFont = "-*-fixed-medium-r-normal-*-20-*-*-*-*-*-iso8859-1";
char* connFont = "-*-fixed-medium-r-normal-*-12-*-*-*-*-*-iso8859-1";
void get_home_directory( char* path ) {
char* p;
struct passwd* pwd;
if ( homeDirectory[ 0 ] == '/' )
strcpy( path, homeDirectory );
else {
p = getenv( "HOME" );
if ( p ) {
strcpy( path, p );
strcat( path, "/" );
} else {
pwd = getpwuid( getuid() );
if ( pwd ) {
strcpy( path, pwd->pw_dir );
strcat( path, "/" );
} else {
if ( verbose )
fprintf( stderr, "can\'t figure out your home directory, "
"trying /tmp\n" );
strcpy( path, "/tmp" );
}
}
strcat( path, homeDirectory );
}
}
int parse_args( int argc, char* argv[] ) {
int option_index;
int c = '?';
@ -300,5 +330,52 @@ int parse_args( int argc, char* argv[] ) {
fprintf( stderr, "connFont = %s\n", connFont );
}
/* check that homeDirectory exists, otherwise initialize */
char config_dir[ 1024 ];
char rom_filename[ 1024 ];
char config_filename[ 1024 ];
struct stat sb;
get_home_directory( config_dir );
if ( romFileName[ 0 ] == '/' )
strcpy( rom_filename, "" );
else
strcpy( rom_filename, config_dir );
strcat( rom_filename, romFileName );
if ( stat( config_dir, &sb ) == 0 && S_ISDIR( sb.st_mode ) &&
stat( rom_filename, &sb ) == 0 ) {
if ( verbose )
fprintf( stderr, "%s exists\n", config_dir );
/* a config_dir exists with a romFileName in it. */
/* we can initialize if necessary */
if ( !initialize ) {
/* Not forced to initialize but does stateFileName exist? */
if ( stateFileName[ 0 ] == '/' )
strcpy( config_filename, "" );
else
strcpy( config_filename, config_dir );
strcat( config_filename, stateFileName );
/* if not then initialize */
initialize = stat( config_filename, &sb ) == 0 ? 0 : 1;
/* Not forced to initialize but does ramFileName exist? */
if ( ramFileName[ 0 ] == '/' )
strcpy( config_filename, "" );
else
strcpy( config_filename, config_dir );
strcat( config_filename, ramFileName );
/* if not then initialize */
initialize = stat( config_filename, &sb ) == 0 ? 0 : 1;
}
} else {
if ( mkdir( config_dir, 0755 ) == 0 )
fprintf( stderr, "Created %s, please copy a rom in it.\n",
config_dir );
exit( 1 );
}
return ( optind );
}

View file

@ -50,6 +50,7 @@ extern char* connFont;
/*************/
/* functions */
/*************/
extern void get_home_directory( char* path );
extern int parse_args( int argc, char* argv[] );
#endif /* !_OPTIONS_H */