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