src/stdlib/SDL_getenv.c
changeset 3581 15eea7a1fa97
parent 2859 99210400e8b9
child 3583 65f66674b7fb
     1.1 --- a/src/stdlib/SDL_getenv.c	Wed Dec 16 08:17:05 2009 +0000
     1.2 +++ b/src/stdlib/SDL_getenv.c	Wed Dec 16 10:59:51 2009 +0000
     1.3 @@ -35,31 +35,18 @@
     1.4  static char *SDL_envmem = NULL; /* Ugh, memory leak */
     1.5  static size_t SDL_envmemlen = 0;
     1.6  
     1.7 -/* Put a variable of the form "name=value" into the environment */
     1.8 +/* Put a variable into the environment */
     1.9  int
    1.10 -SDL_putenv(const char *variable)
    1.11 +SDL_setenv(const char *name, const char *value, int overwrite)
    1.12  {
    1.13 -    size_t bufferlen;
    1.14 -    char *value;
    1.15 -    const char *sep;
    1.16 -
    1.17 -    sep = SDL_strchr(variable, '=');
    1.18 -    if (sep == NULL) {
    1.19 -        return -1;
    1.20 +    if (!overwrite) {
    1.21 +        char ch = 0;
    1.22 +        const size_t len = GetEnvironmentVariable(name, &ch, sizeof (ch));
    1.23 +        if (len > 0) {
    1.24 +            return 0;  /* asked not to overwrite existing value. */
    1.25 +        }
    1.26      }
    1.27 -    bufferlen = SDL_strlen(variable) + 1;
    1.28 -    if (bufferlen > SDL_envmemlen) {
    1.29 -        char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
    1.30 -        if (newmem == NULL) {
    1.31 -            return -1;
    1.32 -        }
    1.33 -        SDL_envmem = newmem;
    1.34 -        SDL_envmemlen = bufferlen;
    1.35 -    }
    1.36 -    SDL_strlcpy(SDL_envmem, variable, bufferlen);
    1.37 -    value = SDL_envmem + (sep - variable);
    1.38 -    *value++ = '\0';
    1.39 -    if (!SetEnvironmentVariable(SDL_envmem, *value ? value : NULL)) {
    1.40 +    if (!SetEnvironmentVariable(name, *value ? value : NULL)) {
    1.41          return -1;
    1.42      }
    1.43      return 0;
    1.44 @@ -92,36 +79,31 @@
    1.45  
    1.46  static char **SDL_env = (char **) 0;
    1.47  
    1.48 -/* Put a variable of the form "name=value" into the environment */
    1.49 +/* Put a variable into the environment */
    1.50  int
    1.51 -SDL_putenv(const char *variable)
    1.52 +SDL_setenv(const char *name, const char *value, int overwrite)
    1.53  {
    1.54 -    const char *name, *value;
    1.55      int added;
    1.56      int len, i;
    1.57      char **new_env;
    1.58      char *new_variable;
    1.59  
    1.60      /* A little error checking */
    1.61 -    if (!variable) {
    1.62 -        return (-1);
    1.63 -    }
    1.64 -    name = variable;
    1.65 -    for (value = variable; *value && (*value != '='); ++value) {
    1.66 -        /* Keep looking for '=' */ ;
    1.67 -    }
    1.68 -    if (*value) {
    1.69 -        ++value;
    1.70 -    } else {
    1.71 +    if (!name || !value) {
    1.72          return (-1);
    1.73      }
    1.74  
    1.75      /* Allocate memory for the variable */
    1.76 -    new_variable = SDL_strdup(variable);
    1.77 +    len = SDL_strlen(name) + SDL_strlen(value) + 2;
    1.78 +    new_variable = (char *) SDL_malloc(len);
    1.79      if (!new_variable) {
    1.80          return (-1);
    1.81      }
    1.82  
    1.83 +    SDL_snprintf(new_variable, len, "%s=%s", name, value);
    1.84 +    value = new_variable + SDL_strlen(name) + 1;
    1.85 +    name = new_variable;
    1.86 +
    1.87      /* Actually put it into the environment */
    1.88      added = 0;
    1.89      i = 0;
    1.90 @@ -135,6 +117,10 @@
    1.91          }
    1.92          /* If we found it, just replace the entry */
    1.93          if (SDL_env[i]) {
    1.94 +            if (!overwrite) {
    1.95 +                SDL_free(new_variable);
    1.96 +                return 0;
    1.97 +            }
    1.98              SDL_free(SDL_env[i]);
    1.99              SDL_env[i] = new_variable;
   1.100              added = 1;
   1.101 @@ -180,6 +166,36 @@
   1.102  
   1.103  #endif /* !HAVE_GETENV */
   1.104  
   1.105 +
   1.106 +/* We have a real environment table, but no real setenv? Fake it w/ putenv. */
   1.107 +#if (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
   1.108 +int
   1.109 +SDL_setenv(const char *name, const char *value, int overwrite)
   1.110 +{
   1.111 +    size_t len;
   1.112 +    char *new_variable;
   1.113 +
   1.114 +    if (getenv(name) != NULL) {
   1.115 +        if (overwrite) {
   1.116 +            unsetenv(name);
   1.117 +        } else {
   1.118 +            return 0;  /* leave the existing one there. */
   1.119 +        }
   1.120 +    }
   1.121 +
   1.122 +    /* This leaks. Sorry. Get a better OS so we don't have to do this. */
   1.123 +    len = SDL_strlen(name) + SDL_strlen(value) + 2;
   1.124 +    new_variable = (char *) SDL_malloc(len);
   1.125 +    if (!new_variable) {
   1.126 +        return (-1);
   1.127 +    }
   1.128 +
   1.129 +    SDL_snprintf(new_variable, len, "%s=%s", name, value);
   1.130 +    return putenv(new_variable);
   1.131 +}
   1.132 +#endif
   1.133 +
   1.134 +
   1.135  #ifdef TEST_MAIN
   1.136  #include <stdio.h>
   1.137