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