src/stdlib/SDL_getenv.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 08:58:51 +0000
branchSDL-1.2
changeset 4214 4250beeb5ad1
parent 4159 a1b03ba2fcd0
child 6137 4720145f848b
permissions -rw-r--r--
Oh yeah, we have GLX support too.
slouken@1330
     1
/*
slouken@1330
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 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
icculus@3975
    28
#if defined(__WIN32__) && !defined(_WIN32_WCE) && !defined(__SYMBIAN32__)
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@1330
    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@1330
    39
int SDL_putenv(const char *variable)
slouken@1330
    40
{
slouken@1456
    41
	size_t bufferlen;
slouken@1330
    42
	char *value;
slouken@1330
    43
	const char *sep;
slouken@1330
    44
slouken@1336
    45
	sep = SDL_strchr(variable, '=');
slouken@1330
    46
	if ( sep == NULL ) {
slouken@1330
    47
		return -1;
slouken@1330
    48
	}
slouken@1336
    49
	bufferlen = SDL_strlen(variable)+1;
slouken@1330
    50
	if ( bufferlen > SDL_envmemlen ) {
slouken@1336
    51
		char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
slouken@1330
    52
		if ( newmem == NULL ) {
slouken@1330
    53
			return -1;
slouken@1330
    54
		}
slouken@1330
    55
		SDL_envmem = newmem;
slouken@1330
    56
		SDL_envmemlen = bufferlen;
slouken@1330
    57
	}
slouken@1379
    58
	SDL_strlcpy(SDL_envmem, variable, bufferlen);
slouken@1330
    59
	value = SDL_envmem + (sep - variable);
slouken@1330
    60
	*value++ = '\0';
slouken@1330
    61
	if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
slouken@1330
    62
		return -1;
slouken@1330
    63
	}
slouken@1330
    64
	return 0;
slouken@1330
    65
}
slouken@1330
    66
slouken@1330
    67
/* Retrieve a variable named "name" from the environment */
slouken@1330
    68
char *SDL_getenv(const char *name)
slouken@1330
    69
{
slouken@1456
    70
	size_t bufferlen;
slouken@1330
    71
slouken@1472
    72
	bufferlen = GetEnvironmentVariable(name, SDL_envmem, (DWORD)SDL_envmemlen);
slouken@1330
    73
	if ( bufferlen == 0 ) {
slouken@1330
    74
		return NULL;
slouken@1330
    75
	}
slouken@1330
    76
	if ( bufferlen > SDL_envmemlen ) {
slouken@1336
    77
		char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
slouken@1330
    78
		if ( newmem == NULL ) {
slouken@1330
    79
			return NULL;
slouken@1330
    80
		}
slouken@1330
    81
		SDL_envmem = newmem;
slouken@1330
    82
		SDL_envmemlen = bufferlen;
slouken@1472
    83
		GetEnvironmentVariable(name, SDL_envmem, (DWORD)SDL_envmemlen);
slouken@1330
    84
	}
slouken@1330
    85
	return SDL_envmem;
slouken@1330
    86
}
slouken@1330
    87
slouken@1330
    88
#else /* roll our own */
slouken@1330
    89
slouken@1330
    90
static char **SDL_env = (char **)0;
slouken@1330
    91
slouken@1330
    92
/* Put a variable of the form "name=value" into the environment */
slouken@1330
    93
int SDL_putenv(const char *variable)
slouken@1330
    94
{
slouken@1330
    95
	const char *name, *value;
slouken@1330
    96
	int added;
slouken@1330
    97
	int len, i;
slouken@1330
    98
	char **new_env;
slouken@1330
    99
	char *new_variable;
slouken@1330
   100
slouken@1330
   101
	/* A little error checking */
slouken@1330
   102
	if ( ! variable ) {
slouken@1330
   103
		return(-1);
slouken@1330
   104
	}
slouken@1330
   105
	name = variable;
slouken@1330
   106
	for ( value=variable; *value && (*value != '='); ++value ) {
slouken@1330
   107
		/* Keep looking for '=' */ ;
slouken@1330
   108
	}
slouken@1330
   109
	if ( *value ) {
slouken@1330
   110
		++value;
slouken@1330
   111
	} else {
slouken@1330
   112
		return(-1);
slouken@1330
   113
	}
slouken@1330
   114
slouken@1330
   115
	/* Allocate memory for the variable */
slouken@1379
   116
	new_variable = SDL_strdup(variable);
slouken@1330
   117
	if ( ! new_variable ) {
slouken@1330
   118
		return(-1);
slouken@1330
   119
	}
slouken@1330
   120
slouken@1330
   121
	/* Actually put it into the environment */
slouken@1330
   122
	added = 0;
slouken@1330
   123
	i = 0;
slouken@1330
   124
	if ( SDL_env ) {
slouken@1330
   125
		/* Check to see if it's already there... */
slouken@1330
   126
		len = (value - name);
slouken@1330
   127
		for ( ; SDL_env[i]; ++i ) {
slouken@1341
   128
			if ( SDL_strncmp(SDL_env[i], name, len) == 0 ) {
slouken@1330
   129
				break;
slouken@1330
   130
			}
slouken@1330
   131
		}
slouken@1330
   132
		/* If we found it, just replace the entry */
slouken@1330
   133
		if ( SDL_env[i] ) {
slouken@1341
   134
			SDL_free(SDL_env[i]);
slouken@1330
   135
			SDL_env[i] = new_variable;
slouken@1330
   136
			added = 1;
slouken@1330
   137
		}
slouken@1330
   138
	}
slouken@1330
   139
slouken@1330
   140
	/* Didn't find it in the environment, expand and add */
slouken@1330
   141
	if ( ! added ) {
slouken@1336
   142
		new_env = SDL_realloc(SDL_env, (i+2)*sizeof(char *));
slouken@1330
   143
		if ( new_env ) {
slouken@1330
   144
			SDL_env = new_env;
slouken@1330
   145
			SDL_env[i++] = new_variable;
slouken@1330
   146
			SDL_env[i++] = (char *)0;
slouken@1330
   147
			added = 1;
slouken@1330
   148
		} else {
slouken@1341
   149
			SDL_free(new_variable);
slouken@1330
   150
		}
slouken@1330
   151
	}
slouken@1330
   152
	return (added ? 0 : -1);
slouken@1330
   153
}
slouken@1330
   154
