From bb5d3496eef56355a03c65eed7dc35a7ca877296 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Wed, 5 Sep 2012 17:25:37 +0000 Subject: [PATCH] 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 --- polymer/eduke32/source/demo.c | 11 +++-------- polymer/eduke32/source/game.h | 9 +++++++++ polymer/eduke32/source/gameexec.c | 15 +++++++++++---- polymer/eduke32/source/savegame.c | 11 +++++------ 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/polymer/eduke32/source/demo.c b/polymer/eduke32/source/demo.c index e6d076ceb..b27fb4d5d 100644 --- a/polymer/eduke32/source/demo.c +++ b/polymer/eduke32/source/demo.c @@ -173,22 +173,17 @@ void G_OpenDemoWrite(void) do { - int32_t nch; - if (demonum == 10000) return; - if (g_modDir[0] != '/') - 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) + if (G_ModDirSnprintf(demofn, sizeof(demofn), "edemo%d.edm", demonum)) { - // TODO: factor out this out and use everywhere else. initprintf("Couldn't start demo writing: INTERNAL ERROR: file name too long\n"); goto error_wopen_demo; } + demonum++; + g_demo_filePtr = Bfopen(demofn, "rb"); if (g_demo_filePtr == NULL) break; diff --git a/polymer/eduke32/source/game.h b/polymer/eduke32/source/game.h index 7fe2fcd67..46d7639bf 100644 --- a/polymer/eduke32/source/game.h +++ b/polymer/eduke32/source/game.h @@ -372,4 +372,13 @@ enum { 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 diff --git a/polymer/eduke32/source/gameexec.c b/polymer/eduke32/source/gameexec.c index 4845524d0..2dbb36308 100644 --- a/polymer/eduke32/source/gameexec.c +++ b/polymer/eduke32/source/gameexec.c @@ -4224,11 +4224,18 @@ nullquote: FILE *fil; char temp[BMAX_PATH]; - if (g_modDir[0] != '/') - Bsprintf(temp,"%s/%s",g_modDir,ScriptQuotes[q]); - else Bsprintf(temp,"%s",ScriptQuotes[q]); + if (G_ModDirSnprintf(temp, sizeof(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); fclose(fil); diff --git a/polymer/eduke32/source/savegame.c b/polymer/eduke32/source/savegame.c index 1e112a39b..1f84d76dd 100644 --- a/polymer/eduke32/source/savegame.c +++ b/polymer/eduke32/source/savegame.c @@ -319,12 +319,11 @@ int32_t G_SavePlayer(int32_t spot) { char temp[BMAX_PATH]; - // TODO: factor this out someday... - if (g_modDir[0] != '/') - Bsnprintf(temp, sizeof(temp), "%s/%s", g_modDir, fn); - else - Bsnprintf(temp, sizeof(temp), "%s", fn); - temp[sizeof(temp)-1] = 0; + if (G_ModDirSnprintf(temp, sizeof(temp), "%s", fn)) + { + OSD_Printf("G_SavePlayer: file name \"%s\" too long\n", fn); + return -1; + } errno = 0; fil = fopen(temp, "wb");