src/stdlib/SDL_getenv.c
changeset 7003 eeaf77005c30
parent 6885 700f1b25f77f
child 7351 668a3dc28361
     1.1 --- a/src/stdlib/SDL_getenv.c	Fri Mar 15 01:02:30 2013 -0400
     1.2 +++ b/src/stdlib/SDL_getenv.c	Fri Mar 15 01:01:20 2013 -0400
     1.3 @@ -22,18 +22,19 @@
     1.4  
     1.5  #include "SDL_stdinc.h"
     1.6  
     1.7 -#ifndef HAVE_GETENV
     1.8 -
     1.9 -#if defined(__WIN32__)
    1.10 -
    1.11 +#if !defined(SDL_setenv) && defined(__WIN32__)
    1.12  #include "../core/windows/SDL_windows.h"
    1.13 -
    1.14  /* Note this isn't thread-safe! */
    1.15 -
    1.16  static char *SDL_envmem = NULL; /* Ugh, memory leak */
    1.17  static size_t SDL_envmemlen = 0;
    1.18 +#endif
    1.19 +
    1.20  
    1.21  /* Put a variable into the environment */
    1.22 +#ifdef SDL_setenv
    1.23 +#undef SDL_setenv
    1.24 +int SDL_setenv(const char *name, const char *value, int overwrite) { return SDL_setenv_inline(name, value, overwrite); }
    1.25 +#elif defined(__WIN32__)
    1.26  int
    1.27  SDL_setenv(const char *name, const char *value, int overwrite)
    1.28  {
    1.29 @@ -49,35 +50,34 @@
    1.30      }
    1.31      return 0;
    1.32  }
    1.33 -
    1.34 -/* Retrieve a variable named "name" from the environment */
    1.35 -char *
    1.36 -SDL_getenv(const char *name)
    1.37 +/* We have a real environment table, but no real setenv? Fake it w/ putenv. */
    1.38 +#elif (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
    1.39 +int
    1.40 +SDL_setenv(const char *name, const char *value, int overwrite)
    1.41  {
    1.42 -    size_t bufferlen;
    1.43 +    size_t len;
    1.44 +    char *new_variable;
    1.45  
    1.46 -    bufferlen =
    1.47 -        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
    1.48 -    if (bufferlen == 0) {
    1.49 -        return NULL;
    1.50 +    if (getenv(name) != NULL) {
    1.51 +        if (overwrite) {
    1.52 +            unsetenv(name);
    1.53 +        } else {
    1.54 +            return 0;  /* leave the existing one there. */
    1.55 +        }
    1.56      }
    1.57 -    if (bufferlen > SDL_envmemlen) {
    1.58 -        char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
    1.59 -        if (newmem == NULL) {
    1.60 -            return NULL;
    1.61 -        }
    1.62 -        SDL_envmem = newmem;
    1.63 -        SDL_envmemlen = bufferlen;
    1.64 -        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
    1.65 +
    1.66 +    /* This leaks. Sorry. Get a better OS so we don't have to do this. */
    1.67 +    len = SDL_strlen(name) + SDL_strlen(value) + 2;
    1.68 +    new_variable = (char *) SDL_malloc(len);
    1.69 +    if (!new_variable) {
    1.70 +        return (-1);
    1.71      }
    1.72 -    return SDL_envmem;
    1.73 +
    1.74 +    SDL_snprintf(new_variable, len, "%s=%s", name, value);
    1.75 +    return putenv(new_variable);
    1.76  }
    1.77 -
    1.78  #else /* roll our own */
    1.79 -
    1.80  static char **SDL_env = (char **) 0;
    1.81 -
    1.82 -/* Put a variable into the environment */
    1.83  int
    1.84  SDL_setenv(const char *name, const char *value, int overwrite)
    1.85  {
    1.86 @@ -140,8 +140,35 @@
    1.87      }
    1.88      return (added ? 0 : -1);
    1.89  }
    1.90 +#endif
    1.91  
    1.92  /* Retrieve a variable named "name" from the environment */
    1.93 +#ifdef SDL_getenv
    1.94 +#undef SDL_getenv
    1.95 +char *SDL_getenv(const char *name) { return SDL_getenv_inline(name); }
    1.96 +#elif defined(__WIN32__)
    1.97 +char *
    1.98 +SDL_getenv(const char *name)
    1.99 +{
   1.100 +    size_t bufferlen;
   1.101 +
   1.102 +    bufferlen =
   1.103 +        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
   1.104 +    if (bufferlen == 0) {
   1.105 +        return NULL;
   1.106 +    }
   1.107 +    if (bufferlen > SDL_envmemlen) {
   1.108 +        char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
   1.109 +        if (newmem == NULL) {
   1.110 +            return NULL;
   1.111 +        }
   1.112 +        SDL_envmem = newmem;
   1.113 +        SDL_envmemlen = bufferlen;
   1.114 +        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
   1.115 +    }
   1.116 +    return SDL_envmem;
   1.117 +}
   1.118 +#else
   1.119  char *
   1.120  SDL_getenv(const char *name)
   1.121  {
   1.122 @@ -160,38 +187,6 @@
   1.123      }
   1.124      return value;
   1.125  }
   1.126 -
   1.127 -#endif /* __WIN32__ */
   1.128 -
   1.129 -#endif /* !HAVE_GETENV */
   1.130 -
   1.131 -
   1.132 -/* We have a real environment table, but no real setenv? Fake it w/ putenv. */
   1.133 -#if (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
   1.134 -int
   1.135 -SDL_setenv(const char *name, const char *value, int overwrite)
   1.136 -{
   1.137 -    size_t len;
   1.138 -    char *new_variable;
   1.139 -
   1.140 -    if (getenv(name) != NULL) {
   1.141 -        if (overwrite) {
   1.142 -            unsetenv(name);
   1.143 -        } else {
   1.144 -            return 0;  /* leave the existing one there. */
   1.145 -        }
   1.146 -    }
   1.147 -
   1.148 -    /* This leaks. Sorry. Get a better OS so we don't have to do this. */
   1.149 -    len = SDL_strlen(name) + SDL_strlen(value) + 2;
   1.150 -    new_variable = (char *) SDL_malloc(len);
   1.151 -    if (!new_variable) {
   1.152 -        return (-1);
   1.153 -    }
   1.154 -
   1.155 -    SDL_snprintf(new_variable, len, "%s=%s", name, value);
   1.156 -    return putenv(new_variable);
   1.157 -}
   1.158  #endif
   1.159  
   1.160