src/stdlib/SDL_getenv.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1472 4aac8563c296
child 2859 99210400e8b9
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
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@1895
    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@1895
    39
int
slouken@1895
    40
SDL_putenv(const char *variable)
slouken@1330
    41
{
slouken@1895
    42
    size_t bufferlen;
slouken@1895
    43
    char *value;
slouken@1895
    44
    const char *sep;
slouken@1330
    45
slouken@1895
    46
    sep = SDL_strchr(variable, '=');
slouken@1895
    47
    if (sep == NULL) {
slouken@1895
    48
        return -1;
slouken@1895
    49
    }
slouken@1895
    50
    bufferlen = SDL_strlen(variable) + 1;
slouken@1895
    51
    if (bufferlen > SDL_envmemlen) {
slouken@1895
    52
        char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
slouken@1895
    53
        if (newmem == NULL) {
slouken@1895
    54
            return -1;
slouken@1895
    55
        }
slouken@1895
    56
        SDL_envmem = newmem;
slouken@1895
    57
        SDL_envmemlen = bufferlen;
slouken@1895
    58
    }
slouken@1895
    59
    SDL_strlcpy(SDL_envmem, variable, bufferlen);
slouken@1895
    60
    value = SDL_envmem + (sep - variable);
slouken@1895
    61
    *value++ = '\0';
slouken@1895
    62
    if (!SetEnvironmentVariable(SDL_envmem, *value ? value : NULL)) {
slouken@1895
    63
        return -1;
slouken@1895
    64
    }
slouken@1895
    65
    return 0;
slouken@1330
    66
}
slouken@1330
    67
slouken@1330
    68
/* Retrieve a variable named "name" from the environment */
slouken@1895
    69
char *
slouken@1895
    70
SDL_getenv(const char *name)
slouken@1330
    71
{
slouken@1895
    72
    size_t bufferlen;
slouken@1330
    73
slouken@1895
    74
    bufferlen =
slouken@1895
    75
        GetEnvironmentVariable(name, SDL_envmem, (DWORD) SDL_envmemlen);
slouken@1895
    76
    if (bufferlen == 0) {
slouken@1895
    77
        return NULL;
slouken@1895
    78
    }
slouken@1895
    79
    if (bufferlen > SDL_envmemlen) {
slouken@1895
    80
        char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
slouken@1895
    81
        if (newmem == NULL) {
slouken@1895
    82
            return NULL;
slouken@1895
    83
        }
slouken@1895
    84
        SDL_envmem = newmem;
slouken@1895
    85
        SDL_envmemlen = bufferlen;
slouken@1895
    86
        GetEnvironmentVariable(name, SDL_envmem, (DWORD) SDL_envmemlen);
slouken@1895
    87
    }
slouken@1895
    88
    return SDL_envmem;
slouken@1330
    89
}
slouken@1330
    90
slouken@1330
    91
#else /* roll our own */
slouken@1330
    92
slouken@1895
    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@1895
    96
int
slouken@1895
    97
SDL_putenv(const char *variable)
slouken@1330
    98
{
slouken@1895
    99
    const char *name, *value;
slouken@1895
   100
    int added;
slouken@1895
   101
    int len, i;
slouken@1895
   102
    char **new_env;
slouken@1895
   103
    char *new_variable;
slouken@1330
   104
slouken@1895
   105
    /* A little error checking */
slouken@1895
   106
    if (!variable) {
slouken@1895
   107
        return (-1);
slouken@1895
   108
    }
slouken@1895
   109
    name = variable;
slouken@1895
   110
    for (value = variable; *value && (*value != '='); ++value) {
slouken@1895
   111
        /* Keep looking for '=' */ ;
slouken@1895
   112
    }
slouken@1895
   113
    if (*value) {
slouken@1895
   114
        ++value;
slouken@1895
   115
    } else {
slouken@1895
   116
        return (-1);
slouken@1895
   117
    }
slouken@1330
   118
slouken@1895
   119
    /* Allocate memory for the variable */
slouken@1895
   120
    new_variable = SDL_strdup(variable);
slouken@1895
   121
    if (!new_variable) {
slouken@1895
   122
        return (-1);
slouken@1895
   123
    }
slouken@1330
   124
slouken@1895
   125
    /* Actually put it into the environment */
slouken@1895
   126
    added = 0;
slouken@1895
   127
    i = 0;
slouken@1895
   128
    if (SDL_env) {
slouken@1895
   129
        /* Check to see if it's already there... */
slouken@1895
   130
        len = (value - name);
slouken@1895
   131
        for (; SDL_env[i]; ++i) {
slouken@1895
   132
            if (SDL_strncmp(SDL_env[i], name, len) == 0) {
slouken@1895
   133
                break;
slouken@1895
   134
            }
slouken@1895
   135
        }
slouken@1895
   136
        /* If we found it, just replace the entry */
slouken@1895
   137
        if (SDL_env[i]) {
slouken@1895
   138
            SDL_free(SDL_env[i]);
slouken@1895
   139
            SDL_env[i] = new_variable;
slouken@1895
   140
            added = 1;
slouken@1895
   141
        }
slouken@1895
   142
    }
slouken@1330
   143
slouken@1895
   144
    /* Didn't find it in the environment, expand and add */
slouken@1895
   145
    if (!added) {
slouken@1895
   146
        new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
slouken@1895
   147
        if (new_env) {
slouken@1895
   148
            SDL_env = new_env;
slouken@1895
   149
            SDL_env[i++] = new_variable;
slouken@1895
   150
            SDL_env[i++] = (char *) 0;
slouken@1895
   151
            added = 1;
slouken@1895
   152
        } else {
slouken@1895
   153
            SDL_free(new_variable);
slouken@1895
   154
        }
slouken@1895
   155
    }
slouken@1895
   156
    return (added ? 0 : -1);
slouken@1330
   157
}
slouken@1330
   158
slouken@1330
   159
/* Retrieve a variable named "name" from the environment */
slouken@1895
   160
char *
slouken@1895
   161
SDL_getenv(const char *name)
slouken@1330
   162
{
slouken@1895
   163
    int len, i;
slouken@1895
   164
    char *value;
slouken@1330
   165
slouken@1895
   166
    value = (char *) 0;
slouken@1895
   167
    if (SDL_env) {
slouken@1895
   168
        len = SDL_strlen(name);
slouken@1895
   169
        for (i = 0; SDL_env[i] && !value; ++i) {
slouken@1895
   170
            if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
slouken@1895
   171
                (SDL_env[i][len] == '=')) {
slouken@1895
   172
                value = &SDL_env[i][len + 1];
slouken@1895
   173
            }
slouken@1895
   174
        }
slouken@1895
   175
    }
slouken@1895
   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@1895
   186
int
slouken@1895
   187
main(int argc, char *argv[])
slouken@1330
   188
{
slouken@1895
   189
    char *value;
slouken@1330
   190
slouken@1895
   191
    printf("Checking for non-existent variable... ");
slouken@1895
   192
    fflush(stdout);
slouken@1895
   193
    if (!SDL_getenv("EXISTS")) {
slouken@1895
   194
        printf("okay\n");
slouken@1895
   195
    } else {
slouken@1895
   196
        printf("failed\n");
slouken@1895
   197
    }
slouken@1895
   198
    printf("Setting FIRST=VALUE1 in the environment... ");
slouken@1895
   199
    fflush(stdout);
slouken@1895
   200
    if (SDL_putenv("FIRST=VALUE1") == 0) {
slouken@1895
   201
        printf("okay\n");
slouken@1895
   202
    } else {
slouken@1895
   203
        printf("failed\n");
slouken@1895
   204
    }
slouken@1895
   205
    printf("Getting FIRST from the environment... ");
slouken@1895
   206
    fflush(stdout);
slouken@1895
   207
    value = SDL_getenv("FIRST");
slouken@1895
   208
    if (value && (SDL_strcmp(value, "VALUE1") == 0)) {
slouken@1895
   209
        printf("okay\n");
slouken@1895
   210
    } else {
slouken@1895
   211
        printf("failed\n");
slouken@1895
   212
    }
slouken@1895
   213
    printf("Setting SECOND=VALUE2 in the environment... ");
slouken@1895
   214
    fflush(stdout);
slouken@1895
   215
    if (SDL_putenv("SECOND=VALUE2") == 0) {
slouken@1895
   216
        printf("okay\n");
slouken@1895
   217
    } else {
slouken@1895
   218
        printf("failed\n");
slouken@1895
   219
    }
slouken@1895
   220
    printf("Getting SECOND from the environment... ");
slouken@1895
   221
    fflush(stdout);
slouken@1895
   222
    value = SDL_getenv("SECOND");
slouken@1895
   223
    if (value && (SDL_strcmp(value, "VALUE2") == 0)) {
slouken@1895
   224
        printf("okay\n");
slouken@1895
   225
    } else {
slouken@1895
   226
        printf("failed\n");
slouken@1895
   227
    }
slouken@1895
   228
    printf("Setting FIRST=NOVALUE in the environment... ");
slouken@1895
   229
    fflush(stdout);
slouken@1895
   230
    if (SDL_putenv("FIRST=NOVALUE") == 0) {
slouken@1895
   231
        printf("okay\n");
slouken@1895
   232
    } else {
slouken@1895
   233
        printf("failed\n");
slouken@1895
   234
    }
slouken@1895
   235
    printf("Getting FIRST from the environment... ");
slouken@1895
   236
    fflush(stdout);
slouken@1895
   237
    value = SDL_getenv("FIRST");
slouken@1895
   238
    if (value && (SDL_strcmp(value, "NOVALUE") == 0)) {
slouken@1895
   239
        printf("okay\n");
slouken@1895
   240
    } else {
slouken@1895
   241
        printf("failed\n");
slouken@1895
   242
    }
slouken@1895
   243
    printf("Checking for non-existent variable... ");
slouken@1895
   244
    fflush(stdout);
slouken@1895
   245
    if (!SDL_getenv("EXISTS")) {
slouken@1895
   246
        printf("okay\n");
slouken@1895
   247
    } else {
slouken@1895
   248
        printf("failed\n");
slouken@1895
   249
    }
slouken@1895
   250
    return (0);
slouken@1330
   251
}
slouken@1330
   252
#endif /* TEST_MAIN */
slouken@1895
   253
/* vi: set ts=4 sw=4 expandtab: */