/* Creates a file that does not exist and fills in filename with its name. filename must point to FILENAME_MAX characters buffer which doesn't need to be initialized. */ FILE *Util_uniqopen(char *filename, const char *mode) { /* We cannot simply call tmpfile(), because we don't want the file to be deleted when we close it, and we need the filename. */ #if defined(HAVE_MKSTEMP) && defined(HAVE_FDOPEN) /* this is the only implementation without a race condition */ strcpy(filename, "a8XXXXXX"); /* mkstemp() modifies the 'X'es and returns an open descriptor */ return fdopen(mkstemp(filename), mode); #elif defined(HAVE_TMPNAM) /* tmpnam() is better than mktemp(), because it creates filenames in system's temporary directory. It is also more portable. */ return fopen(tmpnam(filename), mode); #elif defined(HAVE_MKTEMP) strcpy(filename, "a8XXXXXX"); /* mktemp() modifies the 'X'es and returns filename */ return fopen(mktemp(filename), mode); #else /* Roll-your-own */ int no; for (no = 0; no < 1000000; no++) { snprintf(filename, FILENAME_MAX, "a8%06d", no); if (!Util_fileexists(filename)) return fopen(filename, mode); } return NULL; #endif }
/* * Starts the emulator for the specified snapshot file. * * savefile The name of the save file to load. */ BOOL wii_start_snapshot( char *savefile ) { BOOL succeeded = FALSE; BOOL seterror = FALSE; // Determine the extension char ext[WII_MAX_PATH]; Util_getextension( savefile, ext ); if( !strcmp( ext, WII_SAVE_GAME_EXT ) ) { char savename[WII_MAX_PATH]; // Get the save name (without extension) Util_splitpath( savefile, NULL, savename ); int namelen = strlen( savename ); int extlen = strlen( WII_SAVE_GAME_EXT ); if( namelen > extlen ) { // build the associated rom name savename[namelen - extlen - 1] = '\0'; char romfile[WII_MAX_PATH]; snprintf( romfile, WII_MAX_PATH, "%s%s", wii_get_roms_dir(), savename ); int exists = Util_fileexists( romfile ); // Ensure the rom exists if( !exists ) { wii_set_status_message( "Unable to find associated ROM file." ); seterror = TRUE; } else { // launch the emulator for the save wii_start_emulation( romfile, savefile ); succeeded = TRUE; } } } if( !succeeded && !seterror ) { wii_set_status_message( "The file selected is not a valid saved state file." ); } return succeeded; }