src/stdlib/SDL_getenv.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 19 Feb 2006 23:46:34 +0000
changeset 1379 c0a74f199ecf
parent 1358 c71e05b4dc2e
child 1402 d910939febfa
permissions -rw-r--r--
Use only safe string functions
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@1379
    56
	SDL_strlcpy(SDL_envmem, variable, bufferlen);
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@1379
   114
	new_variable = SDL_strdup(variable);
slouken@1330
   115
	if ( ! new_variable ) {
slouken@1330
   116
		return(-1);
slouken@1330
   117
	}
slouken@1330
   118
slouken@1330
   119
	/* Actually put it into the environment */
slouken@1330
   120
	added = 0;
slouken@1330
   121
	i = 0;
slouken@1330
   122
	if ( SDL_env ) {
slouken@1330
   123
		/* Check to see if it's already there... */
slouken@1330
   124
		len = (value - name);
slouken@1330
   125
		for ( ; SDL_env[i]; ++i ) {
slouken@1341
   126
			if ( SDL_strncmp(SDL_env[i], name, len) == 0 ) {
slouken@1330
   127
				break;
slouken@1330
   128
			}
slouken@1330
   129
		}
slouken@1330
   130
		/* If we found it, just replace the entry */
slouken@1330
   131
		if ( SDL_env[i] ) {
slouken@1341
   132
			SDL_free(SDL_env[i]);
slouken@1330
   133
			SDL_env[i] = new_variable;
slouken@1330
   134
			added = 1;
slouken@1330
   135
		}
slouken@1330
   136
	}
slouken@1330
   137
slouken@1330
   138
	/* Didn't find it in the environment, expand and add */
slouken@1330
   139
	if ( ! added ) {
slouken@1336
   140
		new_env = SDL_realloc(SDL_env, (i+2)*sizeof(char *));
slouken@1330
   141
		if ( new_env ) {
slouken@1330
   142
			SDL_env = new_env;
slouken@1330
   143
			SDL_env[i++] = new_variable;
slouken@1330
   144
			SDL_env[i++] = (char *)0;
slouken@1330
   145
			added = 1;
slouken@1330
   146
		} else {
slouken@1341
   147
			SDL_free(new_variable);
slouken@1330
   148
		}
slouken@1330
   149
	}
slouken@1330
   150
	return (added ? 0 : -1);
slouken@1330
   151
}
slouken@1330
   152
slouken@1330
   153
/* Retrieve a variable named "name" from the environment */
slouken@1330
   154
char *SDL_getenv(const char *name)
slouken@1330
   155
{
slouken@1330
   156
	int len, i;
slouken@1330
   157
	char *value;
slouken@1330
   158
slouken@1330
   159
	value = (char *)0;
slouken@1330
   160
	if ( SDL_env ) {
slouken@1341
   161
		len = SDL_strlen(name);
slouken@1330
   162
		for ( i=0; SDL_env[i] && !value; ++i ) {
slouken@1341
   163
			if ( (SDL_strncmp(SDL_env[i], name, len) == 0) &&
slouken@1330
   164
			     (SDL_env[i][len] == '=') ) {
slouken@1330
   165
				value = &SDL_env[i][len+1];
slouken@1330
   166
			}
slouken@1330
   167
		}
slouken@1330
   168
	}
slouken@1330
   169
	return value;
slouken@1330
   170
}
slouken@1330
   171
slouken@1330
   172
#endif /* WIN32 */
slouken@1330
   173
slouken@1331
   174
#endif /* !HAVE_GETENV */
slouken@1331
   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@1341
   185
	if ( ! SDL_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@1341
   192
	if ( SDL_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@1341
   199
	value = SDL_getenv("FIRST");
slouken@1341
   200
	if ( value && (SDL_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@1341
   207
	if ( SDL_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@1341
   214
	value = SDL_getenv("SECOND");
slouken@1341
   215
	if ( value && (SDL_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@1341
   222
	if ( SDL_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@1341
   229
	value = SDL_getenv("FIRST");
slouken@1341
   230
	if ( value && (SDL_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@1341
   237
	if ( ! SDL_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