src/stdlib/SDL_getenv.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 09 Apr 2015 22:33:39 -0400
branchiOS-improvements
changeset 9542 618662dc9e82
parent 9306 817656bd36ec
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Closing the iOS-improvements branch.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
9306
817656bd36ec Clang static analysis builds should use C runtime directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 8645
diff changeset
    21
817656bd36ec Clang static analysis builds should use C runtime directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 8645
diff changeset
    22
#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
817656bd36ec Clang static analysis builds should use C runtime directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 8645
diff changeset
    23
#define SDL_DISABLE_ANALYZE_MACROS 1
817656bd36ec Clang static analysis builds should use C runtime directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 8645
diff changeset
    24
#endif
817656bd36ec Clang static analysis builds should use C runtime directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 8645
diff changeset
    25
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 8092
diff changeset
    26
#include "../SDL_internal.h"
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
7828
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7351
diff changeset
    28
#if defined(__WIN32__)
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7351
diff changeset
    29
#include "../core/windows/SDL_windows.h"
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7351
diff changeset
    30
#endif
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7351
diff changeset
    31
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1341
diff changeset
    32
#include "SDL_stdinc.h"
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
8092
46fb898f098d Patched stdlib changes to compile on Windows.
Ryan C. Gordon <icculus@icculus.org>
parents: 7828
diff changeset
    34
#if defined(__WIN32__) && (!defined(HAVE_SETENV) || !defined(HAVE_GETENV))
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
/* Note this isn't thread-safe! */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
    36
static char *SDL_envmem = NULL; /* Ugh, memory leak */
1456
84de7511f79f Fixed a bunch of 64-bit compatibility problems
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    37
static size_t SDL_envmemlen = 0;
7003
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    38
#endif
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    39
3581
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
    40
/* Put a variable into the environment */
8645
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    41
/* Note: Name may not contain a '=' character. (Reference: http://www.unix.com/man-page/Linux/3/setenv/) */
7351
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
    42
#if defined(HAVE_SETENV)
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
    43
int
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
    44
SDL_setenv(const char *name, const char *value, int overwrite)
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
    45
{
8645
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    46
    /* Input validation */
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    47
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    48
        return (-1);
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    49
    }
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    50
    
7351
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
    51
    return setenv(name, value, overwrite);
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
    52
}
7003
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    53
#elif defined(__WIN32__)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
    54
int
3581
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
    55
SDL_setenv(const char *name, const char *value, int overwrite)
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
{
8645
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    57
    /* Input validation */
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    58
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    59
        return (-1);
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    60
    }
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    61
    
3581
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
    62
    if (!overwrite) {
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
    63
        char ch = 0;
5090
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    64
        const size_t len = GetEnvironmentVariableA(name, &ch, sizeof (ch));
3581
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
    65
        if (len > 0) {
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
    66
            return 0;  /* asked not to overwrite existing value. */
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
    67
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
    68
    }
5090
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    69
    if (!SetEnvironmentVariableA(name, *value ? value : NULL)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
    70
        return -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
    71
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
    72
    return 0;
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
}
7003
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    74
/* We have a real environment table, but no real setenv? Fake it w/ putenv. */
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    75
#elif (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    76
int
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    77
SDL_setenv(const char *name, const char *value, int overwrite)
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
{
7003
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    79
    size_t len;
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    80
    char *new_variable;
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
8645
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    82
    /* Input validation */
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    83
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    84
        return (-1);
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    85
    }
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
    86
    
7003
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    87
    if (getenv(name) != NULL) {
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    88
        if (overwrite) {
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    89
            unsetenv(name);
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    90
        } else {
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    91
            return 0;  /* leave the existing one there. */
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    92
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
    93
    }
7003
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    94
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    95
    /* This leaks. Sorry. Get a better OS so we don't have to do this. */
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    96
    len = SDL_strlen(name) + SDL_strlen(value) + 2;
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    97
    new_variable = (char *) SDL_malloc(len);
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    98
    if (!new_variable) {
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    99
        return (-1);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   100
    }
7003
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   101
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   102
    SDL_snprintf(new_variable, len, "%s=%s", name, value);
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   103
    return putenv(new_variable);
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
}
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
#else /* roll our own */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   106
static char **SDL_env = (char **) 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   107
int
3581
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   108
SDL_setenv(const char *name, const char *value, int overwrite)
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   110
    int added;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   111
    int len, i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   112
    char **new_env;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   113
    char *new_variable;
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
8645
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   115
    /* Input validation */
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   116
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   117
        return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   118
    }
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
3583
65f66674b7fb Allocate memory only if we're going to overwrite an environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 3581
diff changeset
   120
    /* See if it already exists */
65f66674b7fb Allocate memory only if we're going to overwrite an environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 3581
diff changeset
   121
    if (!overwrite && SDL_getenv(name)) {
65f66674b7fb Allocate memory only if we're going to overwrite an environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 3581
diff changeset
   122
        return 0;
65f66674b7fb Allocate memory only if we're going to overwrite an environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 3581
diff changeset
   123
    }
65f66674b7fb Allocate memory only if we're going to overwrite an environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 3581
diff changeset
   124
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   125
    /* Allocate memory for the variable */
3581
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   126
    len = SDL_strlen(name) + SDL_strlen(value) + 2;
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   127
    new_variable = (char *) SDL_malloc(len);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   128
    if (!new_variable) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   129
        return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   130
    }
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
3581
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   132
    SDL_snprintf(new_variable, len, "%s=%s", name, value);
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   133
    value = new_variable + SDL_strlen(name) + 1;
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   134
    name = new_variable;
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   135
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   136
    /* Actually put it into the environment */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   137
    added = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   138
    i = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   139
    if (SDL_env) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   140
        /* Check to see if it's already there... */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   141
        len = (value - name);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   142
        for (; SDL_env[i]; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   143
            if (SDL_strncmp(SDL_env[i], name, len) == 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   144
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   145
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   146
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   147
        /* If we found it, just replace the entry */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   148
        if (SDL_env[i]) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   149
            SDL_free(SDL_env[i]);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   150
            SDL_env[i] = new_variable;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   151
            added = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   152
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   153
    }
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   155
    /* Didn't find it in the environment, expand and add */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   156
    if (!added) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   157
        new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   158
        if (new_env) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   159
            SDL_env = new_env;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   160
            SDL_env[i++] = new_variable;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   161
            SDL_env[i++] = (char *) 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   162
            added = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   163
        } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   164
            SDL_free(new_variable);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   165
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   166
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   167
    return (added ? 0 : -1);
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
}
7003
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   169
#endif
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
/* Retrieve a variable named "name" from the environment */
7351
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
   172
#if defined(HAVE_GETENV)
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
   173
char *
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
   174
SDL_getenv(const char *name)
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
   175
{
8645
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   176
    /* Input validation */
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   177
    if (!name || SDL_strlen(name)==0) {
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   178
        return NULL;
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   179
    }
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   180
7351
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
   181
    return getenv(name);
668a3dc28361 Removed the inline functions from SDL_stdinc.h
Sam Lantinga <slouken@libsdl.org>
parents: 7003
diff changeset
   182
}
7003
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   183
#elif defined(__WIN32__)
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   184
char *
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   185
SDL_getenv(const char *name)
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   186
{
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   187
    size_t bufferlen;
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   188
8645
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   189
    /* Input validation */
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   190
    if (!name || SDL_strlen(name)==0) {
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   191
        return NULL;
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   192
    }
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   193
    
7003
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   194
    bufferlen =
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   195
        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   196
    if (bufferlen == 0) {
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   197
        return NULL;
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   198
    }
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   199
    if (bufferlen > SDL_envmemlen) {
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   200
        char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   201
        if (newmem == NULL) {
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   202
            return NULL;
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   203
        }
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   204
        SDL_envmem = newmem;
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   205
        SDL_envmemlen = bufferlen;
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   206
        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   207
    }
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   208
    return SDL_envmem;
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   209
}
eeaf77005c30 Improvements to stdlib.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   210
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   211
char *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   212
SDL_getenv(const char *name)
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   214
    int len, i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   215
    char *value;
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   216
8645
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   217
    /* Input validation */
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   218
    if (!name || SDL_strlen(name)==0) {
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   219
        return NULL;
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   220
    }
d69fdbefeecf Add input validation to SDL_getenv/SDL_setenv; update Stdlib testsuite; add Hints testsuite
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 8149
diff changeset
   221
    
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   222
    value = (char *) 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   223
    if (SDL_env) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   224
        len = SDL_strlen(name);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   225
        for (i = 0; SDL_env[i] && !value; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   226
            if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   227
                (SDL_env[i][len] == '=')) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   228
                value = &SDL_env[i][len + 1];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   229
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   230
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   231
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   232
    return value;
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
}
3581
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   234
#endif
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   235
15eea7a1fa97 Implemented SDL_setenv(), moved SDL_putenv() to compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 2859
diff changeset
   236
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
#ifdef TEST_MAIN
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
#include <stdio.h>
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   240
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   241
main(int argc, char *argv[])
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   243
    char *value;
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   244
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   245
    printf("Checking for non-existent variable... ");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   246
    fflush(stdout);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   247
    if (!SDL_getenv("EXISTS")) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   248
        printf("okay\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   249
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   250
        printf("failed\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   251
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   252
    printf("Setting FIRST=VALUE1 in the environment... ");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   253
    fflush(stdout);
3591
db4af3cb938e Updated test code to use SDL_setenv()
Sam Lantinga <slouken@libsdl.org>
parents: 3583
diff changeset
   254
    if (SDL_setenv("FIRST", "VALUE1", 0) == 0) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   255
        printf("okay\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   256
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   257
        printf("failed\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   258
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   259
    printf("Getting FIRST from the environment... ");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   260
    fflush(stdout);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   261
    value = SDL_getenv("FIRST");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   262
    if (value && (SDL_strcmp(value, "VALUE1") == 0)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   263
        printf("okay\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   264
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   265
        printf("failed\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   266
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   267
    printf("Setting SECOND=VALUE2 in the environment... ");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   268
    fflush(stdout);
3591
db4af3cb938e Updated test code to use SDL_setenv()
Sam Lantinga <slouken@libsdl.org>
parents: 3583
diff changeset
   269
    if (SDL_setenv("SECOND", "VALUE2", 0) == 0) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   270
        printf("okay\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   271
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   272
        printf("failed\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   273
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   274
    printf("Getting SECOND from the environment... ");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   275
    fflush(stdout);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   276
    value = SDL_getenv("SECOND");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   277
    if (value && (SDL_strcmp(value, "VALUE2") == 0)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   278
        printf("okay\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   279
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   280
        printf("failed\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   281
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   282
    printf("Setting FIRST=NOVALUE in the environment... ");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   283
    fflush(stdout);
3591
db4af3cb938e Updated test code to use SDL_setenv()
Sam Lantinga <slouken@libsdl.org>
parents: 3583
diff changeset
   284
    if (SDL_setenv("FIRST", "NOVALUE", 1) == 0) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   285
        printf("okay\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   286
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   287
        printf("failed\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   288
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   289
    printf("Getting FIRST from the environment... ");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   290
    fflush(stdout);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   291
    value = SDL_getenv("FIRST");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   292
    if (value && (SDL_strcmp(value, "NOVALUE") == 0)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   293
        printf("okay\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   294
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   295
        printf("failed\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   296
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   297
    printf("Checking for non-existent variable... ");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   298
    fflush(stdout);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   299
    if (!SDL_getenv("EXISTS")) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   300
        printf("okay\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   301
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   302
        printf("failed\n");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   303
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   304
    return (0);
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   305
}
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   306
#endif /* TEST_MAIN */
3591
db4af3cb938e Updated test code to use SDL_setenv()
Sam Lantinga <slouken@libsdl.org>
parents: 3583
diff changeset
   307
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1472
diff changeset
   308
/* vi: set ts=4 sw=4 expandtab: */