src/stdlib/SDL_getenv.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 06 Feb 2006 08:28:51 +0000
changeset 1330 450721ad5436
child 1331 1cbaeee565b1
permissions -rw-r--r--
It's now possible to build SDL without any C runtime at all on Windows,
using Visual C++ 2005
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@1330
    23
#include "SDL_stdlib.h"
slouken@1330
    24
#include "SDL_string.h"
slouken@1330
    25
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
#include "SDL_string.h"
slouken@1330
    31
slouken@1330
    32
/* Note this isn't thread-safe! */
slouken@1330
    33
slouken@1330
    34
static char *SDL_envmem = NULL;	/* Ugh, memory leak */
slouken@1330
    35
static DWORD SDL_envmemlen = 0;
slouken@1330
    36
slouken@1330
    37
/* Put a variable of the form "name=value" into the environment */
slouken@1330
    38
int SDL_putenv(const char *variable)
slouken@1330
    39
{
slouken@1330
    40
	DWORD bufferlen;
slouken@1330
    41
	char *value;
slouken@1330
    42
	const char *sep;
slouken@1330
    43
slouken@1330
    44
	sep = strchr(variable, '=');
slouken@1330
    45
	if ( sep == NULL ) {
slouken@1330
    46
		return -1;
slouken@1330
    47
	}
slouken@1330
    48
	bufferlen = strlen(variable)+1;
slouken@1330
    49
	if ( bufferlen > SDL_envmemlen ) {
slouken@1330
    50
		char *newmem = (char *)realloc(SDL_envmem, bufferlen);
slouken@1330
    51
		if ( newmem == NULL ) {
slouken@1330
    52
			return -1;
slouken@1330
    53
		}
slouken@1330
    54
		SDL_envmem = newmem;
slouken@1330
    55
		SDL_envmemlen = bufferlen;
slouken@1330
    56
	}
slouken@1330
    57
	strcpy(SDL_envmem, variable);
slouken@1330
    58
	value = SDL_envmem + (sep - variable);
slouken@1330
    59
	*value++ = '\0';
slouken@1330
    60
	if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
slouken@1330
    61
		return -1;
slouken@1330
    62
	}
slouken@1330
    63
	return 0;
slouken@1330
    64
}
slouken@1330
    65
slouken@1330
    66
/* Retrieve a variable named "name" from the environment */
slouken@1330
    67
char *SDL_getenv(const char *name)
slouken@1330
    68
{
slouken@1330
    69
	DWORD bufferlen;
slouken@1330
    70
slouken@1330
    71
	bufferlen = GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
slouken@1330
    72
	if ( bufferlen == 0 ) {
slouken@1330
    73
		return NULL;
slouken@1330
    74
	}
slouken@1330
    75
	if ( bufferlen > SDL_envmemlen ) {
slouken@1330
    76
		char *newmem = (char *)realloc(SDL_envmem, bufferlen);
slouken@1330
    77
		if ( newmem == NULL ) {
slouken@1330
    78
			return NULL;
slouken@1330
    79
		}
slouken@1330
    80
		SDL_envmem = newmem;
slouken@1330
    81
		SDL_envmemlen = bufferlen;
slouken@1330
    82
		GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
slouken@1330
    83
	}
slouken@1330
    84
	return SDL_envmem;
slouken@1330
    85
}
slouken@1330
    86
slouken@1330
    87
#else /* roll our own */
slouken@1330
    88
slouken@1330
    89
static char **SDL_env = (char **)0;
slouken@1330
    90
slouken@1330
    91
/* Put a variable of the form "name=value" into the environment */
slouken@1330
    92
int SDL_putenv(const char *variable)
slouken@1330
    93
{
slouken@1330
    94
	const char *name, *value;
slouken@1330
    95
	int added;
slouken@1330
    96
	int len, i;
slouken@1330
    97
	char **new_env;
slouken@1330
    98
	char *new_variable;
slouken@1330
    99
slouken@1330
   100
	/* A little error checking */
slouken@1330
   101
	if ( ! variable ) {
slouken@1330
   102
		return(-1);
slouken@1330
   103
	}
slouken@1330
   104
	name = variable;
slouken@1330
   105
	for ( value=variable; *value && (*value != '='); ++value ) {
slouken@1330
   106
		/* Keep looking for '=' */ ;
slouken@1330
   107
	}
slouken@1330
   108
	if ( *value ) {
slouken@1330
   109
		++value;
slouken@1330
   110
	} else {
slouken@1330
   111
		return(-1);
slouken@1330
   112
	}
slouken@1330
   113
slouken@1330
   114
	/* Allocate memory for the variable */
slouken@1330
   115
	new_variable = (char *)malloc(strlen(variable)+1);
slouken@1330
   116
	if ( ! new_variable ) {
slouken@1330
   117
		return(-1);
slouken@1330
   118
	}
slouken@1330
   119
	strcpy(new_variable, variable);
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@1330
   128
			if ( 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@1330
   134
			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@1330
   142
		new_env = 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@1330
   149
			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@1330
   163
		len = strlen(name);
slouken@1330
   164
		for ( i=0; SDL_env[i] && !value; ++i ) {
slouken@1330
   165
			if ( (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@1330
   174
#endif /* WIN32 */
slouken@1330
   175
slouken@1330
   176
#ifdef TEST_MAIN
slouken@1330
   177
#include <stdio.h>
slouken@1330
   178
slouken@1330
   179
int main(int argc, char *argv[])
slouken@1330
   180
{
slouken@1330
   181
	char *value;
slouken@1330
   182
slouken@1330
   183
	printf("Checking for non-existent variable... ");
slouken@1330
   184
	fflush(stdout);
slouken@1330
   185
	if ( ! getenv("EXISTS") ) {
slouken@1330
   186
		printf("okay\n");
slouken@1330
   187
	} else {
slouken@1330
   188
		printf("failed\n");
slouken@1330
   189
	}
slouken@1330
   190
	printf("Setting FIRST=VALUE1 in the environment... ");
slouken@1330
   191
	fflush(stdout);
slouken@1330
   192
	if ( putenv("FIRST=VALUE1") == 0 ) {
slouken@1330
   193
		printf("okay\n");
slouken@1330
   194
	} else {
slouken@1330
   195
		printf("failed\n");
slouken@1330
   196
	}
slouken@1330
   197
	printf("Getting FIRST from the environment... ");
slouken@1330
   198
	fflush(stdout);
slouken@1330
   199
	value = getenv("FIRST");
slouken@1330
   200
	if ( value && (strcmp(value, "VALUE1") == 0) ) {
slouken@1330
   201
		printf("okay\n");
slouken@1330
   202
	} else {
slouken@1330
   203
		printf("failed\n");
slouken@1330
   204
	}
slouken@1330
   205
	printf("Setting SECOND=VALUE2 in the environment... ");
slouken@1330
   206
	fflush(stdout);
slouken@1330
   207
	if ( putenv("SECOND=VALUE2") == 0 ) {
slouken@1330
   208
		printf("okay\n");
slouken@1330
   209
	} else {
slouken@1330
   210
		printf("failed\n");
slouken@1330
   211
	}
slouken@1330
   212
	printf("Getting SECOND from the environment... ");
slouken@1330
   213
	fflush(stdout);
slouken@1330
   214
	value = getenv("SECOND");
slouken@1330
   215
	if ( value && (strcmp(value, "VALUE2") == 0) ) {
slouken@1330
   216
		printf("okay\n");
slouken@1330
   217
	} else {
slouken@1330
   218
		printf("failed\n");
slouken@1330
   219
	}
slouken@1330
   220
	printf("Setting FIRST=NOVALUE in the environment... ");
slouken@1330
   221
	fflush(stdout);
slouken@1330
   222
	if ( putenv("FIRST=NOVALUE") == 0 ) {
slouken@1330
   223
		printf("okay\n");
slouken@1330
   224
	} else {
slouken@1330
   225
		printf("failed\n");
slouken@1330
   226
	}
slouken@1330
   227
	printf("Getting FIRST from the environment... ");
slouken@1330
   228
	fflush(stdout);
slouken@1330
   229
	value = getenv("FIRST");
slouken@1330
   230
	if ( value && (strcmp(value, "NOVALUE") == 0) ) {
slouken@1330
   231
		printf("okay\n");
slouken@1330
   232
	} else {
slouken@1330
   233
		printf("failed\n");
slouken@1330
   234
	}
slouken@1330
   235
	printf("Checking for non-existent variable... ");
slouken@1330
   236
	fflush(stdout);
slouken@1330
   237
	if ( ! getenv("EXISTS") ) {
slouken@1330
   238
		printf("okay\n");
slouken@1330
   239
	} else {
slouken@1330
   240
		printf("failed\n");
slouken@1330
   241
	}
slouken@1330
   242
	return(0);
slouken@1330
   243
}
slouken@1330
   244
#endif /* TEST_MAIN */
slouken@1330
   245