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