12 May, 2007, Hades_Kane wrote in the 1st comment:
Votes: 0
After having another immortal set one area file name to one that already existed, and after fixing the "hilarity" that ensued as a result, I decided to do something about it.

I noticed a commented out check in my aedit_file function which may have been leftover from stock or another coder having tried the same thing sometime after the codebase began being modified, but either way, uncommented and in use, it would crash.

So after looking over some stuff a bit, this is what I came up with and it seems to work:

added to merc.h:
#define AREA_DIR      "../area/"        	/* Area files */


added to olc_act.c under the aedit_file function:
FILE *fp;
char strsave[MAX_INPUT_LENGTH];

strcat( argument, ".are" );
sprintf(strsave, "%s%s", AREA_DIR, argument);
if((fp = fopen(strsave, "r")) != NULL)
{
send_to_char( "Invalid file name (already in use).\n\r", ch );
return FALSE;
}


Like I said, it seems to work, but when it comes to messing around or checking in directories, I thought I would run a quick post by others to see if they notice anything that might be bad.

I know this isn't much, but if there isn't anything that has the chance of messing something up, I'm thinking of putting up here as a small snippet.

Thanks :)
12 May, 2007, Conner wrote in the 2nd comment:
Votes: 0
Remcon had posted a fix for something sort of related to this in SmaugFUSS at http://www.smaugmuds.org/index.php?a=top... that you might want to check out. (There were several other functions that he posted similar fixes for around the same time, but this was the first I located…)
13 May, 2007, Davion wrote in the 3rd comment:
Votes: 0
if((fp = fopen(strsave, "r")) != NULL)
{
send_to_char( "Invalid file name (already in use).\n\r", ch );
return FALSE;
}


You just leave the file descriptor open like that?
Try something like
bool file_exists(const char *path)
{ FILE *fp;
if( !(fp = fopen(path, "r") ) )
return false;
fclose(fp);
return true;
}
13 May, 2007, Hades_Kane wrote in the 4th comment:
Votes: 0
The lack of a 'fclose(fp)' was the main thing I was concerned about, which is what prompted me to post this.

To be 100% honest, my understanding of certain parts of that is vague at best, and before I left something like that, I thought I would get more educated advice.

Thank you.
13 May, 2007, Davion wrote in the 5th comment:
Votes: 0
Well it's not 100% safe. If you're using ROM, which I believe you are, and you haven't removed every occurrence of the fpReserve file descriptor it might still cause you some problems :). I think there's a file_open snippet here that I released that Chilalin wrote. Works nicely and prevents nasty crash bugs caused from improperly using (or not using) fpReserve
13 May, 2007, kiasyn wrote in the 6th comment:
Votes: 0
bool fileExists( const char *fname )
{
struct stat fst;
return stat( fname, &fst ) != -1;
}
bool playerExists( const char *name )
{
char fname[256];

snprintf( fname, 256, "%s%c/%s.plr", PLAYER_DIR, tolower(name[0]), capitalize( name ) );
return check_parse_name( capitalize(name) ) && fileExists(fname);
}
13 May, 2007, Conner wrote in the 7th comment:
Votes: 0
Davion said:
Well it's not 100% safe. If you're using ROM, which I believe you are, and you haven't removed every occurrence of the fpReserve file descriptor it might still cause you some problems :). I think there's a file_open snippet here that I released that Chilalin wrote. Works nicely and prevents nasty crash bugs caused from improperly using (or not using) fpReserve


I removed all the fpReserves from my code a long while back, myself. There's also a snippet here that I released written by Rogue that does a really good job of making sure files get closed after use and even auto-closes any that get left opened (while logging them so you know which still need fixing) after a few seconds. It is written for smaug, but should be easily adaptable to rom or pretty much any other codebase that uses fopen(fp) & fclose(fp) calls.
13 May, 2007, kiasyn wrote in the 8th comment:
Votes: 0
class OpenFile
{
public:
string filename;
string mode;
OpenFile( string f, string m ) : filename(f), mode(m) {};
~OpenFile();
};
class Files
{
public:
static list<FILE *> fileList;
static FILE *openFile( string *filename, string *mode )
{
FILE *fp = fopen( filename, mode );
if ( fp )
fileList[fp] = new OpenFile( filename, mode );
}
static void closeFile( FILE *fp )
{
if ( !fp ) return;
OpenFile *file = fileList[fp];
fileList.remove( fp );
delete file;
fclose( fp );
}
};

or such
0.0/8