Fixed bug #810 SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Sat, 26 Sep 2009 23:17:08 +0000
branchSDL-1.2
changeset 42468b8314cc34a6
parent 4245 0319c84d274d
child 4247 ee58103d5354
Fixed bug #810

Lauri Kenttä 2009-09-26 06:42:23 PDT

Support for disabling stdio redirect with environment variables.
src/main/win32/SDL_win32_main.c
     1.1 --- a/src/main/win32/SDL_win32_main.c	Sat Sep 26 21:38:07 2009 +0000
     1.2 +++ b/src/main/win32/SDL_win32_main.c	Sat Sep 26 23:17:08 2009 +0000
     1.3 @@ -38,14 +38,15 @@
     1.4  #define STDOUT_FILE	TEXT("stdout.txt")
     1.5  #define STDERR_FILE	TEXT("stderr.txt")
     1.6  
     1.7 -#ifndef NO_STDIO_REDIRECT
     1.8 -# ifdef _WIN32_WCE
     1.9 +/* Set a variable to tell if the stdio redirect has been enabled. */
    1.10 +static int stdioRedirectEnabled = 0;
    1.11 +
    1.12 +#ifdef _WIN32_WCE
    1.13    static wchar_t stdoutPath[MAX_PATH];
    1.14    static wchar_t stderrPath[MAX_PATH];
    1.15 -# else
    1.16 +#else
    1.17    static char stdoutPath[MAX_PATH];
    1.18    static char stderrPath[MAX_PATH];
    1.19 -# endif
    1.20  #endif
    1.21  
    1.22  #if defined(_WIN32_WCE) && _WIN32_WCE < 300
    1.23 @@ -158,18 +159,19 @@
    1.24  }
    1.25  
    1.26  /* Remove the output files if there was no output written */
    1.27 -static void cleanup_output(void)
    1.28 -{
    1.29 -#ifndef NO_STDIO_REDIRECT
    1.30 +static void cleanup_output(void) {
    1.31  	FILE *file;
    1.32  	int empty;
    1.33 -#endif
    1.34  
    1.35  	/* Flush the output in case anything is queued */
    1.36  	fclose(stdout);
    1.37  	fclose(stderr);
    1.38  
    1.39 -#ifndef NO_STDIO_REDIRECT
    1.40 +	/* Without redirection we're done */
    1.41 +	if (!stdioRedirectEnabled) {
    1.42 +		return;
    1.43 +	}
    1.44 +
    1.45  	/* See if the files have any output in them */
    1.46  	if ( stdoutPath[0] ) {
    1.47  		file = fopen(stdoutPath, TEXT("rb"));
    1.48 @@ -191,7 +193,74 @@
    1.49  			}
    1.50  		}
    1.51  	}
    1.52 +}
    1.53 +
    1.54 +/* Redirect the output (stdout and stderr) to a file */
    1.55 +static void redirect_output(void)
    1.56 +{
    1.57 +	DWORD pathlen;
    1.58 +#ifdef _WIN32_WCE
    1.59 +	wchar_t path[MAX_PATH];
    1.60 +#else
    1.61 +	char path[MAX_PATH];
    1.62  #endif
    1.63 +	FILE *newfp;
    1.64 +
    1.65 +	pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
    1.66 +	while ( pathlen > 0 && path[pathlen] != '\\' ) {
    1.67 +		--pathlen;
    1.68 +	}
    1.69 +	path[pathlen] = '\0';
    1.70 +
    1.71 +#ifdef _WIN32_WCE
    1.72 +	wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
    1.73 +	wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
    1.74 +#else
    1.75 +	SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
    1.76 +	SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
    1.77 +#endif
    1.78 +    
    1.79 +	/* Redirect standard input and standard output */
    1.80 +	newfp = freopen(stdoutPath, TEXT("w"), stdout);
    1.81 +
    1.82 +#ifndef _WIN32_WCE
    1.83 +	if ( newfp == NULL ) {	/* This happens on NT */
    1.84 +#if !defined(stdout)
    1.85 +		stdout = fopen(stdoutPath, TEXT("w"));
    1.86 +#else
    1.87 +		newfp = fopen(stdoutPath, TEXT("w"));
    1.88 +		if ( newfp ) {
    1.89 +			*stdout = *newfp;
    1.90 +		}
    1.91 +#endif
    1.92 +	}
    1.93 +#endif /* _WIN32_WCE */
    1.94 +
    1.95 +#ifdef _WIN32_WCE
    1.96 +	wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) );
    1.97 +	wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
    1.98 +#else
    1.99 +	SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
   1.100 +	SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
   1.101 +#endif
   1.102 +
   1.103 +	newfp = freopen(stderrPath, TEXT("w"), stderr);
   1.104 +#ifndef _WIN32_WCE
   1.105 +	if ( newfp == NULL ) {	/* This happens on NT */
   1.106 +#if !defined(stderr)
   1.107 +		stderr = fopen(stderrPath, TEXT("w"));
   1.108 +#else
   1.109 +		newfp = fopen(stderrPath, TEXT("w"));
   1.110 +		if ( newfp ) {
   1.111 +			*stderr = *newfp;
   1.112 +		}
   1.113 +#endif
   1.114 +	}
   1.115 +#endif /* _WIN32_WCE */
   1.116 +
   1.117 +	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
   1.118 +	setbuf(stderr, NULL);			/* No buffering */
   1.119 +	stdioRedirectEnabled = 1;
   1.120  }
   1.121  
   1.122  #if defined(_MSC_VER) && !defined(_WIN32_WCE)
   1.123 @@ -263,6 +332,7 @@
   1.124  	char **argv;
   1.125  	int argc;
   1.126  	char *cmdline;
   1.127 +	char *env_str;
   1.128  #ifdef _WIN32_WCE
   1.129  	wchar_t *bufp;
   1.130  	int nLen;
   1.131 @@ -270,15 +340,6 @@
   1.132  	char *bufp;
   1.133  	size_t nLen;
   1.134  #endif
   1.135 -#ifndef NO_STDIO_REDIRECT
   1.136 -	DWORD pathlen;
   1.137 -#ifdef _WIN32_WCE
   1.138 -	wchar_t path[MAX_PATH];
   1.139 -#else
   1.140 -	char path[MAX_PATH];
   1.141 -#endif
   1.142 -	FILE *newfp;
   1.143 -#endif
   1.144  
   1.145  	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
   1.146  	   keep them open.  This is a hack.. hopefully it will be fixed 
   1.147 @@ -289,62 +350,17 @@
   1.148  		FreeLibrary(handle);
   1.149  	}
   1.150  
   1.151 +	/* Check for stdio redirect settings and do the redirection */
   1.152 +	if ((env_str = SDL_getenv("SDL_STDIO_REDIRECT"))) {
   1.153 +		if (SDL_atoi(env_str)) {
   1.154 +			redirect_output();
   1.155 +		}
   1.156 +	}
   1.157  #ifndef NO_STDIO_REDIRECT
   1.158 -	pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
   1.159 -	while ( pathlen > 0 && path[pathlen] != '\\' ) {
   1.160 -		--pathlen;
   1.161 +	else {
   1.162 +		redirect_output();
   1.163  	}
   1.164 -	path[pathlen] = '\0';
   1.165 -
   1.166 -#ifdef _WIN32_WCE
   1.167 -	wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
   1.168 -	wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   1.169 -#else
   1.170 -	SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
   1.171 -	SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   1.172  #endif
   1.173 -    
   1.174 -	/* Redirect standard input and standard output */
   1.175 -	newfp = freopen(stdoutPath, TEXT("w"), stdout);
   1.176 -
   1.177 -#ifndef _WIN32_WCE
   1.178 -	if ( newfp == NULL ) {	/* This happens on NT */
   1.179 -#if !defined(stdout)
   1.180 -		stdout = fopen(stdoutPath, TEXT("w"));
   1.181 -#else
   1.182 -		newfp = fopen(stdoutPath, TEXT("w"));
   1.183 -		if ( newfp ) {
   1.184 -			*stdout = *newfp;
   1.185 -		}
   1.186 -#endif
   1.187 -	}
   1.188 -#endif /* _WIN32_WCE */
   1.189 -
   1.190 -#ifdef _WIN32_WCE
   1.191 -	wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) );
   1.192 -	wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   1.193 -#else
   1.194 -	SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
   1.195 -	SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
   1.196 -#endif
   1.197 -
   1.198 -	newfp = freopen(stderrPath, TEXT("w"), stderr);
   1.199 -#ifndef _WIN32_WCE
   1.200 -	if ( newfp == NULL ) {	/* This happens on NT */
   1.201 -#if !defined(stderr)
   1.202 -		stderr = fopen(stderrPath, TEXT("w"));
   1.203 -#else
   1.204 -		newfp = fopen(stderrPath, TEXT("w"));
   1.205 -		if ( newfp ) {
   1.206 -			*stderr = *newfp;
   1.207 -		}
   1.208 -#endif
   1.209 -	}
   1.210 -#endif /* _WIN32_WCE */
   1.211 -
   1.212 -	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
   1.213 -	setbuf(stderr, NULL);			/* No buffering */
   1.214 -#endif /* !NO_STDIO_REDIRECT */
   1.215  
   1.216  #ifdef _WIN32_WCE
   1.217  	nLen = wcslen(szCmdLine)+128+1;