Mercurial > sdl-ios-xcode
changeset 1268:f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 26 Jan 2006 06:06:56 +0000 |
parents | fdc7ef6ecab4 |
children | 905d5b482f2a |
files | include/SDL_getenv.h src/SDL_getenv.c |
diffstat | 2 files changed, 67 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- 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 <windows.h> +#include <malloc.h> +#include <string.h> + +/* 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 <stdlib.h> #include <string.h> @@ -123,6 +187,8 @@ return value; } +#endif /* WIN32 */ + #endif /* NEED_GETENV */ #ifdef TEST_MAIN