src/stdlib/SDL_getenv.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 06 Feb 2006 08:28:51 +0000
changeset 1330 450721ad5436
child 1331 1cbaeee565b1
permissions -rw-r--r--
It's now possible to build SDL without any C runtime at all on Windows,
using Visual C++ 2005
     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 
    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 = strchr(variable, '=');
    45 	if ( sep == NULL ) {
    46 		return -1;
    47 	}
    48 	bufferlen = strlen(variable)+1;
    49 	if ( bufferlen > SDL_envmemlen ) {
    50 		char *newmem = (char *)realloc(SDL_envmem, bufferlen);
    51 		if ( newmem == NULL ) {
    52 			return -1;
    53 		}
    54 		SDL_envmem = newmem;
    55 		SDL_envmemlen = bufferlen;
    56 	}
    57 	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 *)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 *)malloc(strlen(variable)+1);
   116 	if ( ! new_variable ) {
   117 		return(-1);
   118 	}
   119 	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 ( 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 			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 = 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 			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 = strlen(name);
   164 		for ( i=0; SDL_env[i] && !value; ++i ) {
   165 			if ( (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 #ifdef TEST_MAIN
   177 #include <stdio.h>
   178 
   179 int main(int argc, char *argv[])
   180 {
   181 	char *value;
   182 
   183 	printf("Checking for non-existent variable... ");
   184 	fflush(stdout);
   185 	if ( ! getenv("EXISTS") ) {
   186 		printf("okay\n");
   187 	} else {
   188 		printf("failed\n");
   189 	}
   190 	printf("Setting FIRST=VALUE1 in the environment... ");
   191 	fflush(stdout);
   192 	if ( putenv("FIRST=VALUE1") == 0 ) {
   193 		printf("okay\n");
   194 	} else {
   195 		printf("failed\n");
   196 	}
   197 	printf("Getting FIRST from the environment... ");
   198 	fflush(stdout);
   199 	value = getenv("FIRST");
   200 	if ( value && (strcmp(value, "VALUE1") == 0) ) {
   201 		printf("okay\n");
   202 	} else {
   203 		printf("failed\n");
   204 	}
   205 	printf("Setting SECOND=VALUE2 in the environment... ");
   206 	fflush(stdout);
   207 	if ( putenv("SECOND=VALUE2") == 0 ) {
   208 		printf("okay\n");
   209 	} else {
   210 		printf("failed\n");
   211 	}
   212 	printf("Getting SECOND from the environment... ");
   213 	fflush(stdout);
   214 	value = getenv("SECOND");
   215 	if ( value && (strcmp(value, "VALUE2") == 0) ) {
   216 		printf("okay\n");
   217 	} else {
   218 		printf("failed\n");
   219 	}
   220 	printf("Setting FIRST=NOVALUE in the environment... ");
   221 	fflush(stdout);
   222 	if ( putenv("FIRST=NOVALUE") == 0 ) {
   223 		printf("okay\n");
   224 	} else {
   225 		printf("failed\n");
   226 	}
   227 	printf("Getting FIRST from the environment... ");
   228 	fflush(stdout);
   229 	value = getenv("FIRST");
   230 	if ( value && (strcmp(value, "NOVALUE") == 0) ) {
   231 		printf("okay\n");
   232 	} else {
   233 		printf("failed\n");
   234 	}
   235 	printf("Checking for non-existent variable... ");
   236 	fflush(stdout);
   237 	if ( ! getenv("EXISTS") ) {
   238 		printf("okay\n");
   239 	} else {
   240 		printf("failed\n");
   241 	}
   242 	return(0);
   243 }
   244 #endif /* TEST_MAIN */
   245