Commit 791f5dbc authored by Ryan C. Gordon's avatar Ryan C. Gordon

Implemented SDL_setenv(), moved SDL_putenv() to compat.

  Fixes Bugzilla #779.

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%404330
parent 034bfdf7
...@@ -207,7 +207,7 @@ if test x$enable_libc = xyes; then ...@@ -207,7 +207,7 @@ if test x$enable_libc = xyes; then
AC_DEFINE(HAVE_MPROTECT) AC_DEFINE(HAVE_MPROTECT)
]), ]),
) )
AC_CHECK_FUNCS(malloc calloc realloc free getenv putenv unsetenv qsort abs bcopy memset memcpy memmove strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp sscanf snprintf vsnprintf sigaction setjmp nanosleep sysctlbyname) AC_CHECK_FUNCS(malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp sscanf snprintf vsnprintf sigaction setjmp nanosleep sysctlbyname)
AC_CHECK_LIB(m, pow, [LIBS="$LIBS -lm"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm"]) AC_CHECK_LIB(m, pow, [LIBS="$LIBS -lm"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm"])
AC_CHECK_FUNCS(ceil copysign cos cosf fabs floor log pow scalbn sin sinf sqrt) AC_CHECK_FUNCS(ceil copysign cos cosf fabs floor log pow scalbn sin sinf sqrt)
......
...@@ -297,6 +297,8 @@ extern DECLSPEC int SDLCALL SDL_EnableUNICODE(int enable); ...@@ -297,6 +297,8 @@ extern DECLSPEC int SDLCALL SDL_EnableUNICODE(int enable);
#define SDL_RenderFill SDL_RenderRect #define SDL_RenderFill SDL_RenderRect
extern DECLSPEC int SDLCALL SDL_putenv(const char *variable);
/*@}*//*Compatibility*/ /*@}*//*Compatibility*/
/* Ends C function definitions when using C++ */ /* Ends C function definitions when using C++ */
......
...@@ -90,6 +90,7 @@ ...@@ -90,6 +90,7 @@
#undef HAVE_ALLOCA #undef HAVE_ALLOCA
#ifndef _WIN32 /* Don't use C runtime versions of these on Windows */ #ifndef _WIN32 /* Don't use C runtime versions of these on Windows */
#undef HAVE_GETENV #undef HAVE_GETENV
#undef HAVE_SETENV
#undef HAVE_PUTENV #undef HAVE_PUTENV
#undef HAVE_UNSETENV #undef HAVE_UNSETENV
#endif #endif
......
...@@ -61,7 +61,9 @@ typedef unsigned long uintptr_t; ...@@ -61,7 +61,9 @@ typedef unsigned long uintptr_t;
#define HAVE_FREE 1 #define HAVE_FREE 1
#define HAVE_ALLOCA 1 #define HAVE_ALLOCA 1
#define HAVE_GETENV 1 #define HAVE_GETENV 1
#define HAVE_SETENV 1
#define HAVE_PUTENV 1 #define HAVE_PUTENV 1
#define HAVE_SETENV 1
#define HAVE_UNSETENV 1 #define HAVE_UNSETENV 1
#define HAVE_QSORT 1 #define HAVE_QSORT 1
#define HAVE_ABS 1 #define HAVE_ABS 1
......
...@@ -60,6 +60,7 @@ ...@@ -60,6 +60,7 @@
#define HAVE_FREE 1 #define HAVE_FREE 1
#define HAVE_ALLOCA 1 #define HAVE_ALLOCA 1
#define HAVE_GETENV 1 #define HAVE_GETENV 1
#define HAVE_SETENV 1
#define HAVE_PUTENV 1 #define HAVE_PUTENV 1
#define HAVE_UNSETENV 1 #define HAVE_UNSETENV 1
#define HAVE_QSORT 1 #define HAVE_QSORT 1
......
...@@ -62,6 +62,7 @@ typedef unsigned __PTRDIFF_TYPE__ uintptr_t; ...@@ -62,6 +62,7 @@ typedef unsigned __PTRDIFF_TYPE__ uintptr_t;
#define HAVE_FREE 1 #define HAVE_FREE 1
#define HAVE_ALLOCA 1 #define HAVE_ALLOCA 1
#define HAVE_GETENV 1 #define HAVE_GETENV 1
#define HAVE_SETENV 1
#define HAVE_PUTENV 1 #define HAVE_PUTENV 1
#define HAVE_QSORT 1 #define HAVE_QSORT 1
#define HAVE_ABS 1 #define HAVE_ABS 1
......
...@@ -53,6 +53,7 @@ ...@@ -53,6 +53,7 @@
#define HAVE_FREE 1 #define HAVE_FREE 1
#define HAVE_ALLOCA 1 #define HAVE_ALLOCA 1
#define HAVE_GETENV 1 #define HAVE_GETENV 1
#define HAVE_SETENV 1
#define HAVE_PUTENV 1 #define HAVE_PUTENV 1
#define HAVE_UNSETENV 1 #define HAVE_UNSETENV 1
#define HAVE_QSORT 1 #define HAVE_QSORT 1
......
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
#define HAVE_FREE 1 #define HAVE_FREE 1
#define HAVE_ALLOCA 1 #define HAVE_ALLOCA 1
#define HAVE_GETENV 1 #define HAVE_GETENV 1
#define HAVE_SETENV 1
#define HAVE_PUTENV 1 #define HAVE_PUTENV 1
#define HAVE_UNSETENV 1 #define HAVE_UNSETENV 1
#define HAVE_QSORT 1 #define HAVE_QSORT 1
......
...@@ -267,20 +267,12 @@ char *alloca(); ...@@ -267,20 +267,12 @@ char *alloca();
extern DECLSPEC char *SDLCALL SDL_getenv(const char *name); extern DECLSPEC char *SDLCALL SDL_getenv(const char *name);
#endif #endif
/** /* SDL_putenv() has moved to SDL_compat. */
* \warning On some platforms, the string you pass to SDL_putenv() becomes #ifdef HAVE_SETENV
* part of the environment table directly...it will use this specific #define SDL_setenv setenv
* buffer, and not a copy of it! This means you can't free it, and
* other pieces of code may try to write to it. In practice, this
* isn't a big deal, but be aware of the possibility.
* However, due to this issue, you should be prepared to
* pass a (char*), or be willing to cast away the constness of your
* string for this call.
*/
#ifdef HAVE_PUTENV
#define SDL_putenv putenv
#else #else
extern DECLSPEC int SDLCALL SDL_putenv(const char *variable); extern DECLSPEC int SDLCALL SDL_setenv(const char *name, const char *value,
int overwrite);
#endif #endif
#ifdef HAVE_QSORT #ifdef HAVE_QSORT
......
...@@ -1754,4 +1754,26 @@ SDL_EnableUNICODE(int enable) ...@@ -1754,4 +1754,26 @@ SDL_EnableUNICODE(int enable)
return previous; return previous;
} }
int
SDL_putenv(const char *_var)
{
char *ptr = NULL;
char *var = SDL_strdup(_var);
if (var == NULL) {
return -1; /* we don't set errno. */
}
ptr = strchr(var, '=');
if (ptr == NULL) {
SDL_free(var);
return -1;
}
*ptr = '\0'; /* split the string into name and value. */
SDL_setenv(var, ptr + 1, 1);
SDL_free(var);
return 0;
}
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */
...@@ -321,9 +321,7 @@ ESD_Init(SDL_AudioDriverImpl * impl) ...@@ -321,9 +321,7 @@ ESD_Init(SDL_AudioDriverImpl * impl)
int connection = 0; int connection = 0;
/* Don't start ESD if it's not running */ /* Don't start ESD if it's not running */
if (SDL_getenv("ESD_NO_SPAWN") == NULL) { SDL_setenv("ESD_NO_SPAWN", "1", 0);
SDL_putenv("ESD_NO_SPAWN=1");
}
connection = SDL_NAME(esd_open_sound) (NULL); connection = SDL_NAME(esd_open_sound) (NULL);
if (connection < 0) { if (connection < 0) {
......
...@@ -35,31 +35,18 @@ ...@@ -35,31 +35,18 @@
static char *SDL_envmem = NULL; /* Ugh, memory leak */ static char *SDL_envmem = NULL; /* Ugh, memory leak */
static size_t SDL_envmemlen = 0; static size_t SDL_envmemlen = 0;
/* Put a variable of the form "name=value" into the environment */ /* Put a variable into the environment */
int int
SDL_putenv(const char *variable) SDL_setenv(const char *name, const char *value, int overwrite)
{ {
size_t bufferlen; if (!overwrite) {
char *value; char ch = 0;
const char *sep; const size_t len = GetEnvironmentVariable(name, &ch, sizeof (ch));
if (len > 0) {
sep = SDL_strchr(variable, '='); return 0; /* asked not to overwrite existing value. */
if (sep == NULL) {
return -1;
}
bufferlen = SDL_strlen(variable) + 1;
if (bufferlen > SDL_envmemlen) {
char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
if (newmem == NULL) {
return -1;
} }
SDL_envmem = newmem;
SDL_envmemlen = bufferlen;
} }
SDL_strlcpy(SDL_envmem, variable, bufferlen); if (!SetEnvironmentVariable(name, *value ? value : NULL)) {
value = SDL_envmem + (sep - variable);
*value++ = '\0';
if (!SetEnvironmentVariable(SDL_envmem, *value ? value : NULL)) {
return -1; return -1;
} }
return 0; return 0;
...@@ -92,36 +79,31 @@ SDL_getenv(const char *name) ...@@ -92,36 +79,31 @@ SDL_getenv(const char *name)
static char **SDL_env = (char **) 0; static char **SDL_env = (char **) 0;
/* Put a variable of the form "name=value" into the environment */ /* Put a variable into the environment */
int int
SDL_putenv(const char *variable) SDL_setenv(const char *name, const char *value, int overwrite)
{ {
const char *name, *value;
int added; int added;
int len, i; int len, i;
char **new_env; char **new_env;
char *new_variable; char *new_variable;
/* A little error checking */ /* A little error checking */
if (!variable) { if (!name || !value) {
return (-1);
}
name = variable;
for (value = variable; *value && (*value != '='); ++value) {
/* Keep looking for '=' */ ;
}
if (*value) {
++value;
} else {
return (-1); return (-1);
} }
/* Allocate memory for the variable */ /* Allocate memory for the variable */
new_variable = SDL_strdup(variable); len = SDL_strlen(name) + SDL_strlen(value) + 2;
new_variable = (char *) SDL_malloc(len);
if (!new_variable) { if (!new_variable) {
return (-1); return (-1);
} }
SDL_snprintf(new_variable, len, "%s=%s", name, value);
value = new_variable + SDL_strlen(name) + 1;
name = new_variable;
/* Actually put it into the environment */ /* Actually put it into the environment */
added = 0; added = 0;
i = 0; i = 0;
...@@ -135,6 +117,10 @@ SDL_putenv(const char *variable) ...@@ -135,6 +117,10 @@ SDL_putenv(const char *variable)
} }
/* If we found it, just replace the entry */ /* If we found it, just replace the entry */
if (SDL_env[i]) { if (SDL_env[i]) {
if (!overwrite) {
SDL_free(new_variable);
return 0;
}
SDL_free(SDL_env[i]); SDL_free(SDL_env[i]);
SDL_env[i] = new_variable; SDL_env[i] = new_variable;
added = 1; added = 1;
...@@ -180,6 +166,36 @@ SDL_getenv(const char *name) ...@@ -180,6 +166,36 @@ SDL_getenv(const char *name)
#endif /* !HAVE_GETENV */ #endif /* !HAVE_GETENV */
/* We have a real environment table, but no real setenv? Fake it w/ putenv. */
#if (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
int
SDL_setenv(const char *name, const char *value, int overwrite)
{
size_t len;
char *new_variable;
if (getenv(name) != NULL) {
if (overwrite) {
unsetenv(name);
} else {
return 0; /* leave the existing one there. */
}
}
/* This leaks. Sorry. Get a better OS so we don't have to do this. */
len = SDL_strlen(name) + SDL_strlen(value) + 2;
new_variable = (char *) SDL_malloc(len);
if (!new_variable) {
return (-1);
}
SDL_snprintf(new_variable, len, "%s=%s", name, value);
return putenv(new_variable);
}
#endif
#ifdef TEST_MAIN #ifdef TEST_MAIN
#include <stdio.h> #include <stdio.h>
......
...@@ -60,7 +60,7 @@ void InitVideo(int argc, char *argv[]) ...@@ -60,7 +60,7 @@ void InitVideo(int argc, char *argv[])
} }
} }
SDL_putenv("SDL_VIDEO_WINDOW_POS=center"); SDL_setenv("SDL_VIDEO_WINDOW_POS", "center", 1);
if (SDL_Init(SDL_INIT_VIDEO) < 0) if (SDL_Init(SDL_INIT_VIDEO) < 0)
{ {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment