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