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