src/stdlib/SDL_getenv.c
changeset 1330 450721ad5436
child 1331 1cbaeee565b1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/stdlib/SDL_getenv.c	Mon Feb 06 08:28:51 2006 +0000
     1.3 @@ -0,0 +1,245 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +#include "SDL_stdlib.h"
    1.27 +#include "SDL_string.h"
    1.28 +
    1.29 +
    1.30 +#if defined(WIN32) && !defined(_WIN32_WCE)
    1.31 +
    1.32 +#include "SDL_windows.h"
    1.33 +#include "SDL_string.h"
    1.34 +
    1.35 +/* Note this isn't thread-safe! */
    1.36 +
    1.37 +static char *SDL_envmem = NULL;	/* Ugh, memory leak */
    1.38 +static DWORD SDL_envmemlen = 0;
    1.39 +
    1.40 +/* Put a variable of the form "name=value" into the environment */
    1.41 +int SDL_putenv(const char *variable)
    1.42 +{
    1.43 +	DWORD bufferlen;
    1.44 +	char *value;
    1.45 +	const char *sep;
    1.46 +
    1.47 +	sep = strchr(variable, '=');
    1.48 +	if ( sep == NULL ) {
    1.49 +		return -1;
    1.50 +	}
    1.51 +	bufferlen = strlen(variable)+1;
    1.52 +	if ( bufferlen > SDL_envmemlen ) {
    1.53 +		char *newmem = (char *)realloc(SDL_envmem, bufferlen);
    1.54 +		if ( newmem == NULL ) {
    1.55 +			return -1;
    1.56 +		}
    1.57 +		SDL_envmem = newmem;
    1.58 +		SDL_envmemlen = bufferlen;
    1.59 +	}
    1.60 +	strcpy(SDL_envmem, variable);
    1.61 +	value = SDL_envmem + (sep - variable);
    1.62 +	*value++ = '\0';
    1.63 +	if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
    1.64 +		return -1;
    1.65 +	}
    1.66 +	return 0;
    1.67 +}
    1.68 +
    1.69 +/* Retrieve a variable named "name" from the environment */
    1.70 +char *SDL_getenv(const char *name)
    1.71 +{
    1.72 +	DWORD bufferlen;
    1.73 +
    1.74 +	bufferlen = GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
    1.75 +	if ( bufferlen == 0 ) {
    1.76 +		return NULL;
    1.77 +	}
    1.78 +	if ( bufferlen > SDL_envmemlen ) {
    1.79 +		char *newmem = (char *)realloc(SDL_envmem, bufferlen);
    1.80 +		if ( newmem == NULL ) {
    1.81 +			return NULL;
    1.82 +		}
    1.83 +		SDL_envmem = newmem;
    1.84 +		SDL_envmemlen = bufferlen;
    1.85 +		GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
    1.86 +	}
    1.87 +	return SDL_envmem;
    1.88 +}
    1.89 +
    1.90 +#else /* roll our own */
    1.91 +
    1.92 +static char **SDL_env = (char **)0;
    1.93 +
    1.94 +/* Put a variable of the form "name=value" into the environment */
    1.95 +int SDL_putenv(const char *variable)
    1.96 +{
    1.97 +	const char *name, *value;
    1.98 +	int added;
    1.99 +	int len, i;
   1.100 +	char **new_env;
   1.101 +	char *new_variable;
   1.102 +
   1.103 +	/* A little error checking */
   1.104 +	if ( ! variable ) {
   1.105 +		return(-1);
   1.106 +	}
   1.107 +	name = variable;
   1.108 +	for ( value=variable; *value && (*value != '='); ++value ) {
   1.109 +		/* Keep looking for '=' */ ;
   1.110 +	}
   1.111 +	if ( *value ) {
   1.112 +		++value;
   1.113 +	} else {
   1.114 +		return(-1);
   1.115 +	}
   1.116 +
   1.117 +	/* Allocate memory for the variable */
   1.118 +	new_variable = (char *)malloc(strlen(variable)+1);
   1.119 +	if ( ! new_variable ) {
   1.120 +		return(-1);
   1.121 +	}
   1.122 +	strcpy(new_variable, variable);
   1.123 +
   1.124 +	/* Actually put it into the environment */
   1.125 +	added = 0;
   1.126 +	i = 0;
   1.127 +	if ( SDL_env ) {
   1.128 +		/* Check to see if it's already there... */
   1.129 +		len = (value - name);
   1.130 +		for ( ; SDL_env[i]; ++i ) {
   1.131 +			if ( strncmp(SDL_env[i], name, len) == 0 ) {
   1.132 +				break;
   1.133 +			}
   1.134 +		}
   1.135 +		/* If we found it, just replace the entry */
   1.136 +		if ( SDL_env[i] ) {
   1.137 +			free(SDL_env[i]);
   1.138 +			SDL_env[i] = new_variable;
   1.139 +			added = 1;
   1.140 +		}
   1.141 +	}
   1.142 +
   1.143 +	/* Didn't find it in the environment, expand and add */
   1.144 +	if ( ! added ) {
   1.145 +		new_env = realloc(SDL_env, (i+2)*sizeof(char *));
   1.146 +		if ( new_env ) {
   1.147 +			SDL_env = new_env;
   1.148 +			SDL_env[i++] = new_variable;
   1.149 +			SDL_env[i++] = (char *)0;
   1.150 +			added = 1;
   1.151 +		} else {
   1.152 +			free(new_variable);
   1.153 +		}
   1.154 +	}
   1.155 +	return (added ? 0 : -1);
   1.156 +}
   1.157 +
   1.158 +/* Retrieve a variable named "name" from the environment */
   1.159 +char *SDL_getenv(const char *name)
   1.160 +{
   1.161 +	int len, i;
   1.162 +	char *value;
   1.163 +
   1.164 +	value = (char *)0;
   1.165 +	if ( SDL_env ) {
   1.166 +		len = strlen(name);
   1.167 +		for ( i=0; SDL_env[i] && !value; ++i ) {
   1.168 +			if ( (strncmp(SDL_env[i], name, len) == 0) &&
   1.169 +			     (SDL_env[i][len] == '=') ) {
   1.170 +				value = &SDL_env[i][len+1];
   1.171 +			}
   1.172 +		}
   1.173 +	}
   1.174 +	return value;
   1.175 +}
   1.176 +
   1.177 +#endif /* WIN32 */
   1.178 +
   1.179 +#ifdef TEST_MAIN
   1.180 +#include <stdio.h>
   1.181 +
   1.182 +int main(int argc, char *argv[])
   1.183 +{
   1.184 +	char *value;
   1.185 +
   1.186 +	printf("Checking for non-existent variable... ");
   1.187 +	fflush(stdout);
   1.188 +	if ( ! getenv("EXISTS") ) {
   1.189 +		printf("okay\n");
   1.190 +	} else {
   1.191 +		printf("failed\n");
   1.192 +	}
   1.193 +	printf("Setting FIRST=VALUE1 in the environment... ");
   1.194 +	fflush(stdout);
   1.195 +	if ( putenv("FIRST=VALUE1") == 0 ) {
   1.196 +		printf("okay\n");
   1.197 +	} else {
   1.198 +		printf("failed\n");
   1.199 +	}
   1.200 +	printf("Getting FIRST from the environment... ");
   1.201 +	fflush(stdout);
   1.202 +	value = getenv("FIRST");
   1.203 +	if ( value && (strcmp(value, "VALUE1") == 0) ) {
   1.204 +		printf("okay\n");
   1.205 +	} else {
   1.206 +		printf("failed\n");
   1.207 +	}
   1.208 +	printf("Setting SECOND=VALUE2 in the environment... ");
   1.209 +	fflush(stdout);
   1.210 +	if ( putenv("SECOND=VALUE2") == 0 ) {
   1.211 +		printf("okay\n");
   1.212 +	} else {
   1.213 +		printf("failed\n");
   1.214 +	}
   1.215 +	printf("Getting SECOND from the environment... ");
   1.216 +	fflush(stdout);
   1.217 +	value = getenv("SECOND");
   1.218 +	if ( value && (strcmp(value, "VALUE2") == 0) ) {
   1.219 +		printf("okay\n");
   1.220 +	} else {
   1.221 +		printf("failed\n");
   1.222 +	}
   1.223 +	printf("Setting FIRST=NOVALUE in the environment... ");
   1.224 +	fflush(stdout);
   1.225 +	if ( putenv("FIRST=NOVALUE") == 0 ) {
   1.226 +		printf("okay\n");
   1.227 +	} else {
   1.228 +		printf("failed\n");
   1.229 +	}
   1.230 +	printf("Getting FIRST from the environment... ");
   1.231 +	fflush(stdout);
   1.232 +	value = getenv("FIRST");
   1.233 +	if ( value && (strcmp(value, "NOVALUE") == 0) ) {
   1.234 +		printf("okay\n");
   1.235 +	} else {
   1.236 +		printf("failed\n");
   1.237 +	}
   1.238 +	printf("Checking for non-existent variable... ");
   1.239 +	fflush(stdout);
   1.240 +	if ( ! getenv("EXISTS") ) {
   1.241 +		printf("okay\n");
   1.242 +	} else {
   1.243 +		printf("failed\n");
   1.244 +	}
   1.245 +	return(0);
   1.246 +}
   1.247 +#endif /* TEST_MAIN */
   1.248 +