Factor out snprintf'ing a maybe-modDir'd file name into macro G_ModDirSnprintf().

... and use it in three places. In two of these uses,
1) CON {read,write}arrayfromfile and
2) G_SavePlayer,
display an error message if the file name such generated is too long.

In the CON commands of 1), also error out if the file couldn't be opened.

git-svn-id: https://svn.eduke32.com/eduke32@2997 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-09-05 17:25:37 +00:00
parent e417274c1e
commit bb5d3496ee
4 changed files with 28 additions and 18 deletions

View file

@ -173,22 +173,17 @@ void G_OpenDemoWrite(void)
do do
{ {
int32_t nch;
if (demonum == 10000) if (demonum == 10000)
return; return;
if (g_modDir[0] != '/') if (G_ModDirSnprintf(demofn, sizeof(demofn), "edemo%d.edm", demonum))
nch=Bsnprintf(demofn, sizeof(demofn), "%s/edemo%d.edm", g_modDir, demonum++);
else nch=Bsnprintf(demofn, sizeof(demofn), "edemo%d.edm", demonum++);
if ((unsigned)nch >= sizeof(demofn)-1)
{ {
// TODO: factor out this out and use everywhere else.
initprintf("Couldn't start demo writing: INTERNAL ERROR: file name too long\n"); initprintf("Couldn't start demo writing: INTERNAL ERROR: file name too long\n");
goto error_wopen_demo; goto error_wopen_demo;
} }
demonum++;
g_demo_filePtr = Bfopen(demofn, "rb"); g_demo_filePtr = Bfopen(demofn, "rb");
if (g_demo_filePtr == NULL) if (g_demo_filePtr == NULL)
break; break;

View file

@ -372,4 +372,13 @@ enum {
ST_31_TWO_WAY_TRAIN = 31, ST_31_TWO_WAY_TRAIN = 31,
}; };
# define G_ModDirSnprintf(buf, size, basename, ...) \
( \
( \
(g_modDir[0] != '/') ? \
Bsnprintf(buf, size, "%s/" basename, g_modDir, ## __VA_ARGS__) : \
Bsnprintf(buf, size, basename, ## __VA_ARGS__) \
) >= ((int32_t)size)-1 \
)
#endif #endif

View file

@ -4224,11 +4224,18 @@ nullquote:
FILE *fil; FILE *fil;
char temp[BMAX_PATH]; char temp[BMAX_PATH];
if (g_modDir[0] != '/') if (G_ModDirSnprintf(temp, sizeof(temp), "%s", ScriptQuotes[q]))
Bsprintf(temp,"%s/%s",g_modDir,ScriptQuotes[q]); {
else Bsprintf(temp,"%s",ScriptQuotes[q]); OSD_Printf(CON_ERROR "file name too long\n",g_errorLineNum,keyw[g_tw]);
continue;
}
if ((fil = fopen(temp,"wb")) == 0) continue; fil = fopen(temp,"wb");
if (fil == NULL)
{
OSD_Printf(CON_ERROR "couldn't open file",g_errorLineNum,keyw[g_tw]);
continue;
}
fwrite(aGameArrays[j].plValues,1,sizeof(int) * aGameArrays[j].size,fil); fwrite(aGameArrays[j].plValues,1,sizeof(int) * aGameArrays[j].size,fil);
fclose(fil); fclose(fil);

View file

@ -319,12 +319,11 @@ int32_t G_SavePlayer(int32_t spot)
{ {
char temp[BMAX_PATH]; char temp[BMAX_PATH];
// TODO: factor this out someday... if (G_ModDirSnprintf(temp, sizeof(temp), "%s", fn))
if (g_modDir[0] != '/') {
Bsnprintf(temp, sizeof(temp), "%s/%s", g_modDir, fn); OSD_Printf("G_SavePlayer: file name \"%s\" too long\n", fn);
else return -1;
Bsnprintf(temp, sizeof(temp), "%s", fn); }
temp[sizeof(temp)-1] = 0;
errno = 0; errno = 0;
fil = fopen(temp, "wb"); fil = fopen(temp, "wb");