Use Win32 API for putenv/getenv to avoid C runtime conflicts
--HG-- extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%401271
This commit is contained in:
parent
0a8520eb1f
commit
a17bf3f69d
2 changed files with 67 additions and 1 deletions
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
/* Not all environments have a working getenv()/putenv() */
|
/* 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
|
#define NEED_SDL_GETENV
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,70 @@ static char rcsid =
|
||||||
|
|
||||||
#ifdef NEED_SDL_GETENV
|
#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 <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
@ -123,6 +187,8 @@ char *SDL_getenv(const char *name)
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif /* WIN32 */
|
||||||
|
|
||||||
#endif /* NEED_GETENV */
|
#endif /* NEED_GETENV */
|
||||||
|
|
||||||
#ifdef TEST_MAIN
|
#ifdef TEST_MAIN
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue