src/stdlib/SDL_getenv.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 09 Dec 2013 13:30:35 -0500
changeset 8092 46fb898f098d
parent 7828 1451063c8ecd
child 8093 b43765095a6f
permissions -rw-r--r--
Patched stdlib changes to compile on Windows.
slouken@1330
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
slouken@1402
    21
#include "SDL_config.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 */
slouken@7351
    36
#if defined(HAVE_SETENV)
slouken@7351
    37
int
slouken@7351
    38
SDL_setenv(const char *name, const char *value, int overwrite)
slouken@7351
    39
{
slouken@7351
    40
    return setenv(name, value, overwrite);
slouken@7351
    41
}
icculus@7003
    42
#elif defined(__WIN32__)
slouken@1895
    43
int
icculus@3581
    44
SDL_setenv(const char *name, const char *value, int overwrite)
slouken@1330
    45
{
icculus@3581
    46
    if (!overwrite) {
icculus@3581
    47
        char ch = 0;
slouken@5090
    48
        const size_t len = GetEnvironmentVariableA(name, &ch, sizeof (ch));
icculus@3581
    49
        if (len > 0) {
icculus@3581
    50
            return 0;  /* asked not to overwrite existing value. */
icculus@3581
    51
        }
slouken@1895
    52
    }
slouken@5090
    53
    if (!SetEnvironmentVariableA(name, *value ? value : NULL)) {
slouken@1895
    54
        return -1;
slouken@1895
    55
    }
slouken@1895
    56
    return 0;
slouken@1330
    57
}
icculus@7003
    58
/* We have a real environment table, but no real setenv? Fake it w/ putenv. */
icculus@7003
    59
#elif (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
icculus@7003
    60
int
icculus@7003
    61
SDL_setenv(const char *name, const char *value, int overwrite)
icculus@7003
    62
{
icculus@7003
    63
    size_t len;
icculus@7003
    64
    char *new_variable;
slouken@1330
    65
icculus@7003
    66
    if (getenv(name) != NULL) {
icculus@7003
    67
        if (overwrite) {
icculus@7003
    68
            unsetenv(name);
icculus@7003
    69
        } else {
icculus@7003
    70
            return 0;  /* leave the existing one there. */
icculus@7003
    71
        }
icculus@7003
    72
    }
slouken@1330
    73
icculus@7003
    74
    /* This leaks. Sorry. Get a better OS so we don't have to do this. */
icculus@7003
    75
    len = SDL_strlen(name) + SDL_strlen(value) + 2;
icculus@7003
    76
    new_variable = (char *) SDL_malloc(len);
icculus@7003
    77
    if (!new_variable) {
icculus@7003
    78
        return (-1);
slouken@1895
    79
    }
icculus@7003
    80
icculus@7003
    81
    SDL_snprintf(new_variable, len, "%s=%s", name, value);
icculus@7003
    82
    return putenv(new_variable);
slouken@1330
    83
}
slouken@1330
    84
#else /* roll our own */
slouken@1895
    85
static char **SDL_env = (char **) 0;
slouken@1895
    86
int
icculus@3581
    87
SDL_setenv(const char *name, const char *value, int overwrite)
slouken@1330
    88
{
slouken@1895
    89
    int added;
slouken@1895
    90
    int len, i;
slouken@1895
    91
    char **new_env;
slouken@1895
    92
    char *new_variable;
slouken@1330
    93
slouken@1895
    94
    /* A little error checking */
icculus@3581
    95
    if (!name || !value) {
slouken@1895
    96
        return (-1);
slouken@1895
    97
    }
slouken@1330
    98
slouken@3583
    99
    /* See if it already exists */
slouken@3583
   100
    if (!overwrite && SDL_getenv(name)) {
slouken@3583
   101
        return 0;
slouken@3583
   102
    }
slouken@3583
   103
slouken@1895
   104
    /* Allocate memory for the variable */
icculus@3581
   105
    len = SDL_strlen(name) + SDL_strlen(value) + 2;
icculus@3581
   106
    new_variable = (char *) SDL_malloc(len);
slouken@1895
   107
    if (!new_variable) {
slouken@1895
   108
        return (-1);
slouken@1895
   109
    }
slouken@1330
   110
icculus@3581
   111
    SDL_snprintf(new_variable, len, "%s=%s", name, value);
icculus@3581
   112
    value = new_variable + SDL_strlen(name) + 1;
icculus@3581
   113
    name = new_variable;
icculus@3581
   114
slouken@1895
   115
    /* Actually put it into the environment */
slouken@1895
   116
    added = 0;
slouken@1895
   117
    i = 0;
slouken@1895
   118
    if (SDL_env) {
slouken@1895
   119
        /* Check to see if it's already there... */
slouken@1895
   120
        len = (value - name);
slouken@1895
   121
        for (; SDL_env[i]; ++i) {
slouken@1895
   122
            if (SDL_strncmp(SDL_env[i], name, len) == 0) {
slouken@1895
   123
                break;
slouken@1895
   124
            }
slouken@1895
   125
        }
slouken@1895
   126
        /* If we found it, just replace the entry */
slouken@1895
   127
        if (SDL_env[i]) {
slouken@1895
   128
            SDL_free(SDL_env[i]);
slouken@1895
   129
            SDL_env[i] = new_variable;
slouken@1895
   130
            added = 1;
slouken@1895
   131
        }
slouken@1895
   132
    }
slouken@1330
   133
slouken@1895
   134
    /* Didn't find it in the environment, expand and add */
slouken@1895
   135
    if (!added) {
slouken@1895
   136
        new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
slouken@1895
   137
        if (new_env) {
slouken@1895
   138
            SDL_env = new_env;
slouken@1895
   139
            SDL_env[i++] = new_variable;
slouken@1895
   140
            SDL_env[i++] = (char *) 0;
slouken@1895
   141
            added = 1;
slouken@1895
   142
        } else {
slouken@1895
   143
            SDL_free(new_variable);
slouken@1895
   144
        }
slouken@1895
   145
    }
slouken@1895
   146
    return (added ? 0 : -1);
slouken@1330
   147
}
icculus@7003
   148
#endif
slouken@1330
   149
slouken@1330
   150
/* Retrieve a variable named "name" from the environment */
slouken@7351
   151
#if defined(HAVE_GETENV)
slouken@7351
   152
char *
slouken@7351
   153
SDL_getenv(const char *name)
slouken@7351
   154
{
slouken@7351
   155
    return getenv(name);
slouken@7351
   156
}
icculus@7003
   157
#elif defined(__WIN32__)
icculus@7003
   158
char *
icculus@7003
   159
SDL_getenv(const char *name)
icculus@7003
   160
{
icculus@7003
   161
    size_t bufferlen;
icculus@7003
   162
icculus@7003
   163
    bufferlen =
icculus@7003
   164
        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
icculus@7003
   165
    if (bufferlen == 0) {
icculus@7003
   166
        return NULL;
icculus@7003
   167
    }
icculus@7003
   168
    if (bufferlen > SDL_envmemlen) {
icculus@7003
   169
        char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
icculus@7003
   170
        if (newmem == NULL) {
icculus@7003
   171
            return NULL;
icculus@7003
   172
        }
icculus@7003
   173
        SDL_envmem = newmem;
icculus@7003
   174
        SDL_envmemlen = bufferlen;
icculus@7003
   175
        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
icculus@7003
   176
    }
icculus@7003
   177
    return SDL_envmem;
icculus@7003
   178
}
icculus@7003
   179
#else
slouken@1895
   180
char *
slouken@1895
   181
SDL_getenv(const char *name)
slouken@1330
   182
{
slouken@1895
   183
    int len, i;
slouken@1895
   184
    char *value;
slouken@1330
   185
slouken@1895
   186
    value = (char *) 0;
slouken@1895
   187
    if (SDL_env) {
slouken@1895
   188
        len = SDL_strlen(name);
slouken@1895
   189
        for (i = 0; SDL_env[i] && !value; ++i) {
slouken@1895
   190
            if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
slouken@1895
   191
                (SDL_env[i][len] == '=')) {
slouken@1895
   192
                value = &SDL_env[i][len + 1];
slouken@1895
   193
            }
slouken@1895
   194
        }
slouken@1895
   195
    }
