Skip to content

Commit

Permalink
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jan 26, 2006
1 parent 6583477 commit 8045878
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/SDL_getenv.h
@@ -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

Expand Down
66 changes: 66 additions & 0 deletions src/SDL_getenv.c
Expand Up @@ -35,6 +35,70 @@ static char rcsid =

#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>

Expand Down Expand Up @@ -123,6 +187,8 @@ char *SDL_getenv(const char *name)
return value;
}

#endif /* WIN32 */

#endif /* NEED_GETENV */

#ifdef TEST_MAIN
Expand Down

0 comments on commit 8045878

Please sign in to comment.