2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 #include "SDL_stdinc.h"
28 #if defined(__WIN32__) && !defined(_WIN32_WCE) && !defined(__SYMBIAN32__)
30 #define WIN32_LEAN_AND_MEAN
33 /* Note this isn't thread-safe! */
35 static char *SDL_envmem = NULL; /* Ugh, memory leak */
36 static size_t SDL_envmemlen = 0;
38 /* Put a variable of the form "name=value" into the environment */
39 int SDL_putenv(const char *variable)
45 sep = SDL_strchr(variable, '=');
49 bufferlen = SDL_strlen(variable)+1;
50 if ( bufferlen > SDL_envmemlen ) {
51 char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
52 if ( newmem == NULL ) {
56 SDL_envmemlen = bufferlen;
58 SDL_strlcpy(SDL_envmem, variable, bufferlen);
59 value = SDL_envmem + (sep - variable);
61 if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
67 /* Retrieve a variable named "name" from the environment */
68 char *SDL_getenv(const char *name)
72 bufferlen = GetEnvironmentVariable(name, SDL_envmem, (DWORD)SDL_envmemlen);
73 if ( bufferlen == 0 ) {
76 if ( bufferlen > SDL_envmemlen ) {
77 char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
78 if ( newmem == NULL ) {
82 SDL_envmemlen = bufferlen;
83 GetEnvironmentVariable(name, SDL_envmem, (DWORD)SDL_envmemlen);
88 #else /* roll our own */
90 static char **SDL_env = (char **)0;
92 /* Put a variable of the form "name=value" into the environment */
93 int SDL_putenv(const char *variable)
95 const char *name, *value;
101 /* A little error checking */
106 for ( value=variable; *value && (*value != '='); ++value ) {
107 /* Keep looking for '=' */ ;
115 /* Allocate memory for the variable */
116 new_variable = SDL_strdup(variable);
117 if ( ! new_variable ) {
121 /* Actually put it into the environment */
125 /* Check to see if it's already there... */
126 len = (value - name);
127 for ( ; SDL_env[i]; ++i ) {
128 if ( SDL_strncmp(SDL_env[i], name, len) == 0 ) {
132 /* If we found it, just replace the entry */
134 SDL_free(SDL_env[i]);
135 SDL_env[i] = new_variable;
140 /* Didn't find it in the environment, expand and add */
142 new_env = SDL_realloc(SDL_env, (i+2)*sizeof(char *));
145 SDL_env[i++] = new_variable;
146 SDL_env[i++] = (char *)0;
149 SDL_free(new_variable);
152 return (added ? 0 : -1);
155 /* Retrieve a variable named "name" from the environment */
156 char *SDL_getenv(const char *name)
163 len = SDL_strlen(name);
164 for ( i=0; SDL_env[i] && !value; ++i ) {
165 if ( (SDL_strncmp(SDL_env[i], name, len) == 0) &&
166 (SDL_env[i][len] == '=') ) {
167 value = &SDL_env[i][len+1];
174 #endif /* __WIN32__ */
176 #endif /* !HAVE_GETENV */
181 int main(int argc, char *argv[])
185 printf("Checking for non-existent variable... ");
187 if ( ! SDL_getenv("EXISTS") ) {
192 printf("Setting FIRST=VALUE1 in the environment... ");
194 if ( SDL_putenv("FIRST=VALUE1") == 0 ) {
199 printf("Getting FIRST from the environment... ");
201 value = SDL_getenv("FIRST");
202 if ( value && (SDL_strcmp(value, "VALUE1") == 0) ) {
207 printf("Setting SECOND=VALUE2 in the environment... ");
209 if ( SDL_putenv("SECOND=VALUE2") == 0 ) {
214 printf("Getting SECOND from the environment... ");
216 value = SDL_getenv("SECOND");
217 if ( value && (SDL_strcmp(value, "VALUE2") == 0) ) {
222 printf("Setting FIRST=NOVALUE in the environment... ");
224 if ( SDL_putenv("FIRST=NOVALUE") == 0 ) {
229 printf("Getting FIRST from the environment... ");
231 value = SDL_getenv("FIRST");
232 if ( value && (SDL_strcmp(value, "NOVALUE") == 0) ) {
237 printf("Checking for non-existent variable... ");
239 if ( ! SDL_getenv("EXISTS") ) {
246 #endif /* TEST_MAIN */