src/stdlib/SDL_getenv.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 21 Apr 2016 03:16:44 -0400
changeset 11729 d1ce8396c356
parent 11678 1f10a52295e3
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Initial shot at a renderer target for Apple's Metal API.

This isn't complete, but is enough to run testsprite2. It's currently
Mac-only; with a little work to figure out how to properly glue in a Metal
layer to a UIView, this will likely work on iOS, too.

This is only wired up to the configure script right now, and disabled by
default. CMake and Xcode still need their bits filled in as appropriate.
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@11678
   174
#if defined(HAVE_GETENV)
slouken@11653
   175
char *
slouken@11653
   176
SDL_getenv(const char *name)
slouken@11653
   177
{
slouken@11678
   178
#if defined(__ANDROID__)
slouken@11678
   179
    /* Make sure variables from the application manifest are available */
slouken@11678
   180
    Android_JNI_GetManifestEnvironmentVariables();
slouken@11678
   181
#endif
slouken@11678
   182
slouken@11653
   183
    /* Input validation */
slouken@11678
   184
    if (!name || !*name) {
aschiffler@8645
   185
        return NULL;
aschiffler@8645
   186
    }
aschiffler@8645
   187
slouken@7351
   188
    return getenv(name);
slouken@7351
   189
}
icculus@7003
   190
#elif defined(__WIN32__)
icculus@7003
   191
char *
icculus@7003
   192
SDL_getenv(const char *name)
icculus@7003
   193
{
icculus@7003
   194
    size_t bufferlen;
icculus@7003
   195
aschiffler@8645
   196
    /* Input validation */
aschiffler@8645
   197
    if (!name || SDL_strlen(name)==0) {
aschiffler@8645
   198
        return NULL;
aschiffler@8645
   199
    }
aschiffler@8645
   200
    
icculus@7003
   201
    bufferlen =
icculus@7003
   202
        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
icculus@7003
   203
    if (bufferlen == 0) {
icculus@7003
   204
        return NULL;
icculus@7003
   205
    }
icculus@7003
   206
    if (bufferlen > SDL_envmemlen) {
icculus@7003
   207
        char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
icculus@7003
   208
        if (newmem == NULL) {
icculus@7003
   209
            return NULL;
icculus@7003
   210
        }
icculus@7003
   211
        SDL_envmem = newmem;
icculus@7003
   212
        SDL_envmemlen = bufferlen;
icculus@7003
   213
        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
icculus@7003
   214
    }
icculus@7003
   215
    return SDL_envmem;
icculus@7003
   216
}
icculus@7003
   217
#else
slouken@1895
   218
char *
slouken@1895
   219
SDL_getenv(const char *name)
slouken@1330
   220
{
slouken@1895
   221
    int len, i;
slouken@1895
   222
    char *value;
slouken@1330
   223
aschiffler@8645
   224
    /* Input validation */
aschiffler@8645
   225
    if (!name || SDL_strlen(name)==0) {
aschiffler@8645
   226
        return NULL;
aschiffler@8645
   227
    }
aschiffler@8645
   228
    
slouken@1895
   229
    value = (char *) 0;
slouken@1895
   230
    if (SDL_env) {
slouken@1895
   231
        len = SDL_strlen(name);
slouken@1895
   232
        for (i = 0; SDL_env[i] && !value; ++i) {
slouken@1895
   233
            if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
slouken@1895
   234
                (SDL_env[i][len] == '=')) {
slouken@1895
   235
                value = &SDL_env[i][len + 1];
slouken@1895
   236
            }
slouken@1895
   237
        }
slouken@1895
   238
    }
slouken@1895
   239
    return value;
slouken@1330
   240
}
icculus@3581
   241
#endif
icculus@3581
   242
icculus@3581
   243
slouken@1330
   244
#ifdef TEST_MAIN
slouken@1330
   245
#include <stdio.h>
slouken@1330
   246
slouken@1895
   247
int
slouken@1895
   248
main(int argc, char *argv[])
slouken@1330
   249
{
slouken@1895
   250
    char *value;
slouken@1330
   251
slouken@1895
   252
    printf("Checking for non-existent variable... ");
slouken@1895
   253
    fflush(stdout);
slouken@1895
   254
    if (!SDL_getenv("EXISTS")) {
slouken@1895
   255
        printf("okay\n");
slouken@1895
   256
    } else {
slouken@1895
   257
        printf("failed\n");
slouken@1895
   258
    }
slouken@1895
   259
    printf("Setting FIRST=VALUE1 in the environment... ");
slouken@1895
   260
    fflush(stdout);
slouken@3591
   261
    if (SDL_setenv("FIRST", "VALUE1", 0) == 0) {
slouken@1895
   262
        printf("okay\n");
slouken@1895
   263
    } else {
slouken@1895
   264
        printf("failed\n");
slouken@1895
   265
    }
slouken@1895
   266
    printf("Getting FIRST from the environment... ");
slouken@1895
   267
    fflush(stdout);
slouken@1895
   268
    value = SDL_getenv("FIRST");
slouken@1895
   269
    if (value && (SDL_strcmp(value, "VALUE1") == 0)) {
slouken@1895
   270
        printf("okay\n");
slouken@1895
   271
    } else {
slouken@1895
   272
        printf("failed\n");
slouken@1895
   273
    }
slouken@1895
   274
    printf("Setting SECOND=VALUE2 in the environment... ");
slouken@1895
   275
    fflush(stdout);
slouken@3591
   276
    if (SDL_setenv("SECOND", "VALUE2", 0) == 0) {
slouken@1895
   277
        printf("okay\n");
slouken@1895
   278
    } else {
slouken@1895
   279
        printf("failed\n");
slouken@1895
   280
    }
slouken@1895
   281
    printf("Getting SECOND from the environment... ");
slouken@1895
   282
    fflush(stdout);
slouken@1895
   283
    value = SDL_getenv("SECOND");
slouken@1895
   284
    if (value && (SDL_strcmp(value, "VALUE2") == 0)) {
slouken@1895
   285
        printf("okay\n");
slouken@1895
   286
    } else {
slouken@1895
   287
        printf("failed\n");
slouken@1895
   288
    }
slouken@1895
   289
    printf("Setting FIRST=NOVALUE in the environment... ");
slouken@1895
   290
    fflush(stdout);
slouken@3591
   291
    if (SDL_setenv("FIRST", "NOVALUE", 1) == 0) {
slouken@1895
   292
        printf("okay\n");
slouken@1895
   293
    } else {
slouken@1895
   294
        printf("failed\n");
slouken@1895
   295
    }
slouken@1895
   296
    printf("Getting FIRST from the environment... ");
slouken@1895
   297
    fflush(stdout);
slouken@1895
   298
    value = SDL_getenv("FIRST");
slouken@1895
   299
    if (value && (SDL_strcmp(value, "NOVALUE") == 0)) {
slouken@1895
   300
        printf("okay\n");
slouken@1895
   301
    } else {
slouken@1895
   302
        printf("failed\n");
slouken@1895
   303
    }
slouken@1895
   304
    printf("Checking for non-existent variable... ");
slouken@1895
   305
    fflush(stdout);
slouken@1895
   306
    if (!SDL_getenv("EXISTS")) {
slouken@1895
   307
        printf("okay\n");
slouken@1895
   308
    } else {
slouken@1895
   309
        printf("failed\n");
slouken@1895
   310
    }
slouken@1895
   311
    return (0);
slouken@1330
   312
}
slouken@1330
   313
#endif /* TEST_MAIN */
slouken@3591
   314
slouken@1895
   315
/* vi: set ts=4 sw=4 expandtab: */