slouken@1895
   196
    return value;
slouken@1330
   197
}
icculus@3581
   198
#endif
icculus@3581
   199
icculus@3581
   200
slouken@1330
   201
#ifdef TEST_MAIN
slouken@1330
   202
#include <stdio.h>
slouken@1330
   203
slouken@1895
   204
int
slouken@1895
   205
main(int argc, char *argv[])
slouken@1330
   206
{
slouken@1895
   207
    char *value;
slouken@1330
   208
slouken@1895
   209
    printf("Checking for non-existent variable... ");
slouken@1895
   210
    fflush(stdout);
slouken@1895
   211
    if (!SDL_getenv("EXISTS")) {
slouken@1895
   212
        printf("okay\n");
slouken@1895
   213
    } else {
slouken@1895
   214
        printf("failed\n");
slouken@1895
   215
    }
slouken@1895
   216
    printf("Setting FIRST=VALUE1 in the environment... ");
slouken@1895
   217
    fflush(stdout);
slouken@3591
   218
    if (SDL_setenv("FIRST", "VALUE1", 0) == 0) {
slouken@1895
   219
        printf("okay\n");
slouken@1895
   220
    } else {
slouken@1895
   221
        printf("failed\n");
slouken@1895
   222
    }
slouken@1895
   223
    printf("Getting FIRST from the environment... ");
slouken@1895
   224
    fflush(stdout);
slouken@1895
   225
    value = SDL_getenv("FIRST");
slouken@1895
   226
    if (value && (SDL_strcmp(value, "VALUE1") == 0)) {
slouken@1895
   227
        printf("okay\n");
slouken@1895
   228
    } else {
slouken@1895
   229
        printf("failed\n");
slouken@1895
   230
    }
slouken@1895
   231
    printf("Setting SECOND=VALUE2 in the environment... ");
slouken@1895
   232
    fflush(stdout);
slouken@3591
   233
    if (SDL_setenv("SECOND", "VALUE2", 0) == 0) {
slouken@1895
   234
        printf("okay\n");
slouken@1895
   235
    } else {
slouken@1895
   236
        printf("failed\n");
slouken@1895
   237
    }
slouken@1895
   238
    printf("Getting SECOND from the environment... ");
slouken@1895
   239
    fflush(stdout);
slouken@1895
   240
    value = SDL_getenv("SECOND");
slouken@1895
   241
    if (value && (SDL_strcmp(value, "VALUE2") == 0)) {
slouken@1895
   242
        printf("okay\n");
slouken@1895
   243
    } else {
slouken@1895
   244
        printf("failed\n");
slouken@1895
   245
    }
slouken@1895
   246
    printf("Setting FIRST=NOVALUE in the environment... ");
slouken@1895
   247
    fflush(stdout);
slouken@3591
   248
    if (SDL_setenv("FIRST", "NOVALUE", 1) == 0) {
slouken@1895
   249
        printf("okay\n");
slouken@1895
   250
    } else {
slouken@1895
   251
        printf("failed\n");
slouken@1895
   252
    }
slouken@1895
   253
    printf("Getting FIRST from the environment... ");
slouken@1895
   254
    fflush(stdout);
slouken@1895
   255
    value = SDL_getenv("FIRST");
slouken@1895
   256
    if (value && (SDL_strcmp(value, "NOVALUE") == 0)) {
slouken@1895
   257
        printf("okay\n");
slouken@1895
   258
    } else {
slouken@1895
   259
        printf("failed\n");
slouken@1895
   260
    }
slouken@1895
   261
    printf("Checking for non-existent variable... ");
slouken@1895
   262
    fflush(stdout);
slouken@1895
   263
    if (!SDL_getenv("EXISTS")) {
slouken@1895
   264
        printf("okay\n");
slouken@1895
   265
    } else {
slouken@1895
   266
        printf("failed\n");
slouken@1895
   267
    }
slouken@1895
   268
    return (0);
slouken@1330
   269
}
slouken@1330
   270
#endif /* TEST_MAIN */
slouken@3591
   271
slouken@1895
   272
/* vi: set ts=4 sw=4 expandtab: */