src/stdlib/SDL_getenv.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Aug 2017 21:30:06 -0700
changeset 11232 b26929d6ca20
parent 10737 3406a0f8b041
child 11653 4130b92b6be4
permissions -rw-r--r--
Fixed bug 3284 - minor correction for SDL_setenv on _WIN32__ platform

Coriiander

Here is a minor correction for a non-breaking mistake in SDL_setenv for __WIN32__ platform. See below for details.

FILE:
"SDL/src/stdlib/SDL_getenv.c"

FUNCTION: (__WIN32__ platform)
int SDL_setenv(const char *name, const char *value, int overwrite)

CODE:
if (!overwrite) {
char ch = 0;
const size_t len = GetEnvironmentVariableA(name, &ch, sizeof (ch));
if (len > 0) {
return 0; /* asked not to overwrite existing value. */
}
}


WHAT'S WRONG:
The 3th argument to GetEnvironmentVariable (being DWORD nSize) must be the number of characters, not the number of bytes. SDL currently passes "the size of 1 char", rather "1". While it is non-breaking (1=1 after all), it is incorrect. Furthermore there is no need to specify the 2nd and 3th arguments at all.

CORRECTION 1: (corrected argument_
if (!overwrite) {
char ch = 0;
const size_t len = GetEnvironmentVariableA(name, &ch, 1);
if (len > 0) {
return 0; /* asked not to overwrite existing value. */
}
}

CORRECTION 2: (stripped of unneeded code)
if (!overwrite) {
if (GetEnvironmentVariableA(name, NULL, 0) > 0) {
return 0; /* asked not to overwrite existing value. */
}
}
slouken@1330
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@1330
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1330
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1330
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1330
    20
*/
icculus@9306
    21
icculus@9306
    22
#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
icculus@9306
    23
#define SDL_DISABLE_ANALYZE_MACROS 1
icculus@9306
    24
#endif
icculus@9306
    25
icculus@8093
    26
#include "../SDL_internal.h"
slouken@1330
    27
slouken@7828
    28
#if defined(__WIN32__)
slouken@7828
    29
#include "../core/windows/SDL_windows.h"
slouken@7828
    30
#endif
slouken@7828
    31
slouken@1354
    32
#include "SDL_stdinc.h"
slouken@1330
    33
icculus@8092
    34
#if defined(__WIN32__) && (!defined(HAVE_SETENV) || !defined(HAVE_GETENV))
slouken@1330
    35
/* Note this isn't thread-safe! */
slouken@1895
    36
static char *SDL_envmem = NULL; /* Ugh, memory leak */
slouken@1456
    37
static size_t SDL_envmemlen = 0;
icculus@7003
    38
#endif
icculus@7003
    39
icculus@3581
    40
/* Put a variable into the environment */
aschiffler@8645
    41
/* Note: Name may not contain a '=' character. (Reference: http://www.unix.com/man-page/Linux/3/setenv/) */
slouken@7351
    42
#if defined(HAVE_SETENV)
slouken@7351
    43
int
slouken@7351
    44
SDL_setenv(const char *name, const char *value, int overwrite)
slouken@7351
    45
{
aschiffler@8645
    46
    /* Input validation */
aschiffler@8645
    47
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
aschiffler@8645
    48
        return (-1);
aschiffler@8645
    49
    }
aschiffler@8645
    50
    
slouken@7351
    51
    return setenv(name, value, overwrite);
slouken@7351
    52
}
icculus@7003
    53
#elif defined(__WIN32__)
slouken@1895
    54
int
icculus@3581
    55
SDL_setenv(const char *name, const char *value, int overwrite)
slouken@1330
    56
{
aschiffler@8645
    57
    /* Input validation */
aschiffler@8645
    58
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
aschiffler@8645
    59
        return (-1);
aschiffler@8645
    60
    }
aschiffler@8645
    61
    
icculus@3581
    62
    if (!overwrite) {
slouken@11232
    63
        if (GetEnvironmentVariableA(name, NULL, 0) > 0) {
icculus@3581
    64
            return 0;  /* asked not to overwrite existing value. */
icculus@3581
    65
        }
slouken@1895
    66
    }
slouken@5090
    67
    if (!SetEnvironmentVariableA(name, *value ? value : NULL)) {
slouken@1895
    68
        return -1;
slouken@1895
    69
    }
slouken@1895
    70
    return 0;
slouken@1330
    71
}
icculus@7003
    72
/* We have a real environment table, but no real setenv? Fake it w/ putenv. */
icculus@7003
    73
#elif (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
icculus@7003
    74
int
icculus@7003
    75
SDL_setenv(const char *name, const char *value, int overwrite)
icculus@7003
    76
{
icculus@7003
    77
    size_t len;
icculus@7003
    78
    char *new_variable;
slouken@1330
    79
aschiffler@8645
    80
    /* Input validation */
aschiffler@8645
    81
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
aschiffler@8645
    82
        return (-1);
aschiffler@8645
    83
    }
aschiffler@8645
    84
    
icculus@7003
    85
    if (getenv(name) != NULL) {
icculus@7003
    86
        if (overwrite) {
icculus@7003
    87
            unsetenv(name);
icculus@7003
    88
        } else {
icculus@7003
    89
            return 0;  /* leave the existing one there. */
icculus@7003
    90
        }
icculus@7003
    91
    }
slouken@1330
    92
icculus@7003
    93
    /* This leaks. Sorry. Get a better OS so we don't have to do this. */
icculus@7003
    94
    len = SDL_strlen(name) + SDL_strlen(value) + 2;
icculus@7003
    95
    new_variable = (char *) SDL_malloc(len);
icculus@7003
    96
    if (!new_variable) {
icculus@7003
    97
        return (-1);
slouken@1895
    98
    }
icculus@7003
    99
icculus@7003
   100
    SDL_snprintf(new_variable, len, "%s=%s", name, value);
icculus@7003
   101
    return putenv(new_variable);
slouken@1330
   102
}
slouken@1330
   103
#else /* roll our own */
slouken@1895
   104
static char **SDL_env = (char **) 0;
slouken@1895
   105
int
icculus@3581
   106
SDL_setenv(const char *name, const char *value, int overwrite)
slouken@1330
   107
{
slouken@1895
   108
    int added;
slouken@1895
   109
    int len, i;
slouken@1895
   110
    char **new_env;
slouken@1895
   111
    char *new_variable;
slouken@1330
   112
aschiffler@8645
   113
    /* Input validation */
aschiffler@8645
   114
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
slouken@1895
   115
        return (-1);
slouken@1895
   116
    }
slouken@1330
   117
slouken@3583
   118
    /* See if it already exists */
slouken@3583
   119
    if (!overwrite && SDL_getenv(name)) {
slouken@3583
   120
        return 0;
slouken@3583
   121
    }
slouken@3583
   122
slouken@1895
   123
    /* Allocate memory for the variable */
icculus@3581
   124
    len = SDL_strlen(name) + SDL_strlen(value) + 2;
icculus@3581
   125
    new_variable = (char *) SDL_malloc(len);
slouken@1895
   126
    if (!new_variable) {
slouken@1895
   127
        return (-1);
slouken@1895
   128
    }
slouken@1330
   129
icculus@3581
   130
    SDL_snprintf(new_variable, len, "%s=%s", name, value);
icculus@3581
   131
    value = new_variable + SDL_strlen(name) + 1;
icculus@3581
   132
    name = new_variable;
icculus@3581
   133
slouken@1895
   134
    /* Actually put it into the environment */
slouken@1895
   135
    added = 0;
slouken@1895
   136
    i = 0;
slouken@1895
   137
    if (SDL_env) {
slouken@1895
   138
        /* Check to see if it's already there... */
slouken@1895
   139
        len = (value - name);
slouken@1895
   140
        for (; SDL_env[i]; ++i) {
slouken@1895
   141
            if (SDL_strncmp(SDL_env[i], name, len) == 0) {
slouken@1895
   142
                break;
slouken@1895
   143
            }
slouken@1895
   144
        }
slouken@1895
   145
        /* If we found it, just replace the entry */
slouken@1895
   146
        if (SDL_env[i]) {
slouken@1895
   147
            SDL_free(SDL_env[i]);
slouken@1895
   148
            SDL_env[i] = new_variable;
slouken@1895
   149
            added = 1;
slouken@1895
   150
        }
slouken@1895
   151
    }
slouken@1330
   152
slouken@1895
   153
    /* Didn't find it in the environment, expand and add */
slouken@1895
   154
    if (!added) {
slouken@1895
   155
        new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
slouken@1895
   156
        if (new_env) {
slouken@1895
   157
            SDL_env = new_env;
slouken@1895
   158
            SDL_env[i++] = new_variable;
slouken@1895
   159
            SDL_env[i++] = (char *) 0;
slouken@1895
   160
            added = 1;
slouken@1895
   161
        } else {
slouken@1895
   162
            SDL_free(new_variable);
slouken@1895
   163
        }
slouken@1895
   164
    }
slouken@1895
   165
    return (added ? 0 : -1);
slouken@1330
   166
}
icculus@7003
   167
#endif
slouken@1330
   168
slouken@1330
   169
/* Retrieve a variable named "name" from the environment */
slouken@7351
   170
#if defined(HAVE_GETENV)
slouken@7351
   171
char *
slouken@7351
   172
SDL_getenv(const char *name)
slouken@7351
   173
{
aschiffler@8645
   174
    /* Input validation */
aschiffler@8645
   175
    if (!name || SDL_strlen(name)==0) {
aschiffler@8645
   176
        return NULL;
aschiffler@8645
   177
    }
aschiffler@8645
   178
slouken@7351
   179
    return getenv(name);
slouken@7351
   180
}
icculus@7003
   181
#elif defined(__WIN32__)
icculus@7003
   182
char *
icculus@7003
   183
SDL_getenv(const char *name)
icculus@7003
   184
{
icculus@7003
   185
    size_t bufferlen;
icculus@7003
   186
aschiffler@8645
   187
    /* Input validation */
aschiffler@8645
   188
    if (!name || SDL_strlen(name)==0) {
aschiffler@8645
   189
        return NULL;
aschiffler@8645
   190
    }
aschiffler@8645
   191
    
icculus@7003
   192
    bufferlen =
icculus@7003
   193
        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
icculus@7003
   194
    if (bufferlen == 0) {
icculus@7003
   195
        return NULL;
icculus@7003
   196
    }
icculus@7003
   197
    if (bufferlen > SDL_envmemlen) {
icculus@7003
   198
        char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
icculus@7003
   199
        if (newmem == NULL) {
icculus@7003
   200
            return NULL;
icculus@7003
   201
        }
icculus@7003
   202
        SDL_envmem = newmem;
icculus@7003
   203
        SDL_envmemlen = bufferlen;
icculus@7003
   204
        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
icculus@7003
   205
    }
icculus@7003
   206
    return SDL_envmem;
icculus@7003
   207
}
icculus@7003
   208
#else
slouken@1895
   209
char *
slouken@1895
   210
SDL_getenv(const char *name)
slouken@1330
   211
{
slouken@1895
   212
    int len, i;
slouken@1895
   213
    char *value;
slouken@1330
   214
aschiffler@8645
   215
    /* Input validation */
aschiffler@8645
   216
    if (!name || SDL_strlen(name)==0) {
aschiffler@8645
   217
        return NULL;
aschiffler@8645
   218
    }
aschiffler@8645
   219
    
slouken@1895
   220
    value = (char *) 0;
slouken@1895
   221
    if (SDL_env) {
slouken@1895
   222
        len = SDL_strlen(name);
slouken@1895
   223
        for (i = 0; SDL_env[i] && !value; ++i) {
slouken@1895
   224
            if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
slouken@1895
   225
                (SDL_env[i][len] == '=')) {
slouken@1895
   226
                value = &SDL_env[i][len + 1];
slouken@1895
   227
            }
slouken@1895
   228
        }
slouken@1895
   229
    }
slouken@1895
   230
    return value;
slouken@1330
   231
}
icculus@3581
   232
#endif
icculus@3581
   233
icculus@3581
   234
slouken@1330
   235
#ifdef TEST_MAIN
slouken@1330
   236
#include <stdio.h>
slouken@1330
   237
slouken@1895
   238
int
slouken@1895
   239
main(int argc, char *argv[])
slouken@1330
   240
{
slouken@1895
   241
    char *value;
slouken@1330
   242
slouken@1895
   243
    printf("Checking for non-existent variable... ");
slouken@1895
   244
    fflush(stdout);
slouken@1895
   245
    if (!SDL_getenv("EXISTS")) {
slouken@1895
   246
        printf("okay\n");
slouken@1895
   247
    } else {
slouken@1895
   248
        printf("failed\n");
slouken@1895
   249
    }
slouken@1895
   250
    printf("Setting FIRST=VALUE1 in the environment... ");
slouken@1895
   251
    fflush(stdout);
slouken@3591
   252
    if (SDL_setenv("FIRST", "VALUE1", 0) == 0) {
slouken@1895
   253
        printf("okay\n");
slouken@1895
   254
    } else {
slouken@1895
   255
        printf("failed\n");
slouken@1895
   256
    }
slouken@1895
   257
    printf("Getting FIRST from the environment... ");
slouken@1895
   258
    fflush(stdout);
slouken@1895
   259
    value = SDL_getenv("FIRST");
slouken@1895
   260
    if (value && (SDL_strcmp(value, "VALUE1") == 0)) {
slouken@1895
   261
        printf("okay\n");
slouken@1895
   262
    } else {
slouken@1895
   263
        printf("failed\n");
slouken@1895
   264
    }
slouken@1895
   265
    printf("Setting SECOND=VALUE2 in the environment... ");
slouken@1895
   266
    fflush(stdout);
slouken@3591
   267
    if (SDL_setenv("SECOND", "VALUE2", 0) == 0) {
slouken@1895
   268
        printf("okay\n");
slouken@1895
   269
    } else {
slouken@1895
   270
        printf("failed\n");
slouken@1895
   271
    }
slouken@1895
   272
    printf("Getting SECOND from the environment... ");
slouken@1895
   273
    fflush(stdout);
slouken@1895
   274
    value = SDL_getenv("SECOND");
slouken@1895
   275
    if (value && (SDL_strcmp(value, "VALUE2") == 0)) {
slouken@1895
   276
        printf("okay\n");
slouken@1895
   277
    } else {
slouken@1895
   278
        printf("failed\n");
slouken@1895
   279
    }
slouken@1895
   280
    printf("Setting FIRST=NOVALUE in the environment... ");
slouken@1895
   281
    fflush(stdout);
slouken@3591
   282
    if (SDL_setenv("FIRST", "NOVALUE", 1) == 0) {
slouken@1895
   283
        printf("okay\n");
slouken@1895
   284
    } else {
slouken@1895
   285
        printf("failed\n");
slouken@1895
   286
    }
slouken@1895
   287
    printf("Getting FIRST from the environment... ");
slouken@1895
   288
    fflush(stdout);
slouken@1895
   289
    value = SDL_getenv("FIRST");
slouken@1895
   290
    if (value && (SDL_strcmp(value, "NOVALUE") == 0)) {
slouken@1895
   291
        printf("okay\n");
slouken@1895
   292
    } else {
slouken@1895
   293
        printf("failed\n");
slouken@1895
   294
    }
slouken@1895
   295
    printf("Checking for non-existent variable... ");
slouken@1895
   296
    fflush(stdout);
slouken@1895
   297
    if (!SDL_getenv("EXISTS")) {
slouken@1895
   298
        printf("okay\n");
slouken@1895
   299
    } else {
slouken@1895
   300
        printf("failed\n");
slouken@1895
   301
    }
slouken@1895
   302
    return (0);
slouken@1330
   303
}
slouken@1330
   304
#endif /* TEST_MAIN */
slouken@3591
   305
slouken@1895
   306
/* vi: set ts=4 sw=4 expandtab: */