# HG changeset patch # User Sam Lantinga # Date 1138255616 0 # Node ID f098b247299de2ec6b6df4fb3b39558de395a723 # Parent fdc7ef6ecab4216914c23e006dd9c079f1d552bd Use Win32 API for putenv/getenv to avoid C runtime conflicts diff -r fdc7ef6ecab4 -r f098b247299d include/SDL_getenv.h --- a/include/SDL_getenv.h Wed Jan 25 20:36:21 2006 +0000 +++ b/include/SDL_getenv.h Thu Jan 26 06:06:56 2006 +0000 @@ -1,7 +1,7 @@ /* Not all environments have a working getenv()/putenv() */ -#if defined(macintosh) || defined(_WIN32_WCE) +#if defined(macintosh) || defined(WIN32) || defined(_WIN32_WCE) #define NEED_SDL_GETENV #endif diff -r fdc7ef6ecab4 -r f098b247299d src/SDL_getenv.c --- a/src/SDL_getenv.c Wed Jan 25 20:36:21 2006 +0000 +++ b/src/SDL_getenv.c Thu Jan 26 06:06:56 2006 +0000 @@ -35,6 +35,70 @@ #ifdef NEED_SDL_GETENV +#ifdef WIN32 + +#define WIN32_LEAN_AND_MEAN +#include +#include +#include + +/* Note this isn't thread-safe! */ + +static char *SDL_envmem = NULL; /* Ugh, memory leak */ +static DWORD SDL_envmemlen = 0; + +/* Put a variable of the form "name=value" into the environment */ +int SDL_putenv(const char *variable) +{ + DWORD bufferlen; + char *value; + const char *sep; + + sep = strchr(variable, '='); + if ( sep == NULL ) { + return -1; + } + bufferlen = strlen(variable)+1; + if ( bufferlen > SDL_envmemlen ) { + char *newmem = (char *)realloc(SDL_envmem, bufferlen); + if ( newmem == NULL ) { + return -1; + } + SDL_envmem = newmem; + SDL_envmemlen = bufferlen; + } + strcpy(SDL_envmem, variable); + value = SDL_envmem + (sep - variable); + *value++ = '\0'; + if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) { + return -1; + } + return 0; +} + +/* Retrieve a variable named "name" from the environment */ +char *SDL_getenv(const char *name) +{ + DWORD bufferlen; + + bufferlen = GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen); + if ( bufferlen == 0 ) { + return NULL; + } + if ( bufferlen > SDL_envmemlen ) { + char *newmem = (char *)realloc(SDL_envmem, bufferlen); + if ( newmem == NULL ) { + return NULL; + } + SDL_envmem = newmem; + SDL_envmemlen = bufferlen; + GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen); + } + return SDL_envmem; +} + +#else /* roll our own */ + #include #include @@ -123,6 +187,8 @@ return value; } +#endif /* WIN32 */ + #endif /* NEED_GETENV */ #ifdef TEST_MAIN