From a959b998bd77086be3f3f0cac20618cad9a653f9 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sun, 6 Jan 2013 18:56:29 +0000 Subject: [PATCH] Factor "pipe standard outputs to files" code into baselayer.c. The SDL and Windows layers had slightly different code: the latter would dereference a NULL pointer if stdout.txt failed being write-opened. git-svn-id: https://svn.eduke32.com/eduke32@3377 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/build/include/baselayer.h | 1 + polymer/eduke32/build/src/baselayer.c | 24 +++++++++++++++++++++++ polymer/eduke32/build/src/sdlayer.c | 20 +------------------ polymer/eduke32/build/src/winlayer.c | 15 +------------- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/polymer/eduke32/build/include/baselayer.h b/polymer/eduke32/build/include/baselayer.h index 33456dc16..6555a7d44 100644 --- a/polymer/eduke32/build/include/baselayer.h +++ b/polymer/eduke32/build/include/baselayer.h @@ -181,6 +181,7 @@ void wm_setapptitle(char *name); int32_t baselayer_init(); void makeasmwriteable(void); +void maybe_redirect_outputs(void); #ifdef EXTERNC } diff --git a/polymer/eduke32/build/src/baselayer.c b/polymer/eduke32/build/src/baselayer.c index 2fb51958e..6f5b0eaed 100644 --- a/polymer/eduke32/build/src/baselayer.c +++ b/polymer/eduke32/build/src/baselayer.c @@ -476,3 +476,27 @@ void makeasmwriteable(void) # endif #endif } + +void maybe_redirect_outputs(void) +{ +#if !(defined __APPLE__ && defined __BIG_ENDIAN__) + char *argp; + + // pipe standard outputs to files + if ((argp = Bgetenv("BUILD_LOGSTDOUT")) != NULL) + if (!Bstrcasecmp(argp, "TRUE")) + { + FILE *fp = freopen("stdout.txt", "w", stdout); + + if (!fp) + fp = fopen("stdout.txt", "w"); + + if (fp) + { + setvbuf(fp, 0, _IONBF, 0); + *stdout = *fp; + *stderr = *fp; + } + } +#endif +} diff --git a/polymer/eduke32/build/src/sdlayer.c b/polymer/eduke32/build/src/sdlayer.c index 25a8a146e..dcf9c348a 100644 --- a/polymer/eduke32/build/src/sdlayer.c +++ b/polymer/eduke32/build/src/sdlayer.c @@ -206,7 +206,6 @@ int32_t main(int32_t argc, char *argv[]) { int32_t r; char *argp; - FILE *fp; buildkeytranslationtable(); @@ -229,24 +228,7 @@ int32_t main(int32_t argc, char *argv[]) _buildargc = argc; _buildargv = (const char **)argv; -#if !(defined __APPLE__ && defined __BIG_ENDIAN__) - // pipe standard outputs to files - if ((argp = Bgetenv("BUILD_LOGSTDOUT")) != NULL) - if (!Bstrcasecmp(argp, "TRUE")) - { - fp = freopen("stdout.txt", "w", stdout); - - if (!fp) - fp = fopen("stdout.txt", "w"); - - if (fp) - { - setvbuf(fp, 0, _IONBF, 0); - *stdout = *fp; - *stderr = *fp; - } - } -#endif + maybe_redirect_outputs(); #ifdef USE_OPENGL if ((argp = Bgetenv("BUILD_NOFOG")) != NULL) diff --git a/polymer/eduke32/build/src/winlayer.c b/polymer/eduke32/build/src/winlayer.c index e1f8fe727..43d20fb01 100644 --- a/polymer/eduke32/build/src/winlayer.c +++ b/polymer/eduke32/build/src/winlayer.c @@ -316,7 +316,6 @@ int32_t WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, in { int32_t r; char *argp; - FILE *fp; HDC hdc; UNREFERENCED_PARAMETER(lpCmdLine); @@ -419,19 +418,7 @@ int32_t WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, in } } - // pipe standard outputs to files - if ((argp = Bgetenv("BUILD_LOGSTDOUT")) != NULL) - if (!Bstrcasecmp(argp, "TRUE")) - { - fp = freopen("stdout.txt", "w", stdout); - if (!fp) - { - fp = fopen("stdout.txt", "w"); - } - if (fp) setvbuf(fp, 0, _IONBF, 0); - *stdout = *fp; - *stderr = *fp; - } + maybe_redirect_outputs(); #ifdef USE_OPENGL if ((argp = Bgetenv("BUILD_NOFOG")) != NULL)