Use Win32 API for putenv/getenv to avoid C runtime conflicts
authorSam Lantinga <slouken@libsdl.org>
Thu, 26 Jan 2006 06:06:56 +0000
changeset 1268f098b247299d
parent 1267 fdc7ef6ecab4
child 1269 905d5b482f2a
Use Win32 API for putenv/getenv to avoid C runtime conflicts
include/SDL_getenv.h
src/SDL_getenv.c
     1.1 --- a/include/SDL_getenv.h	Wed Jan 25 20:36:21 2006 +0000
     1.2 +++ b/include/SDL_getenv.h	Thu Jan 26 06:06:56 2006 +0000
     1.3 @@ -1,7 +1,7 @@
     1.4  
     1.5  /* Not all environments have a working getenv()/putenv() */
     1.6  
     1.7 -#if defined(macintosh) || defined(_WIN32_WCE)
     1.8 +#if defined(macintosh) || defined(WIN32) || defined(_WIN32_WCE)
     1.9  #define NEED_SDL_GETENV
    1.10  #endif
    1.11  
     2.1 --- a/src/SDL_getenv.c	Wed Jan 25 20:36:21 2006 +0000
     2.2 +++ b/src/SDL_getenv.c	Thu Jan 26 06:06:56 2006 +0000
     2.3 @@ -35,6 +35,70 @@
     2.4  
     2.5  #ifdef NEED_SDL_GETENV
     2.6  
     2.7 +#ifdef WIN32
     2.8 +
     2.9 +#define WIN32_LEAN_AND_MEAN
    2.10 +#include <windows.h>
    2.11 +#include <malloc.h>
    2.12 +#include <string.h>
    2.13 +
    2.14 +/* Note this isn't thread-safe! */
    2.15 +
    2.16 +static char *SDL_envmem = NULL;	/* Ugh, memory leak */
    2.17 +static DWORD SDL_envmemlen = 0;
    2.18 +
    2.19 +/* Put a variable of the form "name=value" into the environment */
    2.20 +int SDL_putenv(const char *variable)
    2.21 +{
    2.22 +	DWORD bufferlen;
    2.23 +	char *value;
    2.24 +	const char *sep;
    2.25 +
    2.26 +	sep = strchr(variable, '=');
    2.27 +	if ( sep == NULL ) {
    2.28 +		return -1;
    2.29 +	}
    2.30 +	bufferlen = strlen(variable)+1;
    2.31 +	if ( bufferlen > SDL_envmemlen ) {
    2.32 +		char *newmem = (char *)realloc(SDL_envmem, bufferlen);
    2.33 +		if ( newmem == NULL ) {
    2.34 +			return -1;
    2.35 +		}
    2.36 +		SDL_envmem = newmem;
    2.37 +		SDL_envmemlen = bufferlen;
    2.38 +	}
    2.39 +	strcpy(SDL_envmem, variable);
    2.40 +	value = SDL_envmem + (sep - variable);
    2.41 +	*value++ = '\0';
    2.42 +	if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
    2.43 +		return -1;
    2.44 +	}
    2.45 +	return 0;
    2.46 +}
    2.47 +
    2.48 +/* Retrieve a variable named "name" from the environment */
    2.49 +char *SDL_getenv(const char *name)
    2.50 +{
    2.51 +	DWORD bufferlen;
    2.52 +
    2.53 +	bufferlen = GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
    2.54 +	if ( bufferlen == 0 ) {
    2.55 +		return NULL;
    2.56 +	}
    2.57 +	if ( bufferlen > SDL_envmemlen ) {
    2.58 +		char *newmem = (char *)realloc(SDL_envmem, bufferlen);
    2.59 +		if ( newmem == NULL ) {
    2.60 +			return NULL;
    2.61 +		}
    2.62 +		SDL_envmem = newmem;
    2.63 +		SDL_envmemlen = bufferlen;
    2.64 +		GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
    2.65 +	}
    2.66 +	return SDL_envmem;
    2.67 +}
    2.68 +
    2.69 +#else /* roll our own */
    2.70 +
    2.71  #include <stdlib.h>
    2.72  #include <string.h>
    2.73  
    2.74 @@ -123,6 +187,8 @@
    2.75  	return value;
    2.76  }
    2.77  
    2.78 +#endif /* WIN32 */
    2.79 +
    2.80  #endif /* NEED_GETENV */
    2.81  
    2.82  #ifdef TEST_MAIN