Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed bug #810
 Lauri Kentt?      2009-09-26 06:42:23 PDT

Support for disabling stdio redirect with environment variables.
  • Loading branch information
slouken committed Sep 26, 2009
1 parent 5b08052 commit 7595428
Showing 1 changed file with 87 additions and 71 deletions.
158 changes: 87 additions & 71 deletions src/main/win32/SDL_win32_main.c
Expand Up @@ -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
Expand Down Expand Up @@ -158,18 +159,19 @@ static void cleanup(void)
}

/* 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"));
Expand All @@ -191,7 +193,74 @@ static void cleanup_output(void)
}
}
}
}

/* 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)
Expand Down Expand Up @@ -263,22 +332,14 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
char **argv;
int argc;
char *cmdline;
char *env_str;
#ifdef _WIN32_WCE
wchar_t *bufp;
int nLen;
#else
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
Expand All @@ -289,62 +350,17 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
FreeLibrary(handle);
}

#ifndef NO_STDIO_REDIRECT
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;
/* Check for stdio redirect settings and do the redirection */
if ((env_str = SDL_getenv("SDL_STDIO_REDIRECT"))) {
if (SDL_atoi(env_str)) {
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
#ifndef NO_STDIO_REDIRECT
else {
redirect_output();
}
#endif /* _WIN32_WCE */

setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */
setbuf(stderr, NULL); /* No buffering */
#endif /* !NO_STDIO_REDIRECT */
#endif

#ifdef _WIN32_WCE
nLen = wcslen(szCmdLine)+128+1;
Expand Down

0 comments on commit 7595428

Please sign in to comment.