slouken@1330
   155
/* Retrieve a variable named "name" from the environment */
slouken@1330
   156
char *SDL_getenv(const char *name)
slouken@1330
   157
{
slouken@1330
   158
	int len, i;
slouken@1330
   159
	char *value;
slouken@1330
   160
slouken@1330
   161
	value = (char *)0;
slouken@1330
   162
	if ( SDL_env ) {
slouken@1341
   163
		len = SDL_strlen(name);
slouken@1330
   164
		for ( i=0; SDL_env[i] && !value; ++i ) {
slouken@1341
   165
			if ( (SDL_strncmp(SDL_env[i], name, len) == 0) &&
slouken@1330
   166
			     (SDL_env[i][len] == '=') ) {
slouken@1330
   167
				value = &SDL_env[i][len+1];
slouken@1330
   168
			}
slouken@1330
   169
		}
slouken@1330
   170
	}
slouken@1330
   171
	return value;
slouken@1330
   172
}
slouken@1330
   173
slouken@1402
   174
#endif /* __WIN32__ */
slouken@1330
   175
slouken@1331
   176
#endif /* !HAVE_GETENV */
slouken@1331
   177
slouken@1330
   178
#ifdef TEST_MAIN
slouken@1330
   179
#include <stdio.h>
slouken@1330
   180
slouken@1330
   181
int main(int argc, char *argv[])
slouken@1330
   182
{
slouken@1330
   183
	char *value;
slouken@1330
   184
slouken@1330
   185
	printf("Checking for non-existent variable... ");
slouken@1330
   186
	fflush(stdout);
slouken@1341
   187
	if ( ! SDL_getenv("EXISTS") ) {
slouken@1330
   188
		printf("okay\n");
slouken@1330
   189
	} else {
slouken@1330
   190
		printf("failed\n");
slouken@1330
   191
	}
slouken@1330
   192
	printf("Setting FIRST=VALUE1 in the environment... ");
slouken@1330
   193
	fflush(stdout);
slouken@1341
   194
	if ( SDL_putenv("FIRST=VALUE1") == 0 ) {
slouken@1330
   195
		printf("okay\n");
slouken@1330
   196
	} else {
slouken@1330
   197
		printf("failed\n");
slouken@1330
   198
	}
slouken@1330
   199
	printf("Getting FIRST from the environment... ");
slouken@1330
   200
	fflush(stdout);
slouken@1341
   201
	value = SDL_getenv("FIRST");
slouken@1341
   202
	if ( value && (SDL_strcmp(value, "VALUE1") == 0) ) {
slouken@1330
   203
		printf("okay\n");
slouken@1330
   204
	} else {
slouken@1330
   205
		printf("failed\n");
slouken@1330
   206
	}
slouken@1330
   207
	printf("Setting SECOND=VALUE2 in the environment... ");
slouken@1330
   208
	fflush(stdout);
slouken@1341
   209
	if ( SDL_putenv("SECOND=VALUE2") == 0 ) {
slouken@1330
   210
		printf("okay\n");
slouken@1330
   211
	} else {
slouken@1330
   212
		printf("failed\n");
slouken@1330
   213
	}
slouken@1330
   214
	printf("Getting SECOND from the environment... ");
slouken@1330
   215
	fflush(stdout);
slouken@1341
   216
	value = SDL_getenv("SECOND");
slouken@1341
   217
	if ( value && (SDL_strcmp(value, "VALUE2") == 0) ) {
slouken@1330
   218
		printf("okay\n");
slouken@1330
   219
	} else {
slouken@1330
   220
		printf("failed\n");
slouken@1330
   221
	}
slouken@1330
   222
	printf("Setting FIRST=NOVALUE in the environment... ");
slouken@1330
   223
	fflush(stdout);
slouken@1341
   224
	if ( SDL_putenv("FIRST=NOVALUE") == 0 ) {
slouken@1330
   225
		printf("okay\n");
slouken@1330
   226
	} else {
slouken@1330
   227
		printf("failed\n");
slouken@1330
   228
	}
slouken@1330
   229
	printf("Getting FIRST from the environment... ");
slouken@1330
   230
	fflush(stdout);
slouken@1341
   231
	value = SDL_getenv("FIRST");
slouken@1341
   232
	if ( value && (SDL_strcmp(value, "NOVALUE") == 0) ) {
slouken@1330
   233
		printf("okay\n");
slouken@1330
   234
	} else {
slouken@1330
   235
		printf("failed\n");
slouken@1330
   236
	}
slouken@1330
   237
	printf("Checking for non-existent variable... ");
slouken@1330
   238
	fflush(stdout);
slouken@1341
   239
	if ( ! SDL_getenv("EXISTS") ) {
slouken@1330
   240
		printf("okay\n");
slouken@1330
   241
	} else {
slouken@1330
   242
		printf("failed\n");
slouken@1330
   243
	}
slouken@1330
   244
	return(0);
slouken@1330
   245
}
slouken@1330
   246
#endif /* TEST_MAIN */
slouken@1330
   247