diff --git a/include/objstore.h b/include/objstore.h index 535f2ae..283a638 100644 --- a/include/objstore.h +++ b/include/objstore.h @@ -1,6 +1,8 @@ #ifndef _OBJSTORE_H_ #define _OBJSTORE_H_ +#include + #include "apple2.mem.h" #include "vm_bits.h" @@ -12,6 +14,7 @@ typedef struct { } objstore; extern int objstore_init(); +extern bool objstore_ready(); #define OBJSTORE_DECL(x) \ const vm_8bit *objstore_##x() diff --git a/src/objstore.c b/src/objstore.c index 9807807..eb249b6 100644 --- a/src/objstore.c +++ b/src/objstore.c @@ -27,7 +27,11 @@ static objstore store; int objstore_init() { - int cmp; + // Oh, you're calling this again? Cool, but let's bail before we do + // anything else. + if (objstore_ready()) { + return OK; + } // We want to input some bad header data and compare with what // eventually should get put into there by memcmp. @@ -38,6 +42,20 @@ objstore_init() memcpy(&store, store_data, sizeof(store)); + // If the copy didn't work out somehow... + if (!objstore_ready()) { + log_critical("Object store initialization failed with bad data"); + return ERR_BADFILE; + } + + return OK; +} + +bool +objstore_ready() +{ + int cmp; + // Test if the header field data is exactly equivalent to that // defined in HEADER_DATA. Note we use memcmp(), because the header // field is an array of just 4 bytes; strcmp() and strncmp() have an @@ -47,13 +65,7 @@ objstore_init() cmp = memcmp(store.header, HEADER_DATA, sizeof(store.header) / sizeof(char)); - // If the two areas of memory were not a complete match... - if (cmp != 0) { - log_critical("Object store initialization failed with bad data"); - return ERR_BADFILE; - } - - return OK; + return cmp == 0; } /*