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