# HG changeset patch # User Sam Lantinga # Date 1254007028 0 # Node ID 8b8314cc34a62efb913816fd8a9a995314189a2b # Parent 0319c84d274de34895658675e75957f654937825 Fixed bug #810 Lauri Kenttä 2009-09-26 06:42:23 PDT Support for disabling stdio redirect with environment variables. diff -r 0319c84d274d -r 8b8314cc34a6 src/main/win32/SDL_win32_main.c --- a/src/main/win32/SDL_win32_main.c Sat Sep 26 21:38:07 2009 +0000 +++ b/src/main/win32/SDL_win32_main.c Sat Sep 26 23:17:08 2009 +0000 @@ -38,14 +38,15 @@ #define STDOUT_FILE TEXT("stdout.txt") #define STDERR_FILE TEXT("stderr.txt") -#ifndef NO_STDIO_REDIRECT -# ifdef _WIN32_WCE +/* Set a variable to tell if the stdio redirect has been enabled. */ +static int stdioRedirectEnabled = 0; + +#ifdef _WIN32_WCE static wchar_t stdoutPath[MAX_PATH]; static wchar_t stderrPath[MAX_PATH]; -# else +#else static char stdoutPath[MAX_PATH]; static char stderrPath[MAX_PATH]; -# endif #endif #if defined(_WIN32_WCE) && _WIN32_WCE < 300 @@ -158,18 +159,19 @@ } /* Remove the output files if there was no output written */ -static void cleanup_output(void) -{ -#ifndef NO_STDIO_REDIRECT +static void cleanup_output(void) { FILE *file; int empty; -#endif /* Flush the output in case anything is queued */ fclose(stdout); fclose(stderr); -#ifndef NO_STDIO_REDIRECT + /* Without redirection we're done */ + if (!stdioRedirectEnabled) { + return; + } + /* See if the files have any output in them */ if ( stdoutPath[0] ) { file = fopen(stdoutPath, TEXT("rb")); @@ -191,7 +193,74 @@ } } } +} + +/* Redirect the output (stdout and stderr) to a file */ +static void redirect_output(void) +{ + DWORD pathlen; +#ifdef _WIN32_WCE + wchar_t path[MAX_PATH]; +#else + char path[MAX_PATH]; #endif + FILE *newfp; + + pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path)); + while ( pathlen > 0 && path[pathlen] != '\\' ) { + --pathlen; + } + path[pathlen] = '\0'; + +#ifdef _WIN32_WCE + wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); + wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); +#else + SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); + SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); +#endif + + /* Redirect standard input and standard output */ + newfp = freopen(stdoutPath, TEXT("w"), stdout); + +#ifndef _WIN32_WCE + if ( newfp == NULL ) { /* This happens on NT */ +#if !defined(stdout) + stdout = fopen(stdoutPath, TEXT("w")); +#else + newfp = fopen(stdoutPath, TEXT("w")); + if ( newfp ) { + *stdout = *newfp; + } +#endif + } +#endif /* _WIN32_WCE */ + +#ifdef _WIN32_WCE + wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) ); + wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); +#else + SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) ); + SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) ); +#endif + + newfp = freopen(stderrPath, TEXT("w"), stderr); +#ifndef _WIN32_WCE + if ( newfp == NULL ) { /* This happens on NT */ +#if !defined(stderr) + stderr = fopen(stderrPath, TEXT("w")); +#else + newfp = fopen(stderrPath, TEXT("w")); + if ( newfp ) { + *stderr = *newfp; + } +#endif + } +#endif /* _WIN32_WCE */ + + setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */ + setbuf(stderr, NULL); /* No buffering */ + stdioRedirectEnabled = 1; } #if defined(_MSC_VER) && !defined(_WIN32_WCE) @@ -263,6 +332,7 @@ char **argv; int argc; char *cmdline; + char *env_str; #ifdef _WIN32_WCE wchar_t *bufp; int nLen; @@ -270,15 +340,6 @@ char *bufp; size_t nLen; #endif -#ifndef NO_STDIO_REDIRECT - DWORD pathlen; -#ifdef _WIN32_WCE - wchar_t path[MAX_PATH]; -#else - char path[MAX_PATH]; -#endif - FILE *newfp; -#endif /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't keep them open. This is a hack.. hopefully it will be fixed @@ -289,62 +350,17 @@ FreeLibrary(handle); } -#ifndef NO_STDIO_REDIRECT - pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path)); - while ( pathlen > 0 && path[pathlen] != '\\' ) { - --pathlen; + /* Check for stdio redirect settings and do the redirection */ + if ((env_str = SDL_getenv("SDL_STDIO_REDIRECT"))) { + if (SDL_atoi(env_str)) { + redirect_output(); + } } - path[pathlen] = '\0'; - -#ifdef _WIN32_WCE - wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); - wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); -#else - SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); - SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); -#endif - - /* Redirect standard input and standard output */ - newfp = freopen(stdoutPath, TEXT("w"), stdout); - -#ifndef _WIN32_WCE - if ( newfp == NULL ) { /* This happens on NT */ -#if !defined(stdout) - stdout = fopen(stdoutPath, TEXT("w")); -#else - newfp = fopen(stdoutPath, TEXT("w")); - if ( newfp ) { - *stdout = *newfp; - } +#ifndef NO_STDIO_REDIRECT + else { + redirect_output(); + } #endif - } -#endif /* _WIN32_WCE */ - -#ifdef _WIN32_WCE - wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) ); - wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); -#else - SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) ); - SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) ); -#endif - - newfp = freopen(stderrPath, TEXT("w"), stderr); -#ifndef _WIN32_WCE - if ( newfp == NULL ) { /* This happens on NT */ -#if !defined(stderr) - stderr = fopen(stderrPath, TEXT("w")); -#else - newfp = fopen(stderrPath, TEXT("w")); - if ( newfp ) { - *stderr = *newfp; - } -#endif - } -#endif /* _WIN32_WCE */ - - setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */ - setbuf(stderr, NULL); /* No buffering */ -#endif /* !NO_STDIO_REDIRECT */ #ifdef _WIN32_WCE nLen = wcslen(szCmdLine)+128+1;