src/stdlib/SDL_getenv.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1472 4aac8563c296
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
slouken@1330
     1
/*
slouken@1330
     2
    SDL - Simple DirectMedia Layer
slouken@1330
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1330
     4
slouken@1330
     5
    This library is free software; you can redistribute it and/or
slouken@1330
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1330
     7
    License as published by the Free Software Foundation; either
slouken@1330
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1330
     9
slouken@1330
    10
    This library is distributed in the hope that it will be useful,
slouken@1330
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1330
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1330
    13
    Lesser General Public License for more details.
slouken@1330
    14
slouken@1330
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1330
    16
    License along with this library; if not, write to the Free Software
slouken@1330
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1330
    18
slouken@1330
    19
    Sam Lantinga
slouken@1330
    20
    slouken@libsdl.org
slouken@1330
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@1330
    23
slouken@1354
    24
#include "SDL_stdinc.h"
slouken@1330
    25
slouken@1331
    26
#ifndef HAVE_GETENV
slouken@1330
    27
slouken@1402
    28
#if defined(__WIN32__) && !defined(_WIN32_WCE)
slouken@1330
    29
slouken@1433
    30
#define WIN32_LEAN_AND_MEAN
slouken@1433
    31
#include <windows.h>
slouken@1330
    32
slouken@1330
    33
/* Note this isn't thread-safe! */
slouken@1330
    34
slouken@1662
    35
static char *SDL_envmem = NULL; /* Ugh, memory leak */
slouken@1456
    36
static size_t SDL_envmemlen = 0;
slouken@1330
    37
slouken@1330
    38
/* Put a variable of the form "name=value" into the environment */
slouken@1662
    39
int
slouken@1662
    40
SDL_putenv (const char *variable)
slouken@1330
    41
{
slouken@1662
    42
    size_t bufferlen;
slouken@1662
    43
    char *value;
slouken@1662
    44
    const char *sep;
slouken@1330
    45
slouken@1662
    46
    sep = SDL_strchr (variable, '=');
slouken@1662
    47
    if (sep == NULL) {
slouken@1662
    48
        return -1;
slouken@1662
    49
    }
slouken@1662
    50
    bufferlen = SDL_strlen (variable) + 1;
slouken@1662
    51
    if (bufferlen > SDL_envmemlen) {
slouken@1662
    52
        char *newmem = (char *) SDL_realloc (SDL_envmem, bufferlen);
slouken@1662
    53
        if (newmem == NULL) {
slouken@1662
    54
            return -1;
slouken@1662
    55
        }
slouken@1662
    56
        SDL_envmem = newmem;
slouken@1662
    57
        SDL_envmemlen = bufferlen;
slouken@1662
    58
    }
slouken@1662
    59
    SDL_strlcpy (SDL_envmem, variable, bufferlen);
slouken@1662
    60
    value = SDL_envmem + (sep - variable);
slouken@1662
    61
    *value++ = '\0';
slouken@1662
    62
    if (!SetEnvironmentVariable (SDL_envmem, *value ? value : NULL)) {
slouken@1662
    63
        return -1;
slouken@1662
    64
    }
slouken@1662
    65
    return 0;
slouken@1330
    66
}
slouken@1330
    67
slouken@1330
    68
/* Retrieve a variable named "name" from the environment */
slouken@1662
    69
char *
slouken@1662
    70
SDL_getenv (const char *name)
slouken@1330
    71
{
slouken@1662
    72
    size_t bufferlen;
slouken@1330
    73
slouken@1662
    74
    bufferlen =
slouken@1662
    75
        GetEnvironmentVariable (name, SDL_envmem, (DWORD) SDL_envmemlen);
slouken@1662
    76
    if (bufferlen == 0) {
slouken@1662
    77
        return NULL;
slouken@1662
    78
    }
slouken@1662
    79
    if (bufferlen > SDL_envmemlen) {
slouken@1662
    80
        char *newmem = (char *) SDL_realloc (SDL_envmem, bufferlen);
slouken@1662
    81
        if (newmem == NULL) {
slouken@1662
    82
            return NULL;
slouken@1662
    83
        }
slouken@1662
    84
        SDL_envmem = newmem;
slouken@1662
    85
        SDL_envmemlen = bufferlen;
slouken@1662
    86
        GetEnvironmentVariable (name, SDL_envmem, (DWORD) SDL_envmemlen);
slouken@1662
    87
    }
slouken@1662
    88
    return SDL_envmem;
slouken@1330
    89
}
slouken@1330
    90
slouken@1330
    91
#else /* roll our own */
slouken@1330
    92
slouken@1662
    93
static char **SDL_env = (char **) 0;
slouken@1330
    94
slouken@1330
    95
/* Put a variable of the form "name=value" into the environment */
slouken@1662
    96
int
slouken@1662
    97
SDL_putenv (const char *variable)
slouken@1330
    98
{
slouken@1662
    99
    const char *name, *value;
slouken@1662
   100
    int added;
slouken@1662
   101
    int len, i;
slouken@1662
   102
    char **new_env;
slouken@1662
   103
    char *new_variable;
slouken@1330
   104
slouken@1662
   105
    /* A little error checking */
slouken@1662
   106
    if (!variable) {
slouken@1662
   107
        return (-1);
slouken@1662
   108
    }
slouken@1662
   109
    name = variable;
slouken@1662
   110
    for (value = variable; *value && (*value != '='); ++value) {
slouken@1662
   111
        /* Keep looking for '=' */ ;
slouken@1662
   112
    }
slouken@1662
   113
    if (*value) {
slouken@1662
   114
        ++value;
slouken@1662
   115
    } else {
slouken@1662
   116
        return (-1);
slouken@1662
   117
    }
slouken@1330
   118
slouken@1662
   119
    /* Allocate memory for the variable */
slouken@1662
   120
    new_variable = SDL_strdup (variable);
slouken@1662
   121
    if (!new_variable) {
slouken@1662
   122
        return (-1);
slouken@1662
   123
    }
slouken@1330
   124
slouken@1662
   125
    /* Actually put it into the environment */
slouken@1662
   126
    added = 0;
slouken@1662
   127
    i = 0;
slouken@1662
   128
    if (SDL_env) {
slouken@1662
   129
        /* Check to see if it's already there... */
slouken@1662
   130
        len = (value - name);
slouken@1662
   131
        for (; SDL_env[i]; ++i) {
slouken@1662
   132
            if (SDL_strncmp (SDL_env[i], name, len) == 0) {
slouken@1662
   133
                break;
slouken@1662
   134
            }
slouken@1662
   135
        }
slouken@1662
   136
        /* If we found it, just replace the entry */
slouken@1662
   137
        if (SDL_env[i]) {
slouken@1662
   138
            SDL_free (SDL_env[i]);
slouken@1662
   139
            SDL_env[i] = new_variable;
slouken@1662
   140
            added = 1;
slouken@1662
   141
        }
slouken@1662
   142
    }
slouken@1330
   143
slouken@1662
   144
    /* Didn't find it in the environment, expand and add */
slouken@1662
   145
    if (!added) {
slouken@1662
   146
        new_env = SDL_realloc (SDL_env, (i + 2) * sizeof (char *));
slouken@1662
   147
        if (new_env) {
slouken@1662
   148
            SDL_env = new_env;
slouken@1662
   149
            SDL_env[i++] = new_variable;
slouken@1662
   150
            SDL_env[i++] = (char *) 0;
slouken@1662
   151
            added = 1;
slouken@1662
   152
        } else {
slouken@1662
   153
            SDL_free (new_variable);
slouken@1662
   154
        }
slouken@1662
   155
    }
slouken@1662
   156
    return (added ? 0 : -1);
slouken@1330
   157
}
slouken@1330
   158
slouken@1330
   159
/* Retrieve a variable named "name" from the environment */
slouken@1662
   160
char *
slouken@1662
   161
SDL_getenv (const char *name)
slouken@1330
   162
{
slouken@1662
   163
    int len, i;
slouken@1662
   164
    char *value;
slouken@1330
   165
slouken@1662
   166
    value = (char *) 0;
slouken@1662
   167
    if (SDL_env) {
slouken@1662
   168
        len = SDL_strlen (name);
slouken@1662
   169
        for (i = 0; SDL_env[i] && !value; ++i) {
slouken@1662
   170
            if ((SDL_strncmp (SDL_env[i], name, len) == 0) &&
slouken@1662
   171
                (SDL_env[i][len] == '=')) {
slouken@1662
   172
                value = &SDL_env[i][len + 1];
slouken@1662
   173
            }
slouken@1662
   174
        }
slouken@1662
   175
    }
slouken@1662
   176
    return value;
slouken@1330
   177
}
slouken@1330
   178
slouken@1402
   179
#endif /* __WIN32__ */
slouken@1330
   180
slouken@1331
   181
#endif /* !HAVE_GETENV */
slouken@1331
   182
slouken@1330
   183
#ifdef TEST_MAIN
slouken@1330
   184
#include <stdio.h>
slouken@1330
   185
slouken@1662
   186
int
slouken@1662
   187
main (int argc, char *argv[])
slouken@1330
   188
{
slouken@1662
   189
    char *value;
slouken@1330
   190
slouken@1662
   191
    printf ("Checking for non-existent variable... ");
slouken@1662
   192
    fflush (stdout);
slouken@1662
   193
    if (!SDL_getenv ("EXISTS")) {
slouken@1662
   194
        printf ("okay\n");
slouken@1662
   195
    } else {
slouken@1662
   196
        printf ("failed\n");
slouken@1662
   197
    }
slouken@1662
   198
    printf ("Setting FIRST=VALUE1 in the environment... ");
slouken@1662
   199
    fflush (stdout);
slouken@1662
   200
    if (SDL_putenv ("FIRST=VALUE1") == 0) {
slouken@1662
   201
        printf ("okay\n");
slouken@1662
   202
    } else {
slouken@1662
   203
        printf ("failed\n");
slouken@1662
   204
    }
slouken@1662
   205
    printf ("Getting FIRST from the environment... ");
slouken@1662
   206
    fflush (stdout);
slouken@1662
   207
    value = SDL_getenv ("FIRST");
slouken@1662
   208
    if (value && (SDL_strcmp (value, "VALUE1") == 0)) {
slouken@1662
   209
        printf ("okay\n");
slouken@1662
   210
    } else {
slouken@1662
   211
        printf ("failed\n");
slouken@1662
   212
    }
slouken@1662
   213
    printf ("Setting SECOND=VALUE2 in the environment... ");
slouken@1662
   214
    fflush (stdout);
slouken@1662
   215
    if (SDL_putenv ("SECOND=VALUE2") == 0) {
slouken@1662
   216
        printf ("okay\n");
slouken@1662
   217
    } else {
slouken@1662
   218
        printf ("failed\n");
slouken@1662
   219
    }
slouken@1662
   220
    printf ("Getting SECOND from the environment... ");
slouken@1662
   221
    fflush (stdout);
slouken@1662
   222
    value = SDL_getenv ("SECOND");
slouken@1662
   223
    if (value && (SDL_strcmp (value, "VALUE2") == 0)) {
slouken@1662
   224
        printf ("okay\n");
slouken@1662
   225
    } else {
slouken@1662
   226
        printf ("failed\n");
slouken@1662
   227
    }
slouken@1662
   228
    printf ("Setting FIRST=NOVALUE in the environment... ");
slouken@1662
   229
    fflush (stdout);
slouken@1662
   230
    if (SDL_putenv ("FIRST=NOVALUE") == 0) {
slouken@1662
   231
        printf ("okay\n");
slouken@1662
   232
    } else {
slouken@1662
   233
        printf ("failed\n");
slouken@1662
   234
    }
slouken@1662
   235
    printf ("Getting FIRST from the environment... ");
slouken@1662
   236
    fflush (stdout);
slouken@1662
   237
    value = SDL_getenv ("FIRST");
slouken@1662
   238
    if (value && (SDL_strcmp (value, "NOVALUE") == 0)) {
slouken@1662
   239
        printf ("okay\n");
slouken@1662
   240
    } else {
slouken@1662
   241
        printf ("failed\n");
slouken@1662
   242
    }
slouken@1662
   243
    printf ("Checking for non-existent variable... ");
slouken@1662
   244
    fflush (stdout);
slouken@1662
   245
    if (!SDL_getenv ("EXISTS")) {
slouken@1662
   246
        printf ("okay\n");
slouken@1662
   247
    } else {
slouken@1662
   248
        printf ("failed\n");
slouken@1662
   249
    }
slouken@1662
   250
    return (0);
slouken@1330
   251
}
slouken@1330
   252
#endif /* TEST_MAIN */
slouken@1662
   253
/* vi: set ts=4 sw=4 expandtab